make a new product active by default in magento

2019-06-09 17:52发布

问题:

When duplicating a product in the backend in magento the new product status is Disabled by default. That confuses the store admins who expect the product to show on the frontend.

How can I make the product status Enabled by default?

THanks

回答1:

In your custom module you need to:

in config.xml file:

<config>
    <adminhtml>
        <events>
            <catalog_model_product_duplicate>
                <observers>
                    <custom_catalog_product_duplicate>
                        <class>custom_module/observer</class>
                        <method>catalogModelProductDuplicate</method>
                    </custom_catalog_product_duplicate>
                </observers>
            </catalog_model_product_duplicate>
        </events>
    </adminhtml>
</config>

Create an observer class with method like this:

class Custom_Module_Model_Observer 
{
    /**
     * Prepare product for duplicate action.
     *
     * @param Varien_Event_Observer $observer
     * @return object
     */
    public function catalogModelProductDuplicate(Varien_Event_Observer  $observer)
    {
        $newProduct = $observer->getEvent()->getNewProduct();
        $newProduct->setStatus(Mage_Catalog_Model_Product_Status::STATUS_ENABLED);

        return $this;
    }
}