0

I need to show labels(BACK ON STOCK) on product when they back to stock, for this I have added a observer

    <global>
       <events>
       <catalog_product_save_commit_after> 
         <observers>
           <Name_StockLabel_Model_Observer> 
             <type>singleton</type> 
             <class>Name_StockLabel_Model_Observer</class> 
             <method>backInStock</method>  
            </Name_StockLabel_Model_Observer>
         </observers>
       </catalog_product_save_commit_after>
     </events>
   </global>

and in observer am adding trying to add block

 public function backInStock(Varien_Event_Observer $observer)
    {
        $product = $observer->getEvent()->getProduct();
        $stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product->getId());
        $stockItem = $product->getStockItem();
        if ($stockItem->getCanBackInStock() && $stockItem->getQty() > $stockItem->getMinQty() && $stockItem->getIsInStock()){

            WANT TO ADD BLOCK HERE THAT WILL DISPLAY ON PRODUCT IN FRONTEND

        }
  }

Can anyone here advise me how can I add I block here

  • Hello @Daniel_12, did my solution work for you ? – Pawan May 31 at 3:38
0

You can use an alternative way:

Create an attribute say back_on_stock with type Yes/No.

Change you event form catalog_product_save_commit_after to catalog_product_save_before.

Now in observer you can set this new attribute value in your if condition, like:

$product->setBackOnStock(1);
return $this;

Now in Your phtml, you can check this attribute value and show label

Hope above will help!

  • my attribute is not getting save I am getting Null always, am updating product from backend – Daniel_12 Jun 12 at 13:59
  • could you please share your code ? – Pawan Jun 12 at 17:03
  • ```function backInStock(Varien_Event_Observer $observer) { $product = $observer->getEvent()->getProduct(); $product->setBackInStock(1); //$product->save(); return $this; }```` – Daniel_12 Jun 13 at 9:35
  • what is your attribute code ? – Pawan Jun 13 at 18:22
0

I don't think you need an observer for that.

Just add a conditon in your template (catalog/product/view.phtml), like this:

//$_product is Mage_Catalog_Model_Product object

<?php if ($this->displayProductStockStatus() && $_product->isAvailable()) :?>
    Stock is back
    <?php //echo $this->getLayout()->createBlock('cms/block')->setBlockId('your-block-id')->toHtml()?>
<?php endif;?>

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.