Developer forum

Forum » Templates » Dynamicweb.Ecommerce.Common.Context.Cart.Price not updated after cart step update

Dynamicweb.Ecommerce.Common.Context.Cart.Price not updated after cart step update

Jacob Storgaard Jensen
Reply

Hi, I've taken over this code which is meant to be part of rendering a mini cart


@{
                        double quantity = 0;
                        var price = new PriceInfo();

                        if (!isCartEmpty)
                        {
                            quantity = Dynamicweb.Ecommerce.Common.Context.Cart.OrderLines.CountOnlyProducts;
                            price = Dynamicweb.Ecommerce.Common.Context.Cart.Price;
                        }

}

BUT when you're on a cart step (show basket) and you change the quantity on an orderline and update the cart, everything on the main cart is updated, but the prise in the mini cart is always "one step behind" showing the total price from before I changed and updated the quantity, but refreshing and re-submitting makes both carts show the same...

Any ideas?


Replies

 
Nicolai Pedersen
Reply

Sounds like a really old solution maybe?

 
Jacob Storgaard Jensen
Reply

9.2.17, so not that old, but the implementation of the mini-cart might be ansient!

 
Nicolai Pedersen
Reply

That is more like a baby, so that is not the problem :-).

Looking into this:

The code you provided is going directly at the API - probably early in the template. Later in the template stack, the cart is rendered, and that is what handles the update command.

So you have to use the template tags to render the mini cart, since the rendering of that happens after the update command.

Alternatively you need to run the above code after the cart module has executed.

Or render the 'mini' cart inside the cart template

Or not render a minicart in a checkout page (which is recommended)

BR Nicolai

 
Jacob Storgaard Jensen
Reply

Ok, yeah rendering it and moving as a snippet could do it, but that would only work on the cart page... Hmm...

The page I'm working on is using a View Model Template - @inherits Dynamicweb.Rendering.ViewModelTemplate<Dynamicweb.Frontend.PageViewModel>
Again, I need to get Visual Studio  and Intellisence, I know!... cause I don't know how to get the template tags when in a View Model Template...

I can see on Rapido that the MiniCart.cshtml uses Model.Cart.TotalPrice.PriceWithVat.Formatted to acces the value... Is this only available on 9.3.X  ? Isn't working when using it here.

I can't dictate wether or not the mini cart should be on the cart page - I'm freelancing ;-)

BR Jacob

 
Nicolai Pedersen
Reply

Hi Jacob

There are no template tags when using a ViewModel - that is the entire concept :-). You have a model instead of tags.

You can see the "tags" of the pageview model (properties and methods) here: https://doc.dynamicweb.com/api/html/8175ac67-de8b-696a-7436-6505c1a61e47.htm

The Model.Cart is only available from 9.3+

Happy coding :-)

Nicolai

 
Jacob Storgaard Jensen
Reply

Some news to this oddity! Another issue appeared when I add a product (costs 100) to the cart from a normal product page BUT the product has a 10% discount, the mini cart (with the afformentioned code) renders the total sum of the cart fine the first time I add the product (100-10 = 90 + 59 in freight = 149), BUT then if I add it again, the total amount is now 249... meaning that the discount is not subtracted from the last added instance of the product... Which means every time you add this product the car shows a total price 10 higher than expected... Going to the actual cart page, both totalt prices match and are as expected...

 
Nicolai Pedersen
Reply

Sounds like a faulty add-to-cart implementation... It may have a &redirect=false in there somewhere. And it should not except for rare occasions (i.e. Ajax calls where the minicart is updated differently)

 
Jacob Storgaard Jensen
Reply

It's a form without ajax involved so it reloads on every add to cart event - but the client want's to redirect to a specific page, which works only if we implement it like below (inspired from the total barebones DW templates and added the redirect stuff. I don't know if the redirect parameter accepts it that way, but it redirects fine ;-)

<form class="form-inline" method="post">
    <input type="hidden" name="ID" value="@GetValue("Ecom:Product:Page.ID")" />
    <input type="hidden" name="ProductID" value="@GetValue("Ecom:Product.ID")" />
    <input type="hidden" name="CartCmd" value="add" />
    <input type="hidden" name="ProductURL" id="PageID" value="@GetGlobalValue("Global:Pageview.Url.Raw")"/>
    <input type="hidden" name="Redirect" id="Redirect" value="/Default.aspx?ID=455" />
    <label for="quantity" class="sr-only">@Translate("Qty")</label>
    <div class="input-group input-group-lg">
        <input name="quantity" id="quantity" value="1" type="text" class="form-control" autocomplete="off">
        <span class="input-group-btn">
            <button class="btn btn-primary" role="button" type="submit" @((GetLoop("VariantGroups").Any() && String.IsNullOrEmpty(GetString("Ecom:Product.VariantID")) ? "disabled=\"disabled\"" : ""))>
                <span class="product-added-text">@Translate("Add to cart")</span> <i class="fa fa-shopping-basket"></i>
            </button>
        </span>
    </div>
</form>
 
Nicolai Pedersen
Reply

Sorry, cannot help. It is just wildguessing. Would have to see the entire solution to be able to see what is going on...

 
Jacob Storgaard Jensen
Reply

ROFL!!

As you mentioned earlier, it might help moving the code to the end of the template-stack... putting the code just before the body-tag on the master template renders the correct values in both trouble cases! :-D

So now I'm moving the value with a snippet :-D

 

 

You must be logged in to post in the forum