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()
))
);
}
Unfortunately the accepted answer didn't work for me and I wasn't able to use widget instance layout updates in my use case
If for whatever reason you need to add a 'Recently Viewed' products block specifically via layout xml, here's my solution (tested in version 2.2.2):
I decided to use the same class Magento\Catalog\Block\Widget\RecentlyViewed
which the 'Recently Viewed' Product widget uses in my block declaration.
<!-- Basic recently viewed products block -->
<block class="Magento\Catalog\Block\Widget\RecentlyViewed" name="recently_viewed" template="Magento_Catalog::product/widget/viewed/grid.phtml" after="-">
<arguments>
<argument name="uiComponent" xsi:type="string">widget_recently_viewed</argument>
<argument name="page_size" xsi:type="number">4</argument>
</arguments>
</block>
This will render a simple grid with the last 4 products view by the user.
You can configure the block further, just as you would the widget instance:
<!-- Configured recently viewed products block -->
<block class="Magento\Catalog\Block\Widget\RecentlyViewed" name="recently_viewed" template="Magento_Catalog::product/widget/viewed/grid.phtml" after="-">
<arguments>
<argument name="uiComponent" xsi:type="string">widget_recently_viewed</argument>
<argument name="page_size" xsi:type="number">4</argument>
<!-- 'Product attributes to show' configuration -->
<argument name="show_attributes" xsi:type="string">name,image,price,learn_more</argument>
<!-- 'Buttons to show' configuration -->
<argument name="show_buttons" xsi:type="string">add_to_cart,add_to_compare,add_to_wishlist</argument>
</arguments>
</block>
One thing to note is that the argument values for show_attributes
and show_buttons
need to be declared as comma delimited strings (no spaces).
Declaring them as arrays or including spaces next to your comma's won't work due to the way the UI Component parse the content before Knockout renders it.
And since I said Knockout... it should play nicely with FPC, should.
Finally this solution doesn't use the 'action' instruction which appears to have been deprecated
Best Answer
Try with below way.
Note : Above code is not tested you have to check. For more detail click here
I hope it helps!