Hi,
I have this code:
<?php require_once 'app/Mage.php'; umask(0) ; Mage::app(); try { Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); $product = Mage::getModel('catalog/product'); $product ->setWebsiteIds(array(1)) //website ID the product is assigned to, as an array ->setAttributeSetId(4) //ID of a attribute set named 'default' ->setTypeId('simple') //product type ->setCreatedAt(strtotime('now')) //product creation time ->setSku('Round Diamonds') //SKU ->setName('round diamonds') //product name ->setWeight(8.00) ->setStatus(1) //product status (1 - enabled, 2 - disabled) ->setTaxClassId(4) //tax class (0 - none, 1 - default, 2 - taxable, 4 - shipping) ->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH) //catalog and search visibility ->setManufacturer(28) //manufacturer id ->setColor(24) ->setNewsFromDate(strtotime('now')) //product set as new from ->setNewsToDate('06/30/2015') //product set as new to ->setCountryOfManufacture('AF') //country of manufacture (2-letter country code) ->setPrice(11.22) //price in form 11.22 ->setCost(11.11) //price in form 11.22 ->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'=>1, //Maximum Qty Allowed in Shopping Cart 'is_in_stock' => 1, //Stock Availability 'qty' => 1 //qty ) ) ->setMetaTitle('test meta title 2') ->setMetaKeyword('test meta keyword 2') ->setMetaDescription('test meta description 2') ->setDescription('This is a long description') ->setShortDescription('This is a short description') ->setMediaGallery (array('images'=>array (), 'values'=>array ())) //media gallery initialization ->addImageToMediaGallery('media/catalog/product/renkaat/350x350_image1.jpg', array('image','thumbnail','small_image'), false, false) //assigning image, thumb and small image to media gallery ->setLeveys(215) ->setCategoryIds(array(2, 3)); //assign product to categories $product->save(); }catch(Exception $e){ Mage::log($e->getMessage()); } ?>
Its adding product and images and all seems to be nice, but that own custom attribute:
->setLeveys(215)
is not working.
Here is the attribute:
What im missing here?
Im using Magento ver. 1.9.3.2
I get it, here is the solution:
<?php require_once 'app/Mage.php'; umask(0) ; Mage::app(); try { Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); $product = Mage::getModel('catalog/product'); $attribute = Mage::getModel('eav/entity_attribute'); $attribute->loadByCode( 4, 'leveys' ); $values = array(); $valuesCollection = Mage::getResourceModel('eav/entity_attribute_option_collection') ->setAttributeFilter( $attribute->getId() ) ->setStoreFilter( Mage_Core_Model_App::ADMIN_STORE_ID, false) ->load(); foreach ($valuesCollection as $item) { $values[$item->getValue()] = $item->getId(); } $product ->setWebsiteIds(array(1)) //website ID the product is assigned to, as an array ->setAttributeSetId(4) //ID of a attribute set named 'default' ->setTypeId('simple') //product type ->setCreatedAt(strtotime('now')) //product creation time ->setSku('Round Diamonds') //SKU ->setName('round diamonds') //product name ->setWeight(8.00) ->setStatus(1) //product status (1 - enabled, 2 - disabled) ->setTaxClassId(4) //tax class (0 - none, 1 - default, 2 - taxable, 4 - shipping) ->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH) //catalog and search visibility ->setManufacturer(28) //manufacturer id ->setColor(24) ->setNewsFromDate(strtotime('now')) //product set as new from ->setNewsToDate('06/30/2015') //product set as new to ->setCountryOfManufacture('AF') //country of manufacture (2-letter country code) ->setPrice(11.22) //price in form 11.22 ->setCost(11.11) //price in form 11.22 ->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'=>1, //Maximum Qty Allowed in Shopping Cart 'is_in_stock' => 1, //Stock Availability 'qty' => 1 //qty ) ) ->setMetaTitle('test meta title 2') ->setMetaKeyword('test meta keyword 2') ->setMetaDescription('test meta description 2') ->setDescription('This is a long description') ->setShortDescription('This is a short description') ->setMediaGallery (array('images'=>array (), 'values'=>array ())) //media gallery initialization ->addImageToMediaGallery('media/catalog/product/renkaat/350x350_Linglong_GreenMax_UHP.jpg', array('image','thumbnail','small_image'), false, false) //assigning image, thumb and small image to media gallery ->setCategoryIds(array(2, 3)); //assign product to categories $product->setleveys( $values['215'] ); // just do whatever you need to code 'Blue' instead of hard-coding it $product->save(); }catch(Exception $e){ Mage::log($e->getMessage()); } ?>