I am writing my own controller for Prestashop. This is done to accomplish a simple task: 1. Create a new cart if it has not been created (works fine) 2. Get the attribute identifier from the database (works fine) 3. Assign a setting (one text box) 4. Add this product to the cart .
My current code is:
$idProduct = 1;
$qty= 1;
$text = (string)Tools::getValue('textField9');
$attribute = (int)Tools::getValue('sel_combination');
if ($this->context->cookie->id_cart)
{
$cart = new Cart($this->context->cookie->id_cart);
}
if (!isset($cart) OR !$cart->id)
{
$cart = new Cart();
$cart->id_customer = (int)($this->context->cookie->id_customer);
$cart->id_address_delivery = (int) (Address::getFirstCustomerAddressId($cart->id_customer));
$cart->id_address_invoice = $cart->id_address_delivery;
$cart->id_lang = (int)($this->context->cookie->id_lang);
$cart->id_currency = (int)($this->context->cookie->id_currency);
$cart->id_carrier = 1;
$cart->recyclable = 0;
$cart->gift = 0;
$cart->add();
$this->context->cookie->id_cart = (int)($cart->id);
$cart->update();
}
$productToAdd = new Product(1, true, (int)($this->context->cookie->id_lang));
$attributes = $attribute;
$cart->update();
$customization = $this->context->cart->addTextFieldToProduct((int)($idProduct), 9, Product::CUSTOMIZE_TEXTFIELD, $text );
$exising_customization = Db::getInstance()->executeS('SELECT id_customization FROM '._DB_PREFIX_.'customized_data ORDER BY id_customization DESC LIMIT 0,1');
$customization = $exising_customization[0]['id_customization'];
Db::getInstance()->execute('UPDATE ps_customization SET in_cart = 1, id_product_attribute = '.$attributes.' WHERE id_customization = ' .$customization);
$cart->updateQty($qty, (int)($idProduct), (int)($attributes), (int)($customization), Tools::getValue('op', 'up'));
$prods = $cart->getProducts(true);
print_r ($prods[0]['id_customization']);
$cart->update();
A cart has been created and a product has been added. The cart says that there is one product, but it is not displayed. After rebooting the above code, the basket shows 2 items with the corresponding settings.
, , . ($ cart- > getProducts (true), ['id_customization'] ), . .