Florian644 Posted October 4, 2022 Share Posted October 4, 2022 (edited) 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 ! Edited October 4, 2022 by Florian644 (see edit history) Link to comment Share on other sites More sharing options...
Florian644 Posted October 6, 2022 Author Share Posted October 6, 2022 I send back this topic to the top 🙏 Link to comment Share on other sites More sharing options...
ps8modules Posted October 9, 2022 Share Posted October 9, 2022 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😞 You can also add an ajax function to create a cart if it doesn't exist. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now