0
votes

I'm trying to write a script to add 10 products to Magento using php/mage.

This is what I have so far, in Magento 1.8.1

<?php
ini_set('memory_limit', '-1');
ini_set('display_errors', '1');
error_reporting(E_ALL);
require '../app/Mage.php';
$app = Mage::app('default');
set_time_limit(0);
$product = Mage::getModel('catalog/product');
$sku = 1;

do {
$product->setName(trim(strip_tags('Unnamed Product')));
$product->setDescription(trim(strip_tags('Description')));
$product->setShortDescription(trim(strip_tags('Short Description')));
$product->setSku($sku);
$product->setPrice(trim(strip_tags('19.99'))); # Set some price
$product->setWeight('1');
$product->setCreatedAt(strtotime('now'));
$product->setTypeId('simple');
$product->setTaxClassId(2); // taxable goods
$def_attribute_set = Mage::getModel('catalog/product')->getResource()->getEntityType()->getDefaultAttributeSetId();
$product->setAttributeSetId($def_attribute_set);
$stock_data=array(
'use_config_manage_stock' => 1,
'qty' => trim(strip_tags($data[13])),
'min_qty' => 0,
'use_config_min_qty'=>1,
'min_sale_qty' => 0,
'use_config_min_sale_qty'=>1,
'max_sale_qty' => 9999,
'use_config_max_sale_qty'=>1,
'is_qty_decimal' => 0,
'backorders' => 0,
'notify_stock_qty' => 1,
'is_in_stock' => 1
);
$product->setData('stock_data',$stock_data);
$product->setWebsiteIds(array(Mage::app()->getStore(true)->getWebsite()->getId()));
$product->setStatus(1);
$product->setVisibility(4);
$product->save();
$sku++;
} while ($sku<=10)

?>

I'm trying to tell it to add products, increment the sku by 1, and keep adding and incrementing till it gets to 10. However it only adds one product and then does nothing?

EDIT: SOLVED

Here's what I did, I was missing it setting a product ID, so it would stop because they already existed. ID wasn't being incremented but SKU was! Below is the working code:

Working Code

<?php
ini_set('memory_limit', '-1');
ini_set('display_errors', '1');
error_reporting(E_ALL);
require '../app/Mage.php';
$app = Mage::app('default');
set_time_limit(0);
$product = Mage::getModel('catalog/product');
$sku = 2;
$productid = 2;

do {
$product->setName(trim(strip_tags('Unnamed Product')));
$product->setDescription(trim(strip_tags('Description')));
$product->setShortDescription(trim(strip_tags('Short Description')));
$product->setSku($sku);
$product->setEntityId($productid);
$product->setPrice(trim(strip_tags('19.99'))); # Set some price
$product->setWeight('1');
$product->setCreatedAt(strtotime('now'));
$product->setTypeId('simple');
$product->setTaxClassId(2); // taxable goods
$def_attribute_set = Mage::getModel('catalog/product')->getResource()->getEntityType()->getDefaultAttributeSetId();
$product->setAttributeSetId($def_attribute_set);
$stock_data=array(
'use_config_manage_stock' => 1,
'qty' => trim(strip_tags($data[13])),
'min_qty' => 0,
'use_config_min_qty'=>1,
'min_sale_qty' => 0,
'use_config_min_sale_qty'=>1,
'max_sale_qty' => 9999,
'use_config_max_sale_qty'=>1,
'is_qty_decimal' => 0,
'backorders' => 0,
'notify_stock_qty' => 1,
'is_in_stock' => 1
);
$product->setData('stock_data',$stock_data);
$product->setWebsiteIds(array(Mage::app()->getStore(true)->getWebsite()->getId()));
$product->setStatus(1);
$product->setVisibility(4);
$product->save();
$sku++;
$productid++;
} while ($sku<=10)

?>

Now I just need to figure out how to check if a sku/ID exists, and if it does - skip it!

1
set_time_limit(0); I recommend that this not to be used.If needed set time as 10000 or something but never 0Oscprofessionals

1 Answers

0
votes

Even if your code seams to work, calling $product->setEntityId($productid); is kind of wrong.
If you run your script again, you won't get 10 new products. Your old products will be updated.

The problem is that you use the same product instance to do all the things.
So the first time your run it, you get a new products, the second through tenth you get an update for the added product.

The only thing you need to do is to include $product = Mage::getModel('catalog/product'); inside the do-while loop.

Something like this:

do {
$product = Mage::getModel('catalog/product');
$product->setName(trim(strip_tags('Unnamed Product')));
$product->setDescription(trim(strip_tags('Description')));
$product->setShortDescription(trim(strip_tags('Short Description')));
$product->setSku($sku);
$product->setPrice(trim(strip_tags('19.99'))); # Set some price
$product->setWeight('1');
$product->setCreatedAt(strtotime('now'));
$product->setTypeId('simple');
$product->setTaxClassId(2); // taxable goods
$def_attribute_set = Mage::getModel('catalog/product')->getResource()->getEntityType()->getDefaultAttributeSetId();
$product->setAttributeSetId($def_attribute_set);
$stock_data=array(
'use_config_manage_stock' => 1,
'qty' => trim(strip_tags($data[13])),
'min_qty' => 0,
'use_config_min_qty'=>1,
'min_sale_qty' => 0,
'use_config_min_sale_qty'=>1,
'max_sale_qty' => 9999,
'use_config_max_sale_qty'=>1,
'is_qty_decimal' => 0,
'backorders' => 0,
'notify_stock_qty' => 1,
'is_in_stock' => 1
);
$product->setData('stock_data',$stock_data);
$product->setWebsiteIds(array(Mage::app()->getStore(true)->getWebsite()->getId()));
$product->setStatus(1);
$product->setVisibility(4);
$product->save();
$sku++;
unset($product);//unset the var because you don't need it anymore.
} while ($sku<=10)