Serial Posted February 28, 2018 Share Posted February 28, 2018 (edited) Hi, In my module, I want to add a product in cart programmatically but with a customization text. if (Tools::isSubmit('submit')) { // Add product in cart with customization text } Do you know how to do this ? Edited February 28, 2018 by Serial (see edit history) Link to comment Share on other sites More sharing options...
Serial Posted February 28, 2018 Author Share Posted February 28, 2018 (edited) I add product in cart successfull with this : if (Tools::isSubmit('submit_confection')) { $id_prod = Tools::getValue('produit_id'); $ipa = Tools::getValue('ipa'); $customData = "My customize text"; $qty = 1; if (is_null($this->context->cart->id)) { $this->context->cart->add(); $this->context->cookie->__set('id_cart', $this->context->cart->id); } // get cart id if exists if ($this->context->cookie->id_cart) { $cart = new Cart($this->context->cookie->id_cart); } // create new cart if needed if (!isset($cart) OR !$cart->id) { $cart = new Cart($this->context->cookie->id_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); } $product = new Product((int)$id_prod); if ($ipa > 0) { $minimal_quantity = (int)Attribute::getAttributeMinimalQty($this->id_product_attribute); } else { $minimal_quantity = (int)$product->minimal_quantity; } // add customizatated text $check =serialize($customData); $id_address_delivery = $cart->id_address_delivery; $customization = $this->context->cart->addTextFieldToProduct((int)($id_prod), 9, Product::CUSTOMIZE_TEXTFIELD, serialize($customData)); $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 = '.$ipa.',id_address_delivery='.$id_address_delivery.',quantity='.$minimal_quantity.' WHERE id_customization = ' .$customization); // get product to add into cart $productToAdd = new Product((int)($id_prod), true, (int)($this->context->cookie->id_lang)); $cart->update(); $this->context->cart->updateQty((int)($qty),(int)($id_prod),(int)($ipa),$customization,'up',(int)($id_address_delivery)); $cart->update(); } But it adds me 4 products in the cart :/ And now, when I confirm order, I see customization text in database (ps_customized_data) but I didn't see it in order detail in back-office. Do you know what ? Edited February 28, 2018 by Serial (see edit history) Link to comment Share on other sites More sharing options...
LauraPresta Posted June 7, 2018 Share Posted June 7, 2018 any new thing on this ? Link to comment Share on other sites More sharing options...
El Patron Posted June 7, 2018 Share Posted June 7, 2018 why are you not just using native custom fields? they do display poorly but that is easily fixed... Link to comment Share on other sites More sharing options...
LauraPresta Posted June 8, 2018 Share Posted June 8, 2018 Hello El Patron, nice to see you again dear signal processing mate i think addTextFieldToProduct works with customfield already or i am wrong ? for this function to work it seems to need at least one customfield set in product configuration in back office. I have a question for someone like you... for this function addTextFieldToProduct... : this function add text field to a product, it can be called even before the product goes into cart... so why the duck it is in class cart ? cart->addTextFieldToProduct and i add here the solution of people's question of this topic : Found here : https://www.prestashop.com/forums/topic/668987-ajout-produit-au-panier-avec-texte-de-personnalisation/#comment-2888076 Concernant le problème de quantité : La méthode updateQty() appel à son tour la méthode _updateCustomizationQuantity() qui s'en charge de modifier la quantité dans la table ps_customization (augmente ou diminue). Avant d’appeler updateQty(), la customization doit être ajouté avec 'quantity' = 0 et 'in_cart' = 0 (la méthode _updateCustomizationQuantity() s'en chargera de mettre à jour ces deux paramètres par la suite) $data = array( 'id_product_attribute' => $line['id_product_attribute'], 'id_address_delivery' => (int)$this->context->cart->id_address_delivery, 'id_cart' => (int)$this->context->cart->id, 'id_product' => (int)$line['article_id'], 'quantity' => 0, 'in_cart' => 0 ); Db::getInstance()->insert('customization', $data); $this->context->cart->updateQty((int)$line['quantity'], $line['article_id'], $line['id_product_attribute'], $id_customization); Link to comment Share on other sites More sharing options...
LauraPresta Posted June 16, 2018 Share Posted June 16, 2018 On 08/06/2018 at 12:14 AM, El Patron said: why are you not just using native custom fields? they do display poorly but that is easily fixed... Hello El Patron, What is the way to figure out which is the file to override so i can improve display of custom text fields ? Because you are right they display pretty poorly in cart summary for exemple : Text 1 : my option one text 2 : my option 2 So at least i need to remove "text number :" but with override. Thank in advance if you can help Link to comment Share on other sites More sharing options...
LauraPresta Posted June 16, 2018 Share Posted June 16, 2018 ok here is what i found : override is not so much a good idea and i checked that there is possible to add a textname of the custom field (im not talking about the value) - ps_customization_field_lang unfortunately method allow only to set value : public function addTextFieldToProduct($id_product, $index, $type, $text_value) Moreover the name of the field will be same for all other product using it, so my solution was to create new text field each time needed and then set the name in DB ps_customization_field_lang Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now