jmauclair Posted July 7, 2021 Share Posted July 7, 2021 (edited) Hi all, I think it's cool to share this with you guys because I've been struggling to find an efficient way to generate products Packs in my custom module. function generateProductPack($ean13, $ref, $name, $text, $price, $catDef, $packedProductID) { $product = new Product(); // Create new product in prestashop $product->ean13 = $ean13; $product->reference = $ref; $product->name = createMultiLangField($name); //$product->id_tax_rules_group=0; //You can set the tax rules group id if you want but it's set by default if you don't $product->description = $text; // You can sanitize by using php functions, in my case, I decided to don't $product->id_category_default = $catDef; $product->redirect_type = '301'; $product->price = number_format($price, 6, '.', ''); $product->minimal_quantity = 1; $product->show_price = 1; $product->on_sale = 0; $product->online_only = 0; $product->meta_description = ''; $product->link_rewrite = createMultiLangField(Tools::str2url($name)); // Contribution credits: mfdenis $product->save(); Pack::AddItem($product->id, $packedProductID, 1 ); // 1 is the number of units of the products to add in your pack StockAvailable::setQuantity($product->id, null, StockAvailable::getQuantityAvailableByProduct($packedProductID)); // It will initiate your product pack with the quantity of products avalaible divided by 1 $product->addToCategories($catDef); // You can associate your product pack with different categories, I choose to only associate it to the default one } This code has been mostly took from Crezzur : Feel free to give optimisations or any comment, I can be wrong, I'm not a pro prestashop developer 😉 Edited July 7, 2021 by jmauclair code format (see edit history) 1 Link to comment Share on other sites More sharing options...
delete-account-pleas Posted July 8, 2021 Share Posted July 8, 2021 Well done! Glad to see that you share your code with the community 😉 I have added your topic to my post 👍 1 Link to comment Share on other sites More sharing options...
jmauclair Posted July 8, 2021 Author Share Posted July 8, 2021 Thank you for sharing too ! 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