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!