5

I'm working on an ajax module and I have to create a product alert button block. I'm using this code to create the block:

$stockAlertBlock = $this->getLayout()->createBlock(
            'productalert/product_view',
            'alert_stock',
            array('template' => 'productalert/product/view.phtml')
        );

        $stockAlertBlock->setHtmlClass('alert-stock link-stock-alert');
        $stockAlertBlock->setSignupLabel('Register to receive stock alert');

The block will be created but the link is not working because I'm not able to set correctly the product.

I've tried

$stockAlertBlock->setSignupUrl('productalert/add/stock/product_id/' . $simpleProduct->getId().'/uenc/'.Mage::helper('core/url')->getEncodedUrl().'/');

but it's not working.

Do you have any suggestion? thanks

  • What is not working? Where is the second code from? NEVER tell someone it is not working, always describe "it" :-) – Fabian Blechschmidt Jun 1 '15 at 16:54
  • is the last parameter I miss to make it works. I guess I'm wrong generating the uenc part. – Antonio Pedicini Jun 1 '15 at 16:59
1

The product is set here:

\Mage_ProductAlert_Block_Product_View::_prepareLayout
protected function _prepareLayout()
{
    $product = Mage::registry('current_product');
    if ($product && $product->getId()) {
        $this->_product = $product;
    }

    return parent::_prepareLayout();
}

I'm not sure whether _prepareLayout() is called if you create the block this way. So you can try to set it in the registry beforehand, the alternative is to either extend the block to make _product accessable or use reflection.

| improve this answer | |
  • I just have to setSignupUrl. I'm using $stockAlertBlock->setSignupUrl('productalert/add/stock/product_id/' . $simpleProduct->getId().'/uenc/'.Mage::helper('core/url')->getEncodedUrl().'/'); But doesn't work. can you help please? – Antonio Pedicini Jun 1 '15 at 16:45
  • Can you please edit your question and add the information, so it is readable? – Fabian Blechschmidt Jun 1 '15 at 16:45
0

You can do this by a simple way.

Rewrite helper class Mage_ProductAlert_Helper_Data of Mage_ProductAlert module and create a custom function and on this funtion write code for custom url for product alert.

<?php
class Amit_CustomProductAlert_Helper_Data extends Mage_ProductAlert_Helper_Data
{
    public function getStockSimpleSaveUrl($type=stock,$product_id)
    {
        return $this->_getUrl('productalert/add/' . $type, array(
            'product_id'    => $product_id,
            Mage_Core_Controller_Front_Action::PARAM_NAME_URL_ENCODED => $this->getEncodedUrl()
        ));
    }

}

?>

Now using the function you can get product stock alert url at any where

Mage::helper('productalert')->getStockSimpleSaveUrl('stock',$productId))
| improve this answer | |

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.