Try our conversational search powered by Generative AI!

IsShippingCostUpToDate and IsShippingTaxUpToDate flags not being reset properly for shipment (Commerce 12)

Vote:
 

In Commerce 12, SerializedShipment now implements a new interface: IShipmentCalculatedAmount, which has IsShippingCostUpToDate and IsShippingTaxUpToDate. If a line item in the cart is updated with new quantity, these flags are not reset in our solution, which leads to wrong shipping cost in many cases since the weight increases when you increase quantity. Might this be a bug or is it most probably something in our solution that causes this?


The same goes for changing properties on the IShipment.ShippingAddress. We have to replace the entire object with a new one for the flags to be reset. There is an extension method for IShipmentCalculatedAmount called ResetUpToDateFlags, but it's in internal namespace so I assume we are not supposed to call that method?

#194841
Jul 04, 2018 13:04
Vote:
 

That is strange. When you update the lineitem quantity it should reset the flag. Is your lineitem SerializableLineItem, or an implementation that implement ILineItemCalculatedAmount? 

Same goes to IShipment.ShippingAddress. It should reset the flag ...

#194842
Jul 04, 2018 13:26
Vote:
 

Yes, it must be SerializableLineItem since it's a SerializableCart and we call IOrderGroup.CreateLineItem when it's added the first time.

Where are changes to IShipment.ShippingAddress properties handled? I'm looking around in assemblies but can't figure out how they are reset when you set properties on the IOrderAddress. However, if you set a new address on the shipment, I see that ResetUpToDateFlags is called.

#194843
Edited, Jul 04, 2018 13:42
Vote:
 

If you are changing the address of a SerializableShipment, this will be called:

                if (_shippingAddress != value)
                {
                    _shippingAddress = value;

                    // Changing the shipping address will leading to change the sales tax of containing line items.
                    ResetUpToDateFlags(true);
                }

If you have a test case which can reproduce the issue, we're happy to look into it 

#194844
Jul 04, 2018 13:47
Vote:
 

Yes, I see that. But what if you change properties on an already existing address on the shipment? I'm trying to figure out what's going on here so I'll get back to you if/when I have a test case.

#194845
Jul 04, 2018 14:03
Vote:
 

As you can see it's in set operator, so as long as you change it to a different address, that part should be called 

#194846
Jul 04, 2018 14:08
Vote:
 

Yes, then at least I know that this is the way to change a shipping adress. In other words, don't change properties on existing shipping address. Instead, create a new one, copy values from existing if necessary, change desired properties and finally set the new shipping address.

#194847
Jul 04, 2018 14:14
Vote:
 

Ah yes, I misunderstood. Good point - I will need to think about it... 

#194848
Jul 04, 2018 14:20
Vote:
 

I had a look at SerializableLineItem and as far as I can see, only IsShippingTaxUpToDate is reset when quantity is changed. The default IShippingCalculator only checks IsShippingCostUpToDate. Or am I missing something here? :)

#194851
Jul 04, 2018 14:27
Vote:
 

To be honest I'm not 100% sure here (this is ... complicated), but it seems you are correct. I will discuss with the developers who worked on this. Thanks for bringing this into our attention. 

#194852
Jul 04, 2018 14:35
Vote:
 

Hi.

I have checked and found that, our implementation for reduce tax calculation is missing two cases:

  1. Shipping Cost flag is not reset when changing quantity of Line Item (In case: Shipping cost depends on LineItem weight).
  2. Flags (Taxes/Cost: Shipment Tax/Cost, OrderGroup/ Line Item tax) are not reset when changing property of an existing Shipment Address because it does not change Id of OrderAddress, so trigger reset Flag didn't fire.

We are working on those bugs and I hope it will be fixed in the next Commerce release.

Thanks.

#194866
Jul 05, 2018 6:08
Vote:
 

Thanks for the info. Then we'll work around these issues until you release bugfixes.

#194878
Jul 05, 2018 12:53
Vote:
 

Hi, Mattias Olsson.

Commerce 12.4.1 has released and fixed for:

  1. LineItem is updated with new quantity (LinteItem.Quantity = newQuantity): IsShippingCostUpToDate will be reset to re-calculate ShippingCost.  Acctually, previous Commerce 12.4.1, you can also use Shipment.SetLineItemQuantity to update quantity of LineItem, it will reset IsShippingCostUpToDate flag as well.

Currently, we don't support clear IsShippingCostUpToDate flag when changing property of Address.
You must create new Address and assign it to Shipment.Address.
We will document or support it.

Br.

#195444
Edited, Jul 25, 2018 10:54
Vote:
 

@cuvu: I upgraded to 12.9.0 but I am still facing the issue where my shipping cost is not being updated if a new line item is added. I see that my shipping calculation method is not being called when I add a new line item. 

Note: we are not implementing serialized carts.

#197203
Edited, Sep 26, 2018 9:04
Vote:
 

@Siddharth: the shipping cost is not automatically updated when you add a new line item. This thread is about it is cached (i.e. not being recalculated) when you add a new line item AND run the calculator again 

#197207
Sep 26, 2018 9:45
Vote:
 

@Quan Mai: I understand, but doesnt OrderGroup GetShippingSubTotal method use the IsShippingCostUpToDate behind the scenes? And when I add a new line item or change the qty of an existing line item I expect this to be set to false which will trigger a re-calculation of shipping cost. 

#197210
Edited, Sep 26, 2018 10:28
Vote:
 

OK. Can you share how do you add a new lineitem/change quantity. The code I am looking at says that would reset the flag ... 

#197212
Sep 26, 2018 10:42
Vote:
 

@Quan Mai: Thank you for the quick respose:

Add line item:

if (addNewItem)
{
lineItem = cart.CreateLineItem(model.Code);
lineItem.Quantity = model.Quantity;
CreateOrGetFirstShipment(cart, orderForm, orderGroupFactory).LineItems.Add(lineItem);
}

public static IOrderForm CreateOrGetFirstForm(IOrderGroup orderGroup, IOrderGroupFactory orderGroupFactory)
{
IOrderForm item = orderGroup.Forms.FirstOrDefault<IOrderForm>();
if (item == null)
{
item = orderGroupFactory.CreateOrderForm(orderGroup);
item.Name = orderGroup.Name;
orderGroup.Forms.Add(item);
}
return item;
}



Update Line Item:

public virtual RedirectToRouteResult UpdateCart(int? quantity, string sku)
{
if (quantity.HasValue)
{
var cartService = ServiceLocator.Current.GetInstance<ICartService>();
var cart = cartService.LoadActiveShoppingCart();

var lineItem = cart.GetAllLineItems().FirstOrDefault(x => x.Code == sku);
if (lineItem != null)
{
if (quantity.Value > 0)
{
var shipment = cart.GetFirstShipment();
cart.UpdateLineItemQuantity(shipment, lineItem, (decimal)quantity);

cartService.SaveCart(cart);
}
}
}

return RedirectToAction("Index");
}



#197214
Sep 26, 2018 10:55
Vote:
 

Hmm, I can see the bug now - you are working on In memory lineitem and when the item is added/quantity is changed it does not reset the flag. Thanks for letting us know - I will file a bug for this. I think the workaround in this case is to save the cart first. 

#197219
Sep 26, 2018 11:18
Vote:
 

@Quan Mai: Thank you, also as a work around can I override CalculateShippingSubTotal from DefaultOrderFormCalculator to have my shipping cost calculated or will this slow down performace?

#197220
Sep 26, 2018 11:23
Vote:
 

Anything that by pass the cache will have performance implications. However you will have to decide if that outweighes the problem you are solving. 

#197223
Sep 26, 2018 12:08
Vote:
 

Siddharth Gupta Hi again. To help us fix this bug, can you look into the debugger to see at the point you set the quantity or you add an item to cart, which is the real implemenation type of lineitem, shipment and cart? 

#197349
Oct 01, 2018 10:12
Vote:
 

@Quan: Hope this is of help to you:

Shipment -> Mediachase.Commerce.Orders.Shipment

Cart -> Mediachase.Commerce.Orders.Cart

Line Item -> Mediachase.Commerce.Orders.LineItem

Cart LineItems -> EPiServer.Commerce.Order.Internal.InMemoryLineItem

https://www.screencast.com/t/qYmM2DRD1H 
https://www.screencast.com/t/nnpk1XG3Jz 
https://www.screencast.com/t/5B6vQHF4AFfv 
https://www.screencast.com/t/MpBE3ylsvHn 

I am also attaching the quick watch snippets of each of these for reference. Please let me know if you need something else.

#197368
Edited, Oct 01, 2018 19:09
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.