Creating Magento Products On-The-Fly!

Sometimes it’s necessary to create products on-the-fly, typically in situations where there are so many product possibilities that even a configurable product isn’t flexible enough! In such situations the need arises to create products dynamically.

Below is the PHP code to produce a single Magento product and add it to the cart.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
 <?php
require_once 'app/Mage.php';
Mage::app();
 
// instatiate Product
$product = Mage::getModel('catalog/product');
 
$product->setWebsiteIds(array(1));
$product->setSku('rand-sku-' . rand());
$product->setPrice(rand(100,2000));
$product->setAttributeSetId(4); 
$product->setCategoryIds(array(3));
$product->setType('Simple Product');
$product->setName('Product Name'.rand(1,200000));
$product->setDescription('The Product Description');
$product->setShortDescription('Brief Description');
$product->setStatus(1);	
$product->setTaxClassId('2');
$product->setWeight(0);				
$product->setCreatedAt(strtotime('now'));
 
/* ADDITIONAL OPTIONS 
 
   $product->setCost();
   $product->setInDepth();
   $product->setKeywords();
 
*/
 
$product->save();
 
// "Stock Item" still required regardless of whether inventory
// control is used, or stock item error given at checkout!
 
$stockItem = Mage::getModel('cataloginventory/stock_item');
$stockItem->loadByProduct($product->getId());
$stockItem->setData('is_in_stock', 1);
$stockItem->save();
 
header("Location: /checkout/cart/add/product/".$product->getId()."/); 
 
?>

This will automatically create a Magento product, with attributes and a price, add it to a number of categories, save the product, add the product to the cart and redirect the user to the Magento cart.

In a future post I will explore how to create multiple products using a set of classes and add all the products to the cart simultaneously.

11 Responses to “Creating Magento Products On-The-Fly!”

Comments

  1. sei_  on March 24th, 2009

    Woah very nice!
    Can I include custom attributes as well to the list?

    Such as:
    SetTint -> Brown ?

  2. sei_  on March 24th, 2009

    Of course these attributes would already have been created in the backend, Im just curious as to would I be able to reference these custom attributes in that manner?

  3. Darryl Adie  on March 24th, 2009

    That’s an interesting thought, I would think the answer is yes, assuming of course that you have selected the correct attribute set with:

    $product->setAttributeSetId();

    But would be interested to get some feedback on that! Thanks for your comment!

  4. flyhigh  on April 30th, 2009

    Which file defines all those “SetXXXX” methods you’re using to build the product? I understand they’re dynamically created, but to what extent can they be used? I want to figure out if there’s a way to set product customizations - specifically a dropdown and text field. Do you know how to do this using this method? Totally - unrelated, but what about product images too?

    $product->setCreatedAt(strtotime(’now’));

  5. Darryl Adie  on April 30th, 2009

    The file you’re looking for is /app/code/core/Mage/Catalog/Model/Product.php.
    Notice that the command Mage::getModel('catalog/product') calls the Model for product.php in Model folder within Catalog.

    In the same way that Mage::getModel('cataloginventory/stock_item') loads /app/code/core/Mage/CatalogInventory/Model/Stock/Item.php
    Hope this helps, this will be covered in detail in my book (when it’s ready!)

  6. Hugo  on May 25th, 2009

    Hi, thank you very much! this is great!, Now I’m wondering how to set the names of the product images so after runing the script and copying the image files to the correct folder, these images will be visible in the catalog.

  7. cvinson86  on June 1st, 2009

    When I try to implement this, I got an ‘Integrity Constraint Violation’, which I fixed by removing the Foreign Key Constraints on the cataloginventory_stock_item, but now when the item is added to the shopping cart, I get a “Stock item for Product is not valid” error. Has anyone else encountered this problem, and how might I go about troubleshooting it.
    Thanks,
    -Colin

  8. Darryl Adie  on June 2nd, 2009

    Colin, firstly you will need that foreign key constraint, so I would restore that! Otherwise, Magento will think that there isn’t any stock available for the customer to purchase and they will receive that error.

    After line 36, try print_r($stockItem) and see what information you get, this will give you an idea of whether you have instantiated a stock item and whether there is another underlying issue.

    What version of Magento are you doing this on?

    Darryl :)

  9. MichaelBai  on November 6th, 2009

    Hi,Darryl, now do you know how to “include custom attributes as well to the list” as 1st comment says?

  10. tmillhouse  on November 12th, 2009

    @cvinson86:

    I had the same issue as you with the code described above. The following is my stock_item creation/update code that seemed to fix the issue. I still have yet to test the validity of the created records, but this at least allows me to save the stock item:

    // update the stock item associated with the product
    $stockItem = Mage::getModel(’cataloginventory/stock_item’);
    $stockItem->assignProduct($product);
    $stockItem->setData(’is_in_stock’, 1);
    $stockItem->setData(’stock_id’, 1);
    $stockItem->setData(’store_id’, 1);
    $stockItem->setData(’manage_stock’, 0);
    $stockItem->setData(’use_config_manage_stock’, 0);
    $stockItem->setData(’min_sale_qty’, 0);
    $stockItem->setData(’use_config_min_sale_qty’, 0);
    $stockItem->setData(’max_sale_qty’, 1000);
    $stockItem->setData(’use_config_max_sale_qty’, 0);

    $stockItem->save();

  11. Keith Greer  on January 19th, 2010

    Thank you Darryl, this is fantastic. Great work!

Trackbacks/Pingbacks

Leave a Reply