Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • iiii 15 posts 154 karma points
    Sep 13, 2017 @ 09:01
    iiii
    0

    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:

    • 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();
    

    THANK YOU! :)

  • Tolu Jemilua 39 posts 166 karma points
    Sep 13, 2017 @ 15:54
    Tolu Jemilua
    0

    did you do:

      _checkoutManager.Customer.SaveShipToAddress(someaddress);
    
  • iiii 15 posts 154 karma points
    Sep 14, 2017 @ 13:10
    iiii
    0

    yes I did

    public void SetDummyShippingAddress()
        {
            var shippingAddress = new AddressModel();
            shippingAddress.Address1 = ...
            ....
            _checkoutManager.Customer.SaveShipToAddress(CreateAddress(shippingAddress));
        }
    
  • Tolu Jemilua 39 posts 166 karma points
    Sep 14, 2017 @ 15:10
    Tolu Jemilua
    0

    What does Create Address look like?

  • iiii 15 posts 154 karma points
    Sep 14, 2017 @ 15:29
    iiii
    0

    It just converts my AddressModel to one Merchello.Core.Models.IAddress address.

    private IAddress CreateAddress(AddressModel model)
    {
                var address = new Address();
                address.Address1 = model.Address1;
                ...
                return address;
    }
    

    (in fact this is kinda useless and i changed it to create directly an IAddress instead of an AddressModel)

  • iiii 15 posts 154 karma points
    Sep 22, 2017 @ 09:10
    iiii
    100

    The error was when I add items to the basket, I wasnt including the extendedData.

    I had:

    public void AddItem(string prodSku, int quantity)
    {
       if (!prodSku.IsNullOrWhiteSpace())
        {
            var product = MerchelloHelper.Query.Product.GetBySku(prodSku);
    
            if (!_basket.Items.Any(i => i.Key.Equals(product.Key)))
            {
                _basket.AddItem(product.Name,product.Sku, quantity, product.Price);
                _basket.Save();
            }
        }
    }
    

    Had to change to

    public void AddItem(string prodSku, int quantity)
    {
        if (!prodSku.IsNullOrWhiteSpace())
        {
            var product = MerchelloHelper.Query.Product.GetProductVariantBySku(prodSku);
    
            if (!_basket.Items.Any(i => i.Key.Equals(product.Key)))
            {
                var extendedData = new ExtendedDataCollection();
                extendedData.SetValue(Merchello.Core.Constants.ExtendedDataKeys.CurrencyCode, "USD");
                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.Key.ToString());
    
                _basket.AddItem(product.Name,product.Sku, quantity, product.Price, extendedData);
                _basket.Save();
            }
        }
    }
    
  • Conor Breen 11 posts 100 karma points
    May 06, 2019 @ 15:45
    Conor Breen
    0

    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>());
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft