1

What have i done wrong here?

If someone saves a product or updates a product attribute but the product is disabled it shouldn't send an email.

If a product is enabled and they disable it whether they change attributes or anything else... it should send an email. as long as it goes from ENABLED TO DISABLED.

Here's my logic:

 public function detectProductChanges($observer)
    {
        $product = $observer->getEvent()->getProduct();

        if ($product->hasDataChanges()) 
        {
            $data        = $observer->getEvent()->getData();
            $status      = $product['status'];
            $getstatus   = $product->getStatus();
            $productname = $product->getName();
            $productsku  = $product->getSku();
            $store       = $product->getWebsiteIds();

            if ($getstatus == 2) 
            {
                $prod = Mage::getSingleton('catalog/product')->load($product->getId());
                $savestatus = $prod->getStatus();

                if ($savestatus == 1) 
                {
                    $this->_sendStatusMail($productname, $productsku, $store, $product->getId());
                }
            }
        }
    } 

Any help would be greatly appreciated.

Additionally,

I'm using these events:

<events>
        <catalog_product_save_after>
            <observers>
                <productchange>
                    <type>singleton</type>
                    <class>ic_ProductNotification_Model_Observer</class>
                    <method>detectProductChanges</method>
                </productchange>
            </observers>
        </catalog_product_save_after>
        <catalog_product_attribute_update_before>
            <observers>
                <product_attributes_change>
                    <type>singleton</type>
                    <class>ic_ProductNotification_Model_Observer</class>
                    <method>detectProductAttributesChanges</method>
                </product_attributes_change>
            </observers>
        </catalog_product_attribute_update_before>
        <catalog_product_import_finish_before>
            <observers>
                <productchangess>
                    <type>singleton</type>
                    <class>ic_ProductNotification_Model_Observer</class>
                    <method>detectImportProductChanges</method>
                </productchangess>
            </observers>
        </catalog_product_import_finish_before>
        </events>
1

For your observer, you should use the event <catalog_product_save_commit_after>.

Here is the function you should use, I have tested it just to be sure.

public function detectProductChanges(Varien_Event_Observer $observer)
{
    $product = $observer->getEvent()->getProduct();
    if($product instanceof Mage_Catalog_Model_Product){
        if($product->getData('status') != $product->getOrigData('status') &&
           $product->getData('status') == Mage_Catalog_Model_Product_Status::STATUS_DISABLED){
            //if you have reached the inside of this if statement, someone disabled the product
        }

    }
    return $this;
}

If this works for you, please mark my answer as accepted.

  • Thank you for your help. Although I ended up using: catalog_product_save_before – thismethod May 12 '16 at 16:54
  • After I posted this I read about using that function $oldData = $model->getOrigData(); --- That will still be really useful from now on so i marked your answer as correct because it's an example from the core. – thismethod May 12 '16 at 16:56

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.