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);
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 !