Jump to content

How to create an object cart in Prestashop 1.6


Recommended Posts

I have problem with create a cart object in Prestashop 1.6. No error but my cart is empty and I don't know why.
 

if (!$this->context->cart->id)
    {
        $cart = new Cart();
        $cart->id_customer = (int)($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)($cookie->id_lang);
        $cart->id_currency = (int)($cookie->id_currency);
        $cart->id_carrier = 1;
        $cart->recyclable = 0;
        $cart->gift = 0;
        $cart->add();
        $cookie->id_cart = (int)($cart->id);
    }else
    {
        $cart = new Cart($this->context->cookie->id_cart);
    }  

    $cart->updateQty(
        $quantity,
        $id_product,
        $id_product_attribute = null,
        $id_customization = false,
        $operator = 'up',
        $id_address_delivery = 0,
        $shop = null,
        $auto_add_cart_rule = true
        );
    $this->context->smarty->assign(array(
        'confirmation' => '1'
    ));  
    
}

Where is the problem ?

Link to comment
Share on other sites

  • 1 month later...

Try to assign new cart object to `context` after create.

$this->context->cart = $cart;

This solved my problem

$cart = $this->context->cart;
if (!$this->context->cart->id){
  $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();
  $this->context->cookie->id_cart = (int)($cart->id);
  $this->context->cart = $cart;
}

 

Edited by skrittar (see edit history)
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...