2

In the following code I have written to create a single simple product at a time, but I want to make a bulk creation of products at a time, can anyone help me in doing this?

Mage::app('default')->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
        $product = Mage::getModel('catalog/product');
        $product->setAttributeSetId($attributeSetId);//ID of a attribute set named 'default'
        $product->setTypeId('simple');  //product type
        $product->setWebsiteIds(array(1));
        $product->setSku($itemjson->getSku($i)); //SKU
        $product->setName($itemjson->getName($i)); //product name
        $product->setWeight($itemjson->getWeight($i));
        $product->setUpdatedAt(strtotime('now'));
        $product->setStatus(1);//product status (1 - enabled, 2 - disabled)
        $product->setTaxClassId(4); //tax class (0 - none, 1 - default, 2 - taxable, 4 - shipping)
        $product->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH); //catalog and search visibility
        $product->setPrice($price); //price in form 11.22
        $product->setDescription($itemjson->getDesc($i));
        $product->setShortDescription($itemjson->getShortDesc($i)) ;
        $product->setStockData(array(
               'use_config_manage_stock' => 0, //'Use config settings' checkbox
               'manage_stock'=>1, //manage stock
               'min_sale_qty'=>1, //Minimum Qty Allowed in Shopping Cart
               'max_sale_qty'=>1000, //Maximum Qty Allowed in Shopping Cart
               'is_in_stock' => 1, //Stock Availability
               'qty' => $itemjson->getStock($i), //qty
                )
            );
        $catArray=$itemjson->getSimpleCat($i);
        $catKeys=array_keys($catArray);
        $product = self::manageAttr($product,$catArray);
        $product->setCategoryIds($categories);
        $product->save();
        
3
  • why are you not using the loop here?
    – Saurabh
    May 20, 2015 at 18:06
  • 1
    There is no builk product creation. Use a loop. If you want to buil-update certain attributes, that is possible: stackoverflow.com/questions/10355652/…
    – laketuna
    May 20, 2015 at 19:36
  • Yes I tried looping here, but performance getting badly hit, If I loop around it, Only one item per second is getting saved, can anyone atleast help me how increase the performance May 21, 2015 at 4:30

3 Answers 3

1

maybe this instruction helps you.

$product->setIsMassupdate(true)->setExcludeUrlRewrite(true);

This may say Magento that is not necessary reindex tables on each product save.

1

I think a real good way to go if you want quick bulk-inserting products is this free plugin ApiImport:

It requires some programming skills, but in the end you'll be rocketing those product into your DB.

Combine with this URL INDEX MODULE from EcomDev (Also compatible with 1.7 - 1.9.2+) and reindexing is much quicker after an import.

Easy does it! ;)

This is how your code would look like:

require_once 'app/Mage.php';
Mage::init('admin');

$out = array();

$attributes = array(
    'sku',
    'weight',
    'website_ids',
    'status',
    'name',
    'desc' => 'description',
    'stock' => 'qty'
    //...
); // all the items you want to update

for($x->whatever_place_you_get_your_data_from() as $i)
{
    echo "$i\r";
    $i++;

    foreach($attr as $json_key => $attr)
    {
        switch($attr)
        {
            case 'website_ids':
            case 'status':
                $out[$i][$attr] = 1;
            break;
            case 'qty':
            case 'description':
                $out[$i][$attr] = $itemjson->{'get'.ucfirst($json_key)}($i);
            break;
            case 'name':
            case 'sku':
            case 'weight':
                $out[$i][$attr] = $itemjson->{'get'.ucfirst($attr)}($i);
            break;
            // etc...

        }
    }
}

$api = Mage::getModel('api_import/import_api');
$api->importEntities(
    $out,
    Mage_ImportExport_Model_Import_Entity_Product::getEntityTypeCode(),
    Danslo_ApiImport_Model_Import::BEHAVIOR_REPLACE
);
1

You can check this script magento_dummy_simple_products_generation.php

Basically it allows you to create a bulk set of simple products, you hard code the PRODUCT_QTY to set the desired number of generated simple products (by default 15) and the CATEGORY_ID.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.