Question

I'm creating a new module for Prestashop where users can design their own product from a third party service. When the user later adds the product to their cart, I would like to save a ID that I get from this Third Party Service where the user designed their product.

I guess the best way to do this is to create a Customization Textfield within Prestashop called "designID". Now I want to know how I save data to this field from module development in Prestashop instead of letting the users manually fill in the data.

So basically... How do I add data to these customizable fields from within a Prestashop module, when the user adds the product to their cart?

Was it helpful?

Solution

These customization fields are used if you are simple user and you are not designing a module. Using it to save the designID will be just a hack.

Since you're creating a new module my advice is to keep the 3rd party ID in a newly created database table, which will match the id_product, id_design, id_cart, id_order, etc...

You can hook to "actionCartSave" and add the record with the matching ids, and all the other required data at your table.

If you want to stick to that Customization feature, add a sample field and review the following database tables:

ps_customization ps_customization_field ps_customization_field_lang ps_customized_data

and replicate the changes when you receive the 3rd party ID.

If all your products will be customized, consider adding the required data in ps_customization_field & ps_customization_field_lang (the table for the field structure) during the module installation, so after it's installed you can just fill ps_customization & ps_customized_data (the tables for the field data)

PrestaShop does not have proper API for adding customizations, only for retrieving data, so you'll have to write the SQL queries yourself. Just review the ProductController for the ps_customization & ps_customized_data changes and the AdminProductsController for ps_customization_field & ps_customization_field_lang.

Do not forget to remove the Customization markup code from your product & cart templates.

OTHER TIPS

I was able to find this out by myself by first trying to use Customization for a couple of hours without any success.

So basically how my module work is that the customer can open a popup iframe to a third party design tool, the customer then save the design in the iframe, which then sends the data to the parent window (The Prestashop Window).

So to store this I did the following:

  • Add a new column to the database table ps_cart_product
  • Hook into any display-hook on the product page and check if any post data is send containing the data from the third party module. If so, then:

    if(isset($_POST['thirdparty'])){
            $id_product = (int)Tools::getValue('id_product');
    
            if (!$this->context->cart->id){
                $this->context->cart->add();
                if ($this->context->cart->id)
                    $this->context->cookie->id_cart = (int)$this->context->cart->id;
            }
    
            $this->context->cart->updateQty(1, $id_product);
    
            if(!Db::getInstance()->update('cart_product', array('id_design'=> pSQL(trim($_POST['thirdparty']))) ,'id_cart = '.$this->context->cart->id.' AND id_product = '.(int)Tools::getValue('id_product') ))
            $this->context->controller->_errors[] = Tools::displayError('Error: ').mysql_error();
        }
    

    So basically first I check if POST is set, then I check if any cart exist, if cart does not exist then add a new cart with ->add() (This function took hours to find, 0 documentation). updateQty() is used to update the cart with the new product.

    The last part is the SQL query that updates the value of id_design column with the data that is send from the third party.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top