Jump to content

Problem with cart quantity update in header


Recommended Posts

Hey, 

I made a module where I need to get or create the Cart object to add the product in the shopping cart.

On "Add to shopping cart" action, if context has already a Cart created, I call this cart and the "1" next to the shopping cart icon appears instantly when I add the product.

if ($this->context->cookie->id_cart){
    $cart = $this->context->cart;
    $cart->my_custom_field = Tools::getValue('svgTemplateResult'); // Here I add a value to a new field I made
    $cart->update();
}

// Update the shopping cart
$cart->updateQty(1, $this->getProductId(), $id_product_attribute = null, $id_customization = false, $operator = 'up', $id_address_delivery = 0, $shop = null, $auto_add_cart_rule = true);

867377593_Capturedecran2022-10-04a11_01_37.png.7a8d2ee6b7e71c7f74c1b71b575ef1bb.png

If there is no Cart in the context, I need to create it like I saw somewhere :

if ($cart->id == null){
    $cart = new Cart();
    $cart->id_customer = (int)($this->context->cookie->id_customer);
    $cart->id_address_delivery = (int)  (Address::getFirstCustomerAddressId($cart->id_customer));
    $cart->id_address_invoice = $cart->id_address_delivery;
    $cart->id_lang = (int)($this->context->cookie->id_lang);
    $cart->id_currency = (int)($this->context->cookie->id_currency);
    $cart->id_carrier = 1;
    $cart->recyclable = 0;
    $cart->gift = 0;
    $cart->add();
    $cart->my_custom_field = Tools::getValue('svgTemplateResult'); // Here I add a value to a new field I made
    $cart->update();
    $this->context->cookie->id_cart = (int)($cart->id);  
}

// Update the shopping cart
$cart->updateQty(1, $this->getProductId(), $id_product_attribute = null, $id_customization = false, $operator = 'up', $id_address_delivery = 0, $shop = null, $auto_add_cart_rule = true);

But the "1" does not appears instantly next to the shopping cart icon. I need to refresh the page to see it appear.

If I don't refresh the page but I click another time on "Add to shopping cart" button, it instantly refresh the number (because the Cart is in the context so we go first option) and I have 2 times the product in the shopping cart.

What can I do with this ?

Thanks for reading !

Edited by Florian644 (see edit history)
Link to comment
Share on other sites

PHP won't refresh the page in the background, so this won't work.
Use your own JavaScript to update your cart.

TPL:

<div class="btn" name="my_cart_button" id="my_cart_button" data-idProduct="{$product.id}" data-idAttribute="{$product.id_attribute}">Add to cart</div>

and

JavaScript (JavaScript cannot be inserted into the forum😞

obrazek.thumb.png.733ae5771d8208bb598a4d5c0ba4be7a.png

You can also add an ajax function to create a cart if it doesn't exist.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...