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

prestashop: adding a product to cart, with custom fields

大家好。

我正在为 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

实际工作的代码是这样的:

  $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(我查看了数据库上的自定义字段 table)。