1

When any product's quantity becomes 0 or out of stock i need to remove all the tags assigned to that particular product.

1 Answer 1

3

You will have to create a module for that.

First create the app/etc/modules/Vendor_Module.xml

<config>
  <modules>
    <Vendor_Module>
      <active>true</active>
      <codePool>local</codePool>
    </Vendor_Module>
  </modules>
</config>

Then you need to observe the cataloginventory_stock_item_save_commit_after that is dispatched when the stock of a product is changed as well as catalog_product_save_after in case the stock status is changed.

app/code/local/Vendor/Module/etc/config.xml

<?xml version="1.0"?>
<config>
  <modules>
    <Vendor_Module>
      <version>0.0.1</version>
    </Vendor_Module>
  </modules>
  <global>
        <models>  
            <module>
                <class>Vendor_Module_Model</class>
            </module>
        </models>
        <events>
                <catalog_product_save_after>
                    <observers>
                        <vendor_module_catalog_product_save_after>
                            <type>model</type>
                            <class>module/observer</class>
                            <method>afterProductSave</method>
                        </vendor_module_catalog_product_save_after>
                    </observers>
                </catalog_product_save_after>
                <cataloginventory_stock_item_save_commit_after>
                    <observers>
                        <vendor_module_cataloginventory_stock_item_save_commit_after>
                            <type>model</type>
                            <class>module/observer</class>
                            <method>afterStockSave</method>
                        </vendor_module_cataloginventory_stock_item_save_commit_after>
                    </observers>
                </cataloginventory_stock_item_save_commit_after>
        </events>
    </global> 
</config> 

Then in app/code/local/Vendor/Module/Model/Observer.php

<?php

class Vendor_Module_Model_Observer {

    public function afterStockSave(Varien_Event_Observer $observer)
    {
        $event = $observer->getEvent();
        $_item = $event->getItem();

        if ((int)$_item->getData('qty') == 0) {
            $productId = $_item->getProductId();
            $tags = Mage::getResourceModel('tag/collection')->addProductFilter($productId)->delete();
        }
    }

    public function afterProductSave(Varien_Event_Observer $observer)
    {
        $product = $observer->getProduct();
        $stockData = $product->getStockData();

        if ((int)$stockData['qty'] == 0 || !$stockData['is_in_stock']) {
            $productId = $product->getProductId();
            $tags = Mage::getResourceModel('tag/collection')->addProductFilter($productId)->delete();
        }
    }
}

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.