Developer forum

Forum » Development » Update quantity on OrderLine object

Update quantity on OrderLine object

Remi Muller
Reply
 Hi,

I'm using the following code to update the quantity.
if (orderLine.Quantity > 99)
{
orderLine.Quantity = 99;
orderLine.Save();
}

After this the order.Price.PriceWithVAT is still calculated for 100 products.
However other order properties like PriceBeforeFees, OrderLines.Price are reflected correctly.
Should i update the quantity in a different way so it does recalc or is it a bug?

Kind regards,
Remi

Replies

 
Reply
 Hello Remi,
 
Could you provide additional information about this problem?
I mean, where do you use this code (shopping cart, extender)? I have tried to reproduce problem on my solution and all works fine. 
 
Reply
 This is with a OrderTemplateExtender
public override void ExtendTemplate(Template Template, Dynamicweb.eCommerce.Frontend.TemplateExtenderRenderingState RenderingState)
        {
            // Only process requests from the front-end
            if (Dynamicweb.Permissions.Security.Environment.IsInBackend())
                return;
 
            if (RenderingState == Dynamicweb.eCommerce.Frontend.TemplateExtenderRenderingState.Before)
            {
 
                foreach (OrderLine orderLine in Order.OrderLines)
                {
		  if (orderLine.Quantity > 99)
                  {
                    orderLine.Quantity = 99;
                    orderLine.Save();
                  }
                }
Example of values:
Before orderline save=
orderLine.Order.Price {€ 533,25}
orderLine.Order.TotalPriceFormatted "€ 533,25"
orderLine.Order.PriceBeforeFees {€ 500,25}
 
After orderline save=
orderLine.Order.Price {€ 533,25}
orderLine.Order.TotalPriceFormatted "€ 533,25"
orderLine.Order.PriceBeforeFees {€ 495,25}

Only PriceBeforeFees is updated.

Another thing which i find strang is that i am going twice through:
if (RenderingState == Dynamicweb.eCommerce.Frontend.TemplateExtenderRenderingState.Before)

This is not a big problem but a waste of processing. I have seen this before with other template extenders. Can we build a better expression to process once?

I am using Dynamicweb 19.2.1.6 (hotfix)
maybe it is isolated in this version.

Kind regards,
Remi
 
Reply
 Hello Remi,

Could you to try this is extender?

[Dynamicweb.Extensibility.Subscribe(Dynamicweb.Notifications.eCommerce.Cart.Line.Increased)]
public class EcomCartLineIncreasedObserver1 : Dynamicweb.Extensibility.NotificationSubscriber
{
public override void OnNotify(string notification, Dynamicweb.Extensibility.NotificationArgs args)
{
if (args == null || !(args is Dynamicweb.Notifications.eCommerce.Cart.Line.IncreasedArgs))
return;

Dynamicweb.Notifications.eCommerce.Cart.Line.IncreasedArgs item = (Dynamicweb.Notifications.eCommerce.Cart.Line.IncreasedArgs)args;

//todo: insert code here

if (item.IncreasedLine.Quantity > 5)
{
item.IncreasedLine.Quantity = 1;
item.IncreasedLine.Save();
}

}
}

-- Dmitry
 
Reply
 That works for the template displaying totalprice.

But if i inspect the objects after saving(orderline.save) the totalprice is still incorrect as i described above.

Have you debugged and watched the item.IncreasedLine.Order.Price before and after changing the quantity?

Kind regards,
Remi
 
Reply

I have a question about this notifier:

Dynamicweb.Notifications.eCommerce.Cart.Line

.Increased

Can i access the template object and add tags? As in my case i want to inform the user which product the quantity has been maxed.

With OrderTemplateExtender i could just add a msg in a template tag and display this.

Thanks.
Remi.

 
Reply

Set orderline quantity
The code provided by Dmitry should work just fine.
Order.Price will be cached when you access it the first time.
That is why you don't see the change after you set the quantity on the orderline.
Ecom makes a redirect after adding a product to the cart, the cart will then be reloaded and the price recalculated.
The correct price will then be rendered.
 
Render twice - a bug?
You mention that you are going through TemplateExtenderRenderingState.Before twice.
This happens because the order is always rendered in a standard PageTemplateExtender - even if the page contains no order tags?!
I think this is as a bug. Why render a complete order on the page if we don't use it for anything?
 
BR.
Morten
 
Reply
The code from Dmitri works fine. But to able to switch i also need access to the template object and add tags.

 
Reply
Hi Remi, there are few links that can help you to implement OrderTemplateExtender

http://engage.dynamicweb-cms.com/Show-Article-361.aspx?PID=2472&ArticleID=161

http://engage.dynamicweb-cms.com/Show-Article-361.aspx?PID=2472&ArticleID=153
 
Reply
 Oke this not going in the right direction. Let me rephrase the current situation.
----------------------------------
1.
I have a ordertemplate extender that enforces a maximum on quantity.

This does not work completely as it should.
The quantity works but the order total price is still incorrect.
After a second reload of the cart it shows the updated total price.
----------------------------------

2. I use Dynamicweb.Notifications.eCommerce.Cart.Line.Increased

This works great i can enforce a maximum on quantity and total price is visible and correct in the template.
But i do not know if i can access the template object because i also need to set template tags to inform the user.
----------------------------------

Who can tell me what i should do?
Fix scenario 1, i think it is a bug because order total price is not recalculated after updating quantities.
Or explain how to access the template object in scenario 2.

Kind regards,
Remi 
 

 
Reply
Hi Remi,

I haven't tried it, but can't you combine both? E.g. recalculate in the subscriber, add a key to HttpContext.Items and then in the extender see if the key esists and render the apprpriate tags? I am not 100% sure, but I think it would make sense if the subscriber fired before the extender starts extending.

I realize this is a workaround, and the problem may need to be fixed in Dynamicweb, but it might work for you.

Kind regards,

Imar
 
Reply
 
I think you need to store the data in a session variable and fetch it from there in your template extender.

HttpContext.Current.Items is the usual way to share data between subscribers and extenders, but it will not work in this case because ecom makes a redirect before the extender is called and the data stored in items is no longer available.

/Morten
 
Reply
These are all workarounds. Which is fine for the time being.
But from Dynamicweb i expect proper solutions eventually.

Maybe it is better to create a case and ask for a fix on the order object. Even a simple recalculate method would suffice.
I mean orderlines are recalculated and quantities are immediately reflected but is is not possible to recalc the order total i can not believe that.

Not to leave the wrong impression i appreciate all the feedback. Thanks :)

Kind regards,
Remi
 
Reply
This post has been marked as an answer
 Hello Remi,
 
I have reproduced your problem and "Dynamicweb.eCommerce.Frontend.Cart.CartCatch.SaveCart()" solve it. All problem in cache :)
 
public override void ExtendTemplate(Dynamicweb.Templatev2.Template template, TemplateExtenderRenderingState renderingState)
{
if (renderingState == TemplateExtenderRenderingState.Before)
{
if (this.OrderLine.Quantity > 10)
{
this.OrderLine.Quantity = 9;
this.OrderLine.Save();

Dynamicweb.eCommerce.Frontend.Cart.CartCatch.SaveCart();
}
}
}
 
Votes for this answer: 0
 
Reply
>> HttpContext.Current.Items is the usual way to share data between subscribers and extenders, but it will not work in this case because ecom makes a redirect

Ha, yes, you're absolutely right. Thanks for the correction.

Cheers,

Imar
 
Reply
 Hi Dmitri,

I tried Dynamicweb.eCommerce.Frontend.Cart.CartCatch.SaveCart(); and it works great. :)
Thanks for looking into the issue, this is what i needed to know.
This is valuable info.
I looked it up in the api documentation which states:

SaveCart Method: Saves the cart id to a cookie

I guess the cart is already saved to a cookie and in my case i need to update the cart cookie because of changes to the cart.

Kind regards,
Remi

 

 
Remi Muller
Reply
FYI follow up on this thread is located here: http://developer.dynamicweb-cms.com/forum.aspx?ThreadID=28036

 

You must be logged in to post in the forum