Skip to content

Instantly share code, notes, and snippets.

@rintoug
Created March 27, 2017 18:23
Show Gist options
  • Select an option

  • Save rintoug/bfd63ef8cb732bcc86da4eaaebd564ce to your computer and use it in GitHub Desktop.

Select an option

Save rintoug/bfd63ef8cb732bcc86da4eaaebd564ce to your computer and use it in GitHub Desktop.

Revisions

  1. rintoug created this gist Mar 27, 2017.
    70 changes: 70 additions & 0 deletions magento.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,70 @@
    <?php
    error_reporting(E_ALL);
    ini_set('display_errors', TRUE);
    ini_set('display_startup_errors', TRUE);
    require_once('../app/Mage.php'); //magento bootstrapper

    umask(0);
    Mage::app('default');
    Mage::app ()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); //set the magento store id as admin

    $product = Mage::getModel('catalog/product');
    try {
    $product
    ->setWebsiteIds(array(1)) //website ID
    ->setAttributeSetId(4) //ID of a attribute set named 'default'
    ->setTypeId('simple') //product type
    ->setCreatedAt(strtotime('now')) //product creation time
    ->setSku(time()) //SKU
    ->setWeight(1.0000)
    ->setStatus(1) //product status (1 - enabled, 2 - disabled)
    ->setTaxClassId(0) //tax class (0 - none, 1 - default, 2 - taxable, 4 - shipping)
    ->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH) //catalog and search visibility
    ->setPrice(11.22) //price in form 11.22
    ->setCategoryIds(array(1723, 1913)) //assign product to categories
    ->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' => 2, //Maximum Qty Allowed in Shopping Cart
    'is_in_stock' => 1, //Stock Availability
    'qty' => 999 //qty
    )
    )
    ->setMetaTitle('Our sample product')
    ->setMetaKeyword('test meta kewords')
    ->setMetaDescription('test meta description')
    ->setShortDescription('Whoa, its short descrition')

    ->setName('My test product name'); //product name;

    //Adding images

    $images = array(
    'thumbnail' => 'image1_1.jpg',
    'small_image' => 'image1_1.jpg',
    'image' => 'image1_1.jpg',
    );

    $dir = Mage::getBaseDir('media') . DS . 'example/imagedir/';

    foreach ($images as $imageType => $imageFileName) {
    $path = $dir . $imageFileName;
    if (file_exists($path)) {
    try {
    $product->addImageToMediaGallery($path, $imageType, false);
    } catch (Exception $e) {
    echo $e->getMessage();
    }
    } else {
    echo "Can not find image by path: `{$path}`<br/>";
    }
    }
    $product->setIsMassupdate(true)->setExcludeUrlRewrite(true);

    $save = $product->save();
    //var_dump($save);
    } catch (Exception $e) {
    Mage::log($e->getMessage());
    echo $e->getMessage();
    }