Kevin
Kevin

Reputation: 759

create a standalone page in magento

Im trying to write a script which add products in database. Its a magento website. Im trying to create a standalone page (example.com/import.php) and include magento framework so that i can use magento catalog model to add products. So far i tried this and got fatal error:

$mageFilename = 'app/Mage.php';
require_once $mageFilename;
Mage::setIsDeveloperMode(true);
umask(0);
Mage::app();
Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID));

Any help is highly appreciated. Thanks!

UPDATED:

This is what i ended up with. works perfectly. Thanks to Ram Sharma.

error_reporting(E_ALL ^ E_NOTICE);
include 'app/Mage.php';
Varien_Profiler::enable();
Mage::setIsDeveloperMode(true);
ini_set('display_errors', 1);
umask(0);
Mage::app('default');
Mage::register('isSecureArea', 1);


$product = new Mage_Catalog_Model_Product();

$product->setSku('some-sku-value-here');
$product->setAttributeSetId('9');# 9 is for default
$product->setTypeId('simple');
$product->setName('Some cool product name');
$product->setCategoryIds(array(42)); # some cat id's,
$product->setWebsiteIDs(array(1)); # Website id, 1 is default
$product->setDescription('Full description here');
$product->setShortDescription('Short description here');
$product->setPrice(39.99); # Set some price

$product->setWeight(4.0000);

$product->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH);
$product->setStatus(1);
$product->setTaxClassId(0); # default tax class
$product->setStockData(array(
    'is_in_stock' => 1,
    'qty' => 99999
));

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

try {
    $product->save();
}
catch (Exception $ex) {

}

Upvotes: 1

Views: 833

Answers (1)

Ram Sharma
Ram Sharma

Reputation: 8809

you can try something like this

error_reporting(E_ALL ^ E_NOTICE);
include 'app/Mage.php';
Varien_Profiler::enable();
Mage::setIsDeveloperMode(true);
ini_set('display_errors', 1);
umask(0);
Mage::app('default');
Mage::register('isSecureArea', 1);

Also remove error_reporting(E_ALL ^ E_NOTICE); ini_set('display_errors', 1); once you complete/done with your development work

Upvotes: 4

Related Questions