Magento – Event for bulk updating product status

event-observermagento-1.9soap

I am trying to find event when admin mass update product status from productgrid.

enter image description here

I have found catalog_product_status_update but this not seem to dispatch on bulk update of status.

I need event for observer to call extern webservice when product status change.

When i update single product from i have it working with catalog_product_save_after.

Any suggestions to help me archieve what i want?

This is what i have so far.

Config

<?xml version="1.0"?>
<config>
    <modules>
        <DM_Microcom>
            <version>0.1.0</version>
        </DM_Microcom>
    </modules>
    <global>
        <helpers>
            <microcom>
                <class>DM_Microcom_Helper</class>
            </microcom>
            <admin>
                <rewrite>
                    <data>DM_Microcom_Helper_Admin_Data</data>
                </rewrite>
            </admin>
        </helpers>
        <models>
            <microcom>
                <class>DM_Microcom_Model</class>
                <resourceModel>microcom_mysql4</resourceModel>
            </microcom>
        </models>
        <events>
            <catalog_product_save_after>
                <observers>
                    <catalog_product_save_after_handler>
                        <type>singleton</type> 
                        <class>microcom/observer</class>
                        <method>SetWebVare</method>
                    </catalog_product_save_after_handler>
                </observers>
            </catalog_product_save_after>
            <catalog_product_status_update> 
                <observers>
                    <catalog_product_status_update_handler>
                        <type>singleton</type>
                        <class>microcom/observer</class>
                        <method>SetWebVare</method>
                    </catalog_product_status_update_handler>
                </observers>
            </catalog_product_status_update>
        </events>
    </global>
</config> 

Observer

<?php
class DM_Microcom_Model_Observer{

            public function SetWebVare(Varien_Event_Observer $observer)
            {
                $user = "USERNAMEHERE";
                $pass = "PASSHERE";
                $product = $observer->getProduct();
                $sku = $product->getSku();
                $newStatus = $product->getStatus();
                $oldStatus = $product->getOrigData('status');
                $status = ($newStatus == 2 ? 0 : 1); 

                if ($newStatus != $oldStatus) {

                    $params = array(              
                        'Brugernavn' => $user, 
                        'Adgangskode' => $pass,
                        'VNr' => $sku,


                'Status' => $status           
                );  

                Mage::log('SetWebVare sku:' . $sku . '. Status: ' . $status, null, 'microcom.log');
                $soapclient = new SoapClient('http://example.com/webservices?wsdl');
                $response = $soapclient->V01_SetWEBVare($params);                   
            }
        }

}

Best Answer

For Magento >= 1.9.2.0:

The event you are looking for is catalog_controller_product_mass_status, it is dispatched under the massStatusAction of app/code/core/Mage/Adminhtml/controllers/Catalog/ProductController.php

However this event only provides the ids of the products that are mass updated, which you can retrieve this way in your observer:

$productsIds = $observer->getEvent()->getProductIds();

For Magento >= 1.6.0.0-beta1:

On top of that you can also observe one of the following events (depending on your needs):

  • catalog_product_attribute_update_before which is dispatched in the updateAttributes method of the app/code/core/Mage/Catalog/Model/Product/Action.php
  • catalog_product_attribute_update_after which is dispatched in the same location as the previous one

BEWARE: thore events are not only triggered by the mass status action, they are also triggered by other Magento functionalities, use with care.

As the massStatusAction method calls the following code, those two events will be dispatched:

Mage::getSingleton('catalog/product_action')
            ->updateAttributes($productIds, array('status' => $status), $storeId);

You can use two two events in your observer this way:

For the first event:

// Get the attribute data
$attributesData = $observer->getEvent()->getAttributesData();
// Get the product ids
$productIds = $observer->getEvent()->getProductIds();
// Get the store id
$storeId = $observer->getEvent()->getStoreId();

For the second one you can only do:

// Get the product ids
$productIds = $observer->getEvent()->getProductIds();

For Magento < 1.6.0.0-beta1:

I guess the last solution here is to observer one the following event: controller_action_predispatch_adminhtml_catalog_product_massStatus

You can use it in your observer this way:

// Get the request
$request = $observer->getEvent()->getControllerAction()->getRequest();
// Get the product ids
$productIds = $request->getParam('product');
// Get the store id
$storeId = (int)$request->getParam('store',0);
// Get the status applied
$status = $request->getParam('status');