product default values

  • Setting Default Product Values in Magento

    When creating new products in the Magento Admin, there are a few fields that you often fill out with the same values over and over again. Values such as the product status, tax class, stock availability, weight, etc. Wouldn't it be nice to have the system automatically assign default values to these fields every time you create a new product? Well good news! You can easily accomplish this by hooking into Magento's catalog_product_new_action event. I'm not going to cover how to create new modules in Magento, but here is the basic structure and files you'll need to accomplish this. Make sure to provide your own value anywhere Namespace is referenced below. /app/code/local/Namespace/Catalog/etc/config.xml /app/code/local/Namespace/Catalog/Model/Product/Observer.php

    1) Place the following in the config.xml file listed above.

    <?xml version="1.0"?>
    <config>
    	<modules>
            <Namespace_Catalog>
                <version>1.0.0</version>
            </Namespace_Catalog>
        </modules>
    
    	<global>
    
            <models>
    			<namespace_catalog>
    	             <class>Namespace_Catalog_Model</class>
    	        </namespace_catalog>
    		</models>
    
            <events>
    			<catalog_product_new_action>
    	        	<observers>
    		          <namespace_catalog_product_observer>
    		            <type>singleton</type>
    		            <class>namespace_catalog/product_observer</class>
    		            <method>catalog_product_new_action</method>
    		          </namespace_catalog_product_observer>
    		        </observers>
    			</catalog_product_new_action>
    		</events>
    
    	</global>
    </config>

    2) Place the following in the Observer.php file. Set product default values as desired.

    <?php
    
    class Namespace_Catalog_Model_Product_Observer
    {
    	public function catalog_product_new_action($observer) {
    
    		$product = $observer->getEvent()->getProduct();
    		$product->setStatus(1);
    		$product->setTaxClassId(2);
    
    		$stockItem = Mage::getModel('cataloginventory/stock_item');
    		$stockItem->assignProduct($product);
    		$stockItem->setData('is_in_stock', 1);
    		$stockItem->setData('qty', 1);
    
    		$product->setStockItem($stockItem);
    	}
    }

    3) Finally, you'll need to activate your new module by creating the file below with the following contents.

    /app/etc/modules/Namespace_All.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <config>
    	<modules>
    		<Namespace_Catalog>
    			<active>true</active>
    			<codePool>local</codePool>
    		</Namespace_Catalog>
    	</modules>
    </config>
    Make sure to clear the Magento cache when you're done with this step as well! Check out the Magento wiki article on using Event/Observer for more information on this topic.

1 Item(s)