简体   繁体   English

如何在Prestashop 1.6.7中的购物车中添加产品

[英]How to add Product in Cart in Prestashop 1.6.7

I am trying to add product in Cart in Prestashop 1.6.7. 我试图在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 我在其中添加了一些静态运营商ID

classes/Cart.php

in getPackageList function at line no. getPackageList函数中的行号。 approx. 大约 1892 1892年

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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