Showing Configurable Swatches in Home Page
Here, You can try the following method, For me it's working
- In Admin->content->pages
- select Home Page and edit
- Click the design Tab from left side
In Layout Update XML Column add following Code
<referenceContainer name="content">
<container name="home-page-full-block2" htmlTag="div" htmlClass="home-page-full-block2">
<block name="topselling.swatchrenderer">
<block class="Magento\Swatches\Block\Product\Renderer\Listing\Configurable" as="configurable" template="Magento_Swatches::product/listing/renderer.phtml" />
</block>
<block class="Ashik\ModuleName\Block\Topselling" name="productlist3" template="ModuleCompany_ModuleName::topselling.phtml">
<block class="Magento\Framework\View\Element\RendererList" name="topselling.swatchrenderer" as="topselling.toprenderers">
<block class="Magento\Framework\View\Element\Template" as="default"/>
</block>
</block>
</container>
</referenceContainer>
finally go to your block Ashik\ModuleName\Block\Topselling.php add following code
public function getProductDetailsHtml(\Magento\Catalog\Model\Product $product)
{
$renderer = $this->getDetailsRenderer($product->getTypeId());
if ($renderer) {
$renderer->setProduct($product);
return $renderer->toHtml();
}
return '';
}
public function getDetailsRenderer($type = null)
{
if ($type === null) {
$type = 'default';
}
$rendererList = $this->getDetailsRendererList();
if ($rendererList) {
return $rendererList->getRenderer($type, 'default');
}
return null;
}
protected function getDetailsRendererList()
{
return $this->getDetailsRendererListName() ? $this->getLayout()->getBlock(
$this->getDetailsRendererListName()
) : $this->getChildBlock(
'topselling.toprenderers'
);
}
In view page get swatches from echo $block->getProductDetailsHtml($_product);