Add quantity to flyout cart 4.3 beta

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
4 years ago
Hi Everyone,

I have managed to add a remove button and also a change quantity button to the fly-out cart. Everything in the back end is working and also the fly-out cart displays the correct numbers.

The only issue i have is that the top link always displays the last total amount, if i refresh page it shows correct amount.

For example if i change quantity to 5 the cart shows 5 and correct amount, but the top link shows 1. Then i select 1 again cart recalculates works but the top link now shows 5. I am wondering if this is a bug or i am doing something wrong.

I added this to the cart html

                        <div class="quantity">@T("ShoppingCart.Mini.Quantity"): <input style="width:35px;" type="number" id="[email protected]" min="1" max="99" onchange="update_cart_quantity(@item.Id)" required value="@item.Quantity" /></div>


My java script is as followed. I don't think this is the issue as if i do an alert it shows the correct amount in new quantity.


function update_cart_quantity(id)
    {
        var newquantity = document.getElementById("cart_item_quantity-" + id.toString()).value;

       if (AjaxCart.loadWaiting !== false) {
            return;
        }
        AjaxCart.setLoadWaiting(true);

        $.ajax({
            url: '/AlternativewellbeingExtensions/UpdateCartItemQuantity',
            data: { id: id, quantity: newquantity },
            type: "POST",
            success: AjaxCart.success_process,
            complete: AjaxCart.resetLoadWaiting,
            error: AjaxCart.ajaxFailure
        });  


My custom action code is

[HttpPost]
        [IgnoreAntiforgeryToken]
        public IActionResult UpdateCartItemQuantity(int id,int Quantity)
        {

                    
        //display notification message and update appropriate blocks
        var shoppingCarts = _shoppingCartService.GetShoppingCart(_workContext.CurrentCustomer, ShoppingCartType.ShoppingCart, _storeContext.CurrentStore.Id);

        var updatecartitem = shoppingCarts.FirstOrDefault(item => item.Id == id);

                    
            _shoppingCartService.UpdateShoppingCartItem(_workContext.CurrentCustomer,
                        updatecartitem.Id, updatecartitem.AttributesXml, updatecartitem.CustomerEnteredPrice,
                        updatecartitem.RentalStartDateUtc, updatecartitem.RentalEndDateUtc, Quantity, true);

        var updatetopcartsectionhtml = string.Format(_localizationService.GetResource("ShoppingCart.HeaderQuantity"),
                            shoppingCarts.Sum(items => items.Quantity));

        var updateflyoutcartsectionhtml = RenderViewComponentToString("FlyoutShoppingCart");

            //Console.WriteLine(Quantity);

            return Json(new
            {
                success = true,
                message = string.Format("Product quantity updated!", Url.RouteUrl("ShoppingCart")),
                updatetopcartsectionhtml,
                updateflyoutcartsectionhtml
            });
        }


I don't usually do this but it has been driving me a bit barmy for a day, i was just wondering if anybody could maybe spot what i have done wrong.

I am using 4.3 beta as i know it will be released soon and i wanted to update my custom plugin to work with the new version.

Any help is much appreciated.

Thanks
4 years ago
Ok just ignore me i managed to solve it and make it quicker by updating the action with the following code, just in case anybody is interested.


[HttpPost]
        [IgnoreAntiforgeryToken]
        public IActionResult UpdateCartItemQuantity(int id,int Quantity)
        {

                    
        //display notification message and update appropriate blocks
        var shoppingCarts = _shoppingCartService.GetShoppingCart(_workContext.CurrentCustomer, ShoppingCartType.ShoppingCart, _storeContext.CurrentStore.Id);

        var updatecartitem = shoppingCarts.FirstOrDefault(item => item.Id == id);

        updatecartitem.Quantity = Quantity;
        _sciRepository.Update(updatecartitem);

            //_shoppingCartService.UpdateShoppingCartItem(_workContext.CurrentCustomer,
              //          updatecartitem.Id, updatecartitem.AttributesXml, updatecartitem.CustomerEnteredPrice,
                //        updatecartitem.RentalStartDateUtc, updatecartitem.RentalEndDateUtc, Quantity, true);

        var updatetopcartsectionhtml = string.Format(_localizationService.GetResource("ShoppingCart.HeaderQuantity"),
                            shoppingCarts.Sum(items => items.Quantity));

        var updateflyoutcartsectionhtml = RenderViewComponentToString("FlyoutShoppingCart");

            //Console.WriteLine(Quantity);

            return Json(new
            {
                success = true,
                message = string.Format("Product quantity updated!", Url.RouteUrl("ShoppingCart")),
                updatetopcartsectionhtml,
                updateflyoutcartsectionhtml
            });
        }



still though was it something im doing wrong or did i just find a bug?
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.