prestashop:使用自定义字段将产品添加到购物车

时间:2015-05-15 12:42:46

标签: php module prestashop cart

美好的一天。

我正在为prestashop开发一个模块,用户可以在其中选择一些设置,然后就可以保存这些设置并将自定义产品添加到购物车,将它们用作自定义字段的值。

我添加了一个新产品,包含7个自定义文本字段。

然后我在模块的PHP中编写了这个,在将该产品添加到购物车中的函数中:

            $debug.=" aggiungerei";
            $fieldVal1= "30";
            $fieldVal2= "30";
            $fieldVal3= "10";
            $fieldVal4= "90";
            $fieldVal5= "10";
            $fieldVal6= "MyTitle";
            $fieldVal7= "MyText";

            // get cart id if exists
            if ($this->context->cookie->id_cart)
            {
                $cart = new Cart($this->context->cookie->id_cart);
                $debug.=" 1.nuovo carrello";
            }
            else
            {
                if (Context::getContext()->cookie->id_guest)
                {
                    $guest = new Guest(Context::getContext()->cookie->id_guest);
                    $this->context->cart->mobile_theme = $guest->mobile_theme;
                    $debug.=" 2.carrello";
                }
                $this->context->cart->add();
                if ($this->context->cart->id)
                {
                    $this->context->cookie->id_cart = (int)$this->context->cart->id;
                    $debug.=" 3.carrello";
                }
            }

            //ADD COSUTM FIELDS VALUES [TODO]


            // add product to cart
            $cart->updateQty(1, 39, null, (int)($customization), Tools::getValue('op', 'up'));
            $prods = $cart->getProducts(true);
            print_r ($prods[0]['id_customization']);
            // update cart
            $cart->update();

在示例中我有自定义字段的静态值,在实际模块中,这些值是从数据库中获取的。

我无法弄清楚如何对购物车说产品的自定义字段必须填充脚本开头的值...是否有人能引导我朝着正确的方向前进?

我从这个例子开始,在这里成立: Prestashop: add customized product to cart

1 个答案:

答案 0 :(得分:1)

实际工作的代码是:

  $this->product = new Product(39, true, (int)($this->context->cookie->id_lang));



        if (!$field_ids = $this->product->getCustomizationFieldIds())
            return false;

            $this->context->cart->addTextFieldToProduct($this->product->id, 7, Product::CUSTOMIZE_TEXTFIELD, $titolo);
            $this->context->cart->addTextFieldToProduct($this->product->id, 8, Product::CUSTOMIZE_TEXTFIELD, $testo);
            $this->context->cart->addTextFieldToProduct($this->product->id, 9, Product::CUSTOMIZE_TEXTFIELD, $miscela1);
            $this->context->cart->addTextFieldToProduct($this->product->id, 10, Product::CUSTOMIZE_TEXTFIELD,$miscela2);
            $this->context->cart->addTextFieldToProduct($this->product->id, 11, Product::CUSTOMIZE_TEXTFIELD, $miscela3);
            $this->context->cart->addTextFieldToProduct($this->product->id, 12, Product::CUSTOMIZE_TEXTFIELD, $miscela4);
            $this->context->cart->addTextFieldToProduct($this->product->id, 13, Product::CUSTOMIZE_TEXTFIELD, $miscela5);

为了我的幸运,我已经知道自定义字段的ID(我已经查看了数据库上的自定义字段表)。