skip to Main Content

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

enter image description here

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 ?

2

Answers


  1. Chosen as BEST ANSWER

    Problem solved, I simply had to create the cart in the context (if there wasn't one already) BEFORE clicking on "Add to shopping cart", so in the public function initContent() of the front controller of the module for example.


  2. You’ll need to refresh the cart icon with a JS AJAX call,
    just check / reproduce in your module what is normally happening when you click the ‘ADD TO CART’ button from a product page.

    You’ll see an ajax call to the ps_shoppingcart front controller:

    /module/ps_shoppingcart/ajax
    

    Payload is pretty basic :

    id_customization: 0
    id_product: {id_product}
    id_product_attribute: {id_product_attribute}
    action: add-to-cart
    

    Otherwise with "pure" PHP your only choice is to refresh the page after every cart modification by using Tools::redirect()

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search