简体   繁体   中英

How to add Product in Cart in Prestashop 1.6.7

I am trying to add product in Cart in Prestashop 1.6.7.

But it doesn't working.

Below is my code:

$context=Context::getContext();//new Cart();

$id_cart=$context->cookie->__get('id_cart');
$lan_id = $this->context->language->id;
$cur_id = $this->context->currency->id;     

$products_ids=16571;
$cart=new Cart($id_cart);
$cart->id_currency=$cur_id;
$cart->id_lang=$lan_id;
$cart->updateQty(1,$products_ids, null, false);

But above code is not working.

Somehow i manage my code as per below:

global $context;

$context=Context::getContext();//new Cart();

$id_cart=$context->cookie->__get('id_cart');
$lan_id = $context->cookie->__get('id_lang');
$cur_id = $context->cookie->__get('id_currency');   


$products_ids=16571;
$cart=new Cart($id_cart);
$cart->id_currency=$cur_id;
$cart->id_lang=$lan_id;
$this->context->cart->update();
$cart->updateQty(1,16571);

But there is one error for carrier

There are no carriers available that deliver to this address.

Try adding the following code at the end of your code.

        $this->context->cart->save();
        $this->context->cookie->id_cart = (int)$this->context->cart->id;
        $this->context->cookie->write();
        $this->context->cart->autosetProductAddress();
        Hook::exec('actionAuthentication');
        CartRule::autoRemoveFromCart($this->context);
        CartRule::autoAddToCart($this->context);

I add some static Carrier ID in

classes/Cart.php

in getPackageList function at line no. approx. 1892

if (empty($product['carrier_list'])) {
    $product['carrier_list'] = array(
                                '0' => 10,
                                '1' => 11
                               );
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM