1

We're using the catalog_model_product_duplicate to override some of the attribute values upon duplicating a product. However, this only works on the global value:

$newProduct = $observer->getEvent()->getNewProduct();
$newProduct->setCustomAttrNewValue('fixed_value');

If I add a store ID to the new product, still, the global value and not store specific value is modified:

$newProduct = $observer->getEvent()->getNewProduct();
$newProduct->setStoreId($_store)->setCustomAttrNewValue('fixed_value');

setStoreId() has no effect. Is there a way I can override the product attribute value if there's a store selected?

1 Answer 1

2

Here is why it doesn't work.
The duplication is made for the global scope and then all the store specific values are copied.
The catalog_model_product_duplicate event is dispatched when cloning the global values.
You can take a look at the Magento_Catalog_Model_Product::duplicate().
At the end of the method there is this line:

$this->getResource()->duplicate($this->getId(), $newProduct->getId());

This is a call made to Mage_Catalog_Model_Resource_Product::duplicate().
This is what copies the store specific values.

You can rewrite this method in your custom module and set your value at the after the original method executes.
You will need this in your config.xml

<models>
    <catalog_resource>
        <rewrite>
            <product>[Namespace]_[Module]_Model_Resource_Product</product>
        </rewrite>
    </catalog_resource>
</models>

Then create the class [Namespace]_[Module]_Model_Resource_Product with this content

class [Namespace]_[Module]_Model_Resource_Product extends Mage_Catalog_Model_Resource_Product 
{
    public function duplicate($oldId, $newId)
    {
         parent::duplicate($oldId, $newId);
         $storeId = your store id here;
         Mage::getSingleton('catalog/product_action')->updateAttributes(
             array($newId),
             array('custom_attribute_code' => 'custom_attribute_value'),
             $storeId
         );
    }
}
1
  • Is there any event that fire when we create a duplicate product in m2 admin? Commented Oct 12, 2018 at 6:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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