I'm trying to get the Merchello order process and am a bit stuck. I'm trying to create a function which is supposed to add, pay and fullfill an order in one step. I hope someone can help me fill in the blanks
// get product : do I really need to fetch all to get the product by sku?
var product = _merchelloContext.Services.ProductService.GetAll().FirstOrDefault(p => p.Sku == productSku);
// get customer
var customer = _merchelloContext.Services.CustomerService.GetByLoginName(userName);
// create customer with fake data if it does not exist already
Great example, I will check it out more for the frontend process, but unfortunately I forgot to add that I'm going to use this from the backend, and there I cannot use the basket as far as I understand (?). If I do I will reset whatever basket the user already has. Therefore I'm creating an invoice directly instead.
Just FYI I tested the code in my Gist outside of the Merchello Integration tests and there were a couple of internals I used. In the 1.5.0 build I have publicized several internals to make things work as intended.
I did change the
// since we know all the items in the invoice will be shipped we don't need to filter
var shipment = new Shipment(shipmentStatus, warehouse.AsAddress(), billingAddress, invoice.Items);
to
var shipmentService = MerchelloContext.Current.Services.ShipmentService;
var shipment = shipmentService.CreateShipment(... a few overloads ... );
so that we could make sure things are setup the way they need to be - and left the actual Shipment class internal.
Necessary steps to create and fullfil order
I'm trying to get the Merchello order process and am a bit stuck. I'm trying to create a function which is supposed to add, pay and fullfill an order in one step. I hope someone can help me fill in the blanks
// get product : do I really need to fetch all to get the product by sku?
var product = _merchelloContext.Services.ProductService.GetAll().FirstOrDefault(p => p.Sku == productSku);
// get customer
var customer = _merchelloContext.Services.CustomerService.GetByLoginName(userName);
// create customer with fake data if it does not exist already
if (customer == null)
{
customer = _merchelloContext.Services.CustomerService.CreateCustomerWithKey(userName);
customer.Email = "[email protected]";
customer.FirstName = "First";
customer.LastName = "Last name";
var destination = new Address()
{
Name = "Mindfly Web Design Studio",
Address1 = "115 W. Magnolia St.",
Address2 = "Suite 504",
Locality = "Bellingham",
Region = "WA",
PostalCode = "98225",
CountryCode = "SE"
};
customer.CreateCustomerAddress(destination, "defaultShipping", AddressType.Shipping);
customer.CreateCustomerAddress(destination, "defaultBilling", AddressType.Billing);
_merchelloContext.Services.CustomerService.Save(customer);
}
var invoiceService = MerchelloContext.Current.Services.InvoiceService;
// how do I instantiate a new invoice for my newly created customer?
var invoice = invoiceService.CreateInvoice(Merchello.Core.Constants.DefaultKeys.InvoiceStatus.Unpaid);
invoice.Total = 100;
// invoice.AuthorizePayment(paymentMethodKey);
var order = invoice.PrepareOrder();
invoiceService.Save(invoice);
Thanks
/ learning step by step :)
Hmm.. looks like its easier to set the customer in v 1.5 (currently using 1.4.1)
((Invoice)invoice1).CustomerKey = customer.Key;
https://github.com/Merchello/Merchello/blob/c67fac10d2f044eac41a63d23017dfd5a7ba9522/test/Merchello.Tests.IntegrationTests/Examine/InvoiceAndOrderProviderTests.cs#L193
I have an example with a Merchello Surface controller and checkout controller. You are missing some steps.
https://github.com/dfberry/Merchello-Example-Projects/tree/master/src/Checkout
Great example, I will check it out more for the frontend process, but unfortunately I forgot to add that I'm going to use this from the backend, and there I cannot use the basket as far as I understand (?). If I do I will reset whatever basket the user already has. Therefore I'm creating an invoice directly instead.
I think I'm in over my head here :-o Still not gotten this to work, this is what I have now : https://gist.github.com/joeriks/87b5f7ef3f5245763e93 ;
Hi Jonas,
I have created some example code for you. You were pretty close, but think the API is a bit confusing and we need to document it better.
Here is my code https://gist.github.com/rustyswayne/598049064a02fce1597c
Awesome, thanks a lot Rusty, exacly what I needed.
Jonas,
Just FYI I tested the code in my Gist outside of the Merchello Integration tests and there were a couple of internals I used. In the 1.5.0 build I have publicized several internals to make things work as intended.
I did change the
to
so that we could make sure things are setup the way they need to be - and left the actual Shipment class internal.
is working on a reply...