Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Amasty_Example_AmastyController extends Mage_Core_Controller_Front_Action
- {
- public function indexAction()
- {
- echo 'This is index controller. Please use specific actions below to create products.
- <br/>
- <a href="/example/amasty/createSimpleProduct">Create Simple Product</a>';
- }
- public function createSimpleProductAction()
- {
- $product = $this->_createProduct(Mage_Catalog_Model_Product_Type::TYPE_SIMPLE);
- echo 'See <a href="/catalog/product/view/id/' . $product->getId() . '">created simple product</a>';
- }
- public function createSimpleProductAndRedirectAction()
- {
- if($product = $this->_createProduct(Mage_Catalog_Model_Product_Type::TYPE_SIMPLE))
- {
- $this->_redirect("catalog/product/view/id/".$product->getId());
- }
- }
- protected function _createProduct($type, $doSave=true)
- {
- // required for some versions
- Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
- $product = Mage::getModel('catalog/product');
- // set madatory system attributes
- $rand = rand(1, 9999);
- $product
- ->setTypeId($type) // e.g. Mage_Catalog_Model_Product_Type::TYPE_SIMPLE
- ->setAttributeSetId(17) // default attribute set
- ->setSku('CDSB1_sku' . $rand) // generate some random SKU
- ->setWebsiteIDs(array(1))
- ;
- // make the product visible
- $product
- ->setCategoryIds(array(2,3))
- ->setStatus(Mage_Catalog_Model_Product_Status::STATUS_ENABLED)
- ->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH) // visible in catalog and search
- ;
- // configure stock
- $product->setStockData(array(
- 'use_config_manage_stock' => 1, // use global config ?
- 'manage_stock' => 0, // shoudl we manage stock or not?
- 'is_in_stock' => 1,
- 'qty' => 50,
- ));
- // optimize performance, tell Magento to not update indexes
- $product
- ->setIsMassupdate(true)
- ->setExcludeUrlRewrite(true)
- ;
- // finally set custom data
- $product
- ->setName($product->getName()) // add string attribute
- ->setShortDescription('description') // add text attribute
- // set up prices
- ->setPrice(599)
- //->setSpecialPrice(19.99)
- ->setTaxClassId(1) // Taxable Goods by default
- ->setWeight(1)
- ;
- // add dropdown attributes like brand, color or size
- //$optionId = $this->_getOptionIDByCode('color', 'Black');
- //$product->setColor($optionId);
- //$optionId = $this->_getOptionIDByCode('size', 'M');
- //$product->setSize($optionId);
- // add product images
- $images = array(
- 'thumbnail' => 'image.png',
- 'small_image' => 'image.png',
- 'image' => 'image.png',
- );
- $dir = Mage::getBaseDir('media') . DS . 'example/amasty/';
- 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/>";
- }
- }
- if ($doSave)
- $product->save();
- return $product;
- }
- protected function _getOptionIDByCode($attrCode, $optionLabel)
- {
- $attrModel = Mage::getModel('eav/entity_attribute');
- $attrID = $attrModel->getIdByCode('catalog_product', $attrCode);
- $attribute = $attrModel->load($attrID);
- $options = Mage::getModel('eav/entity_attribute_source_table')
- ->setAttribute($attribute)
- ->getAllOptions(false);
- foreach ($options as $option) {
- if ($option['label'] == $optionLabel) {
- return $option['value'];
- }
- }
- return false;
- }
- }
Add Comment
Please, Sign In to add comment