1

Good day all.

I'm developing a module for prestashop, in which the user choose some settings, and then it is possible to save these settings and add a custom product to cart, using them as values for custom fields.

I've added a new product, with 7 custom text fields.

then I've write this in the PHP of the module, in the function that add the product in the cart:

            $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();

in the example I have static values for the custom fields, in the real module, these values are taken from the database.

I can't figure out how to say to the cart that the product must have its custom fields populated with the values at the beginning of the script... does anyone can lead me in the right direction?

I've started from this example, founded here: Prestashop: add customized product to cart

Community
  • 1
  • 1
Matteo Bononi 'peorthyr'
  • 2,170
  • 8
  • 46
  • 95

1 Answers1

2

The code that actually work is this:

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

for my luckyness, I already know the IDs of the custom fields (I've taken a look at the custom fields table on the db).

Matteo Bononi 'peorthyr'
  • 2,170
  • 8
  • 46
  • 95