Set Default Values For Attributes When Creating Products – By Product Type Too

I was asked today to change the default value of an attribute to something other than the set default just for simple products only when creating a new product through the admin. I found a very easy way to do this using an event observer. I will do my best to explain:

The event you will want to observe is: catalog_product_new_action. I named the method in the observer to ‘setNewProductDefaults’. I will provide a modified version of my method with some different examples:

public function setNewProductDefaults($observer)
{
    // Get the product object
    $product = $observer->getProduct();
 
    // Sets default for all product types
    $product->setStatus('1');
    
    // Set defaults if it is a Simple Product
    if ($product->getTypeId() == 'simple') {
        $product->setWeight('1')
            ->setTaxClassId('2');
    }
 
    // Set defaults if it is a Configurable Product
    if ($product->getTypeId() == 'configurable') {
        $product->setCustomDesign('yourcustom/design');
    } 
}

And that’s all you need to do! If you’ve implemented this correctly into a custom module, when you create new products in the admin, you will see the attributes set to what you set them to in your observer. Implementing this can save both time, and forgetfulness when setting up new products through the admin.

This entry was posted in Magento. Bookmark the permalink.

One Response to Set Default Values For Attributes When Creating Products – By Product Type Too

  1. Adam Moss says:

    Such a lightweight solution – gotta love observers!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>