PHP Magento Tips

set default values to attribute for newly created product in Magento

Rate this posting:
{[['']]}

When you create a new product in Magento then some attribute are not selected as you expect. In this blog we will create a simple custom module in Magento with which we can make some product attribute like status, tax, stock avability and quantity to some selected values or pre-filled values by default.

Step 1:

In  app/etc/modules/Mycompany_Catalog.xml add below code:

<?xml version="1.0"?>

<config>

    <modules>

        <Mycompany_Catalog>

            <active>true</active>

            <codePool>local</codePool>

        </Mycompany_Catalog>

    </modules>

</config>


Step 2:

In  app/code/local/Mycompany/Catalog/etc/config.xml add below code:

<?xml version="1.0"?>

<config>

 <modules>

        <Mycompany_Catalog>

            <version>1.0.0</version>

        </Mycompany_Catalog>

    </modules>



 <global>

    <models>

         <mycompany_catalog>

              <class>Mycompany_Catalog_Model</class>

         </mycompany_catalog>

    </models>



    <events>

    <catalog_product_new_action>

        <observers>

            <mycompany_catalog_product_observer>

              <type>singleton</type>

              <class>mycompany_catalog/product_observer</class>

              <method>catalog_product_new_action</method>

            </mycompany_catalog_product_observer>

        </observers>

    </catalog_product_new_action>

   </events>

 </global>

</config>

Step 3:

In  app/code/local/Mycompany/Catalog/Model/Product/Observer.php add below code:

<?php

/*

 * Observer which hooks into the magento catalog_product_new_action event and sets default values.

 * app/code/local/Mycompany/Catalog/Model/Product/Observer.php

 */

class Mycompany_Catalog_Model_Product_Observer

{

 public function catalog_product_new_action($observer) {

  $product = $observer->getEvent()->getProduct();

  $product->setStatus(1); // Active = Yes

  $product->setTaxClassId(2); // Taxable Goods

  $stockItem = Mage::getModel('cataloginventory/stock_item');

  $stockItem->assignProduct($product);

  $stockItem->setData('is_in_stock', 1); // Stockable = true

  $stockItem->setData('qty', 100); // Default Quantity = 100

  $product->setStockItem($stockItem);

 }

}

Now when you create a new product then each time Magento will set some default vales to Product Status, Tax Class, Qty and Stock Availability.





Tag : Magento
0 Comments On "set default values to attribute for newly created product in Magento"

Back To Top