0

I am adding a widget to a CMS page. Within this widget will be some ajax calls -- however, i'm unsure what / how I should call this layout block in the controller to update the frontend code.

The widget is located here:

magento/app/design/frontend/enterprise/default/template/catalog/product/widget/new/content/new_grid.phtml

In the CMS page I can calling the widget as such:

{widget type="catalog/product_widget_new" display_type="new_products" show_pager="1" products_per_page="20" products_count="1000" template="catalog/product/widget/new/content/new_grid.phtml"}

The ajax calling function will look something like:

newPerPage = function (value) {
    try {
        jQuery.ajax({
            url: url,
            dataType: 'json',
            type: 'post',
            data: value,
            success: function (data) {
                jQuery('#product-grid-category').html(data.field);
            }
        });
    } catch (e) {
    }
};

The ajax conotroller function will look something like:

public function newPagerAction(){
    $this->setData('products_per_page', param);
    $response = array();
    $response['field'] = $this->getLayout()->createBlock('....')->setTemplate('....')->toHtml();
    $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($response));
}

1 Answer 1

3

Try this in your controller:

public function couponPostAction()
{
    if (!$this->getRequest()->isAjax()) {
        $this->_forward('noRoute');
        return;
    }

    $block = $this->getLayout()->createBlock('catalog/product_widget_new', 'product.widget.new', array(
        'show_pager'        => 1,
        'products_per_page' => 24,
        'products_count'    => 1000,
        'display_type'      => 'new_products',
        'template'          => 'catalog/product/widget/new/content/new_grid.phtml'
    ));

    $this->getResponse()->setBody(
        Mage::helper('core')->jsonEncode(array(
            'field' => $block->toHtml()
        ))
    );
}
4
  • I assume you mean instead of '24' I will use the data passed to the controller, right? Thank you regardless. This is essentially exactly what I needed. Commented Jan 22, 2015 at 18:12
  • Yes, you can pass data in ajax call and fetch it with $this->getRequest()->getParam('someparam'); Commented Jan 22, 2015 at 18:14
  • Any idea of an appropriate place to put this controller? Or should I just make a new module? Commented Jan 22, 2015 at 18:24
  • You should place it in the controllers folder inside either new or existing custom module and adjust url var in your javascript accordingly. Commented Jan 22, 2015 at 18:31

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.