2

I have attribute sets which I use when adding new products. All products have a weight attribute. I know you can set up a default value which will appear along all attribute sets with that attribute. My question is can I set up different default weight for each attribute set? and if so any tips on how I can achieve it would be highly helpful.

I use Magento Commerce 1.7.0.2

3

Interesting question. With default Magento, you can't. But there are ways. I'd recommend creating an extension that listens to the catalog_product_save_before event in the adminhtml scope.

Create the file app/etc/modules/Your_Module.xml with the following content;

<?xml version="1.0"?>
<config>
    <modules>
        <Your_Module>
            <active>true</active>
            <codePool>local</codePool>
        </Your_Module>
    </modules>
</config>

Next up, app/code/local/Your/Module/etc/config.xml, where we declare the event to listen for and which method to execute;

<?xml version="1.0"?>
<config>
    <modules>
        <Your_Module>
            <version>0.1.0</version>
        </Your_Module>
    </modules>
    <adminhtml>
        <events>
            <catalog_product_load_after>
                <observers>
                    <your_module>
                        <class>Your_Module_Model_Observer</class>
                        <method>setDefaultWeightValue</method>
                    </your_module>
                </observers>
            </catalog_product_load_after>
        </events>
    </adminhtml>
    <global>
        <models>
            <your_module>
                <class>Your_Module_Model</class>
            </your_module>
        </models>
    </global>
</config>

And finally, app/code/local/Your/Module/Model/Observer.php, where we set the predefined weights for the different attribute set IDs and where we assign the value to the product currently opened;

<?php

class Your_Module_Model_Observer {

    // This is the default value you would like to use
    const DEFAULT_WEIGHT = 50;

    // Change this if you have set a default value on the attribute other than empty
    const DEFAULT_WEIGHT_SETTING_MAGENTO = '0.0000';

    public function setDefaultWeightValue($observer) {
        $_product = $observer->getProduct();
        if($_product->getWeight() == self::DEFAULT_WEIGHT_SETTING_MAGENTO) {
            $_product->setWeight(
                $this->getWeightsPerAttributeSet($_product->getAttributeSetId())
            );
        }
    }

    public function getWeightsPerAttributeSet($attributeSetId) {
        $weightsPerAttribuetSet = array(
            4 => 100,
            5 => 200,
            6 => 300
        );

        if(isset($weightsPerAttribuetSet[$attributeSetId])) {
            return $weightsPerAttribuetSet[$attributeSetId];
        } else {
            return self::DEFAULT_WEIGHT;
        }
    }

}

This will only work for existing products, since new products aren't loaded and thus won't fire the event.

2

I would create an observer for catalog_product_save_before, which is triggered the first time, when this form is submitted:

New Product

which saves the product with id, type and attribute set and redirects you to the edit page. So ther you will see the new default values that you choose dynamically based on the attribute set.

To prevent saving them again on subsequent saves, use isObjectNew():

if ($product->isObjectNew()) {
    // set default values here, based on attribute set
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.