Skip to content

Instantly share code, notes, and snippets.

@joeriks
Created October 27, 2014 12:51
Show Gist options
  • Save joeriks/87b5f7ef3f5245763e93 to your computer and use it in GitHub Desktop.
Save joeriks/87b5f7ef3f5245763e93 to your computer and use it in GitHub Desktop.
Merchello step
public object GetCreateAndShipOrder(string userName, string productSku)
{
// get product
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 = "foo@bar.se";
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;
var invoice = invoiceService.CreateInvoice(Merchello.Core.Constants.DefaultKeys.InvoiceStatus.Unpaid);
// invoice.SetBillingAddress(destination);
invoice.Total = 100;
var extendedData = new ExtendedDataCollection();
extendedData.SetValue(Merchello.Core.Constants.ExtendedDataKeys.CurrencyCode, "SEK");
extendedData.SetValue(Merchello.Core.Constants.ExtendedDataKeys.Taxable, product.Taxable.ToString());
extendedData.SetValue(Merchello.Core.Constants.ExtendedDataKeys.Shippable, product.Shippable.ToString());
extendedData.SetValue(Merchello.Core.Constants.ExtendedDataKeys.ProductVariantKey, product.ProductVariants.FirstOrDefault().Key.ToString());
//extendedData.AddAddress(destination, AddressType.Shipping);
var l1 = new InvoiceLineItem(LineItemType.Product, product.Name, product.Sku, 1, product.Price, extendedData);
invoice.Items.Add(l1);
var gateway = _merchelloContext.Gateways.Payment.GetAllActivatedProviders().FirstOrDefault();
var provider = _merchelloContext.Gateways.Payment.GetProviderByKey(gateway.Key);
var resource = provider.ListResourcesOffered().FirstOrDefault(x => x.ServiceCode == "Cash");
var paymentMethod = provider.PaymentMethods.FirstOrDefault(p => p.Name == "Cash");
invoice.AuthorizeCapturePayment(paymentMethod.Key);
var customerAddress = customer.DefaultCustomerAddress(AddressType.Shipping);
var address = new Address
{
Name = customerAddress.FullName,
Address1 = customerAddress.Address1,
Address2 = customerAddress.Address2,
Locality = customerAddress.Locality,
Region = customerAddress.Region,
PostalCode = customerAddress.PostalCode,
CountryCode = customerAddress.CountryCode,
Phone = customerAddress.Phone,
Organization = customerAddress.Company,
AddressType = customerAddress.AddressType
};
var strategy = new DefaultWarehousePackagingStrategy(_merchelloContext, invoice.Items,address, Guid.NewGuid());
var shipments = strategy.PackageShipments();
var shipment = shipments.FirstOrDefault();
var shipRateQuotes = shipment.ShipmentRateQuotes().ToArray();
var approvedShipRateQuote = shipRateQuotes.FirstOrDefault();
var ext2 = new ExtendedDataCollection();
extendedData.SetValue(Merchello.Core.Constants.ExtendedDataKeys.ShipmentKey, shipment.Key.ToString());
extendedData.SetValue(Merchello.Core.Constants.ExtendedDataKeys.ShipMethodKey, shipment.ShipMethodKey.ToString());
var shipmentInvoiceLine = approvedShipRateQuote.AsLineItemOf<InvoiceLineItem>();
//shipmentInvoiceLine.ExtendedData = ext2;
invoice.Items.Add(shipmentInvoiceLine);
invoiceService.Save(invoice);
var order = invoice.PrepareOrder();
MerchelloContext.Current.Services.OrderService.Save(order);
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment