I'm using packageBasket method to get shipment and to get the shipment rates. But for some reason the returning shipment is always null (ie the returning ienumerable is empty).
What are the conditions so it returns a not null shipment?
What I've done:
have defined a shipping address
have items in the cart/basket
user is logged in
have Shipping Countries defined
have shipping providers defined
my code:
var shipAddress = _checkoutManager.Customer.GetShipToAddress();
var shipment = _basket.PackageBasket(shipAddress).FirstOrDefault();
var shipmentRateQuotes = shipment.ShipmentRateQuotes();
public void SetDummyShippingAddress()
{
var shippingAddress = new AddressModel();
shippingAddress.Address1 = ...
....
_checkoutManager.Customer.SaveShipToAddress(CreateAddress(shippingAddress));
}
In Merchello 2.7.0 all the above extended data properties seem to already be populated automatically from the product catalog.
I had checked all the bits of info I had pieced together from docs and forums:
Flat rate Shipping provider was enabled.
Default country was added.
A range was added to this country with an associated price (range of
0-100000 for testing).
Default Warehouse was configured.
Products were marked shippable.
Products were assigned to Default catalog.
But still shipment quotes always returned a null object for me. I found out from this comment that I needed to add the Warehouse Catalog key to each basket line item in order to get my shipping calculations to return valid quotes:
var product = MerchelloServices.ProductService.GetByKey(addToCart.SelectedKey);
var warehouse = MerchelloServices.WarehouseService.GetDefaultWarehouse();
var extendedData = new ExtendedDataCollection();
extendedData.SetValue(Merchello.Core.Constants.ExtendedDataKeys.WarehouseCatalogKey, warehouse.WarehouseCatalogs.FirstOrDefault().Key.ToString());
// Specific for my needs
product.Taxable = !addToCart.ZeroVAT;
Basket.AddItem(product, $"{product.Name}{(addToCart.ZeroVAT ? " (Zero VAT Rated)" : "")}", addToCart.SelectedQuantity, extendedData);
Basket.Save();
Checkout code:
@using Merchello.Web;
@using Merchello.Core.Checkout;
@using Merchello.Core.Models;
@using MerchelloCore = Merchello.Core;
var customerContext = new CustomerContext(UmbracoContext);
var basket = customerContext.CurrentCustomer.Basket();
var checkoutManager = basket.GetCheckoutManager(new
CheckoutContextSettings
{
ApplyTaxesToInvoice = true,
EmptyBasketOnPaymentSuccess = true,
RaiseCustomerEvents = false,
ResetCustomerManagerDataOnVersionChange = true,
ResetExtendedManagerDataOnVersionChange = true,
ResetOfferManagerDataOnVersionChange = true,
ResetPaymentManagerDataOnVersionChange = true,
ResetShippingManagerDataOnVersionChange = true
});
var warehouse = MerchelloContext.Current.Services.WarehouseService.GetDefaultWarehouse();
// Setting to Warehouse address just for testing - use customer's address here!
checkoutManager.Customer.SaveBillToAddress(warehouse.AsAddress());
checkoutManager.Customer.SaveShipToAddress(warehouse.AsAddress());
var invoice = checkoutManager.Payment.PrepareInvoice();
var shipmentStatus = MerchelloContext.Current.Services.ShipmentService.GetShipmentStatusByKey(MerchelloCore.Constants.ShipmentStatus.Quoted);
var shipment = MerchelloContext.Current.Services.ShipmentService.CreateShipment(shipmentStatus, warehouse.AsAddress(), warehouse.AsAddress(), basket.Items);
var quotes = MerchelloContext.Current.Gateways.Shipping.GetShipRateQuotesForShipment(shipment);
if (quotes.Any())
{
// this check makes certain a quote was returned. For example if the collection of items was outside the allowable
// weight range, the provider would not return a quote.
// Add the first quote to the invoice.
invoice.Items.Add(quotes.FirstOrDefault().AsLineItemOf<InvoiceLineItem>());
}
PackageBasket null shipment
Hello,
I'm using packageBasket method to get shipment and to get the shipment rates. But for some reason the returning shipment is always null (ie the returning ienumerable is empty).
What are the conditions so it returns a not null shipment?
What I've done:
my code:
THANK YOU! :)
did you do:
yes I did
What does Create Address look like?
It just converts my AddressModel to one Merchello.Core.Models.IAddress address.
(in fact this is kinda useless and i changed it to create directly an IAddress instead of an AddressModel)
The error was when I add items to the basket, I wasnt including the extendedData.
I had:
Had to change to
In Merchello 2.7.0 all the above extended data properties seem to already be populated automatically from the product catalog.
I had checked all the bits of info I had pieced together from docs and forums:
But still shipment quotes always returned a null object for me. I found out from this comment that I needed to add the Warehouse Catalog key to each basket line item in order to get my shipping calculations to return valid quotes:
Checkout code:
is working on a reply...