Nietypowy Informatyk Posted February 10, 2023 Share Posted February 10, 2023 (edited) Hi! I am developing a module, where user can configure certain parameters of the product - such as color, size, etc. The only thing I can't get to work is the fact, that entries in ps_cart_product are being stored with product_id as a primary key. And I need to allow users to add same product twice to the cart (with different color for example) - is there a way to do it? I was thinking about generating id_product with something like UUID, but then I would probably break a couple of things Any help would be appreciated! Kind regards, Bartek Edited February 10, 2023 by Nietypowy Informatyk (see edit history) Link to comment Share on other sites More sharing options...
ps8modules Posted February 10, 2023 Share Posted February 10, 2023 Hi. This is done by creating your own fields and values for customization on the fly. Look at the tables in the database: ps_customization, ps_customization_field, prs_customization_field_lang, ps_customized_data. Also check the ./classes/Customization.php and ./classes/CustomizationField.php files. Link to comment Share on other sites More sharing options...
Nietypowy Informatyk Posted February 10, 2023 Author Share Posted February 10, 2023 Hi, thanks for fast reply! Ok, but I have customization stored in my own tables (containing price, and some other values), should I duplicate them? Link to comment Share on other sites More sharing options...
ps8modules Posted February 10, 2023 Share Posted February 10, 2023 Hello, so you will have to reprogram the entire ordering process. If you use what is already built into Prestashop, you will avoid problems. If you have some preset values in your tables, nothing prevents you from filling the customization tables according to anything. Link to comment Share on other sites More sharing options...
Nietypowy Informatyk Posted February 10, 2023 Author Share Posted February 10, 2023 (edited) But what if I have some parameter types, that are not supported by prestashop customization mechanism? Or can I simply store my settings as JSON in PrestaShop customization tables? In order not to create multiple customization entries. Edited February 10, 2023 by Nietypowy Informatyk (see edit history) Link to comment Share on other sites More sharing options...
ps8modules Posted February 10, 2023 Share Posted February 10, 2023 (edited) 1. it is necessary to show the customer what he has selected and saved 2. you need to show JSON in the administration or in the email In point 1. you can save the text or image, what the customer will see. In point 2. you create your own table in which you write your JSON and id_customization. It's hard to give advice like this when you don't know all the detailed information. Edited February 10, 2023 by ps8moduly.cz (see edit history) Link to comment Share on other sites More sharing options...
ps8modules Posted February 10, 2023 Share Posted February 10, 2023 I don't understand the part, add a duplicate ! What do you mean please? You can add the product to the cart up to 10 times with other customizations. Link to comment Share on other sites More sharing options...
Nietypowy Informatyk Posted February 10, 2023 Author Share Posted February 10, 2023 Ok, so let me give some more details, about whole module:Backoffice part: Administrator of the shop can configure each product with following data: - available amounts - parameters - different types: checkbox, color, single-pick and select - discounts - and some other stuff All backoffice configuration is taking place in a Vue app, that makes REST calls to exposed endpoints. All data is stored in appropriate custom tables.Front part: User can configure all parameters, amounts etc. and add to cart. This is the hardest part for me, but with some overrides and time I have managed to get it working with custom cart controller, which calculates price, and adds to the cart - so far so good. I have also managed to show information about parameters in admin view, and invoice - using following hooks: - displayAdminOrderTabContent - displayPDFInvoice By "add a duplicate" I meant duplication of data in database - in my tables responsible for storing data about price, parameters etc. and the native customization tables which you have mentioned. Hope it helps! Link to comment Share on other sites More sharing options...
ps8modules Posted February 10, 2023 Share Posted February 10, 2023 Sample add customization to databse with id cart and customization price (the whole cart is not built in): public function addMyProductCustomization($product, $customizedSumPrice, $customizedWeight) { $db = Db::getInstance(); $custimzedFieldName = 'Customization'; $customizedValue = 'product customized'; $languages = Language::getLanguages(true); $idShop = $this->context->shop->id; $moduleName = 'myModule'; $idModule = $db->getValue("SELECT id_module FROM "._DB_PREFIX_."module WHERE name = '".$moduleName."'"); if (!$customized_datas) { return; } $idProduct = $product->id; $idProductAttribute = $product->id_product_attribute; /* CART */ if ($this->context->cookie->id_cart){ $cart = new Cart($this->context->cookie->id_cart); } else { $cart = new Cart(); } /* PRODUCT CAN BE ADDED TO CART HERE */ $idCart = $cart->id; /* CUSTOMIZATION */ $db->insert('customization', [ 'id_product_attribute' => $idProductAttribute, 'id_cart' => $idCart, 'id_product' => $idProduct, 'quantity' => $customize['quantity'], 'in_cart' => 1 ] ); $getInsertedCustomizationId = $db->Insert_ID(); /* CUSTOMIZATION FIELD */ $db->insert('customization_field', [ 'id_product' => $idProduct, 'type' => '1', /* text field */ 'required' => 1, 'is_module' => 1, 'is_deleted' => 0 ] ); $getInsertedCustomizationFieldId = $db->Insert_ID(); /* CUSTOMIZATION FIELD LANG */ foreach ($languages as $lang){ $db->insert('customization_field_lang', [ 'id_customization_field' => $getInsertedCustomizationFieldId , 'id_lang' => $lang['id_lang'], 'id_shop' => $idShop, 'name' => 1, ] ); } /* CUSTOMIZED DATA */ $getMaxIndex = $db->getValue('SELECT MAX(index) FROM '._DB_PREFIX_.'customized_data') + 1; $db->insert('customized_data', [ 'id_customization' => $getInsertedCustomizationId, 'type' => '1', /* text field */ 'index' => $getMaxIndex, 'value' => $customizedValue, 'id_module' => $idModule, 'price' => $customizedSumPrice, 'weight' => $customizedWeight ] ); } 1 Link to comment Share on other sites More sharing options...
Nietypowy Informatyk Posted February 10, 2023 Author Share Posted February 10, 2023 (edited) Thanks for the sample code! Shouldn't products be added to cart after creating customization? I thought that I will need to pass customization_id into updateQty cart method. Edited February 10, 2023 by Nietypowy Informatyk (see edit history) Link to comment Share on other sites More sharing options...
ps8modules Posted February 10, 2023 Share Posted February 10, 2023 (edited) You don't have to. Just create one function and that's it. 😉 You can simply hide the default cart button and add your own to your module on the product page. You can even use AJAX add to cart. Edited February 10, 2023 by ps8moduly.cz (see edit history) Link to comment Share on other sites More sharing options...
Nietypowy Informatyk Posted February 10, 2023 Author Share Posted February 10, 2023 Ok, I will give it a go! For now thank you from the bottom of my heart! 1 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