Jump to content

[PS - 1.7] - Generate Products Packs In PHP


Recommended Posts

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 by jmauclair
code format (see edit history)
  • Thanks 1
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...