首页 > 解决方案 > 如何在 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 效果很好。谢谢你的帮助。

亲切的问候

标签: prestashopprestashop-1.6

解决方案


您需要定义货币的 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'
        ));        
    }

推荐阅读