Stopping product saving process in observer

后端 未结 5 1517
猫巷女王i
猫巷女王i 2021-01-06 05:07

I am currently developing a module working with the product edit in the backend. Its purpose is to retrieve categories the product belongs to and populate an attribute (the

5条回答
  •  南笙
    南笙 (楼主)
    2021-01-06 05:50

    In case you need to prevent the save method to execute for a core model (i.e. Catalog/Product), you can use reflection to set "$_dataSaveAllowed" to false:

    public function catalogProductSaveBefore($observer)
    {
        try {
            $product = $observer->getProduct();
    
            $reflectionClass = new ReflectionClass('Mage_Catalog_Model_Product');
            $reflectionProperty = $reflectionClass->getProperty('_dataSaveAllowed');
            $reflectionProperty->setAccessible(true);
            $reflectionProperty->setValue($product, false);
        } catch (Exception $e) {
                Mage::log($e->getMessage());
        }
    
        return $this;
    }
    

提交回复
热议问题