如何在Prestashop 1.6中创建对象购物车

时间:2019-11-02 21:39:18

标签: prestashop prestashop-1.6

我在Prestashop 1.6中创建购物车对象时遇到问题。我有一个错误:

2019/11/02 - 22:02:40: Property Cart->id_currency is empty at line 917 in file classes/ObjectModel.php

在调试模式下,我没有看到购物车对象的ID,这是我认为的问题。这是我的代码:

if($id_product && $checkQty >= $quantity)
    {
        if (!$this->context->cart->id)
        {
            $this->context->cart->add();
            $cart = new Cart();
            if ($this->context->cart->id)
                $this->context->cookie->id_cart = (int)$this->context->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'
        ));        
    }

当然,当我有$ cart-> id时,updateQty效果很好。感谢您的帮助。

亲切的问候

1 个答案:

答案 0 :(得分:0)

您需要定义货币的ID。

使用该代码,您可以根据上下文中定义的货币在购物车中分配货币:

$cart->id_currency = $context->cookie->id_currency;

所以在您的代码中它必须看起来像这样:

if($id_product && $checkQty >= $quantity)
    {
        if (!$this->context->cart->id)
        {
            $this->context->cart->add();
            $cart = new Cart();
            if ($this->context->cart->id)
                $this->context->cookie->id_cart = (int)$this->context->cart->id;
        }else
        {
            $cart = new Cart($this->context->cookie->id_cart);
        }  

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

        $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'
        ));        
    }