I want to show color swatches of configurable product in home page But My all static blocks in cms page. Here is my static block
{{block class="Smartwave\Filterproducts\Block\Home\LatestList" name="latest" aspect_ratio="1" column_count="6" product_count="12" image_width="212" product_type="3" template="grid.phtml"}}
Here is my layout xml code which is working fine to display color swatches in home page but I want to show convert body part in static block.
Should I use addchild()
method in LatestList
block file ? If yes how ?
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<head>
<css src="Magento_Swatches::css/swatches.css"/>
</head>
<body>
<referenceBlock name="homepage.swatchrenderer">
<block class="Magento\Swatches\Block\Product\Renderer\Listing\Configurable" as="configurable" template="Magento_Swatches::product/listing/renderer.phtml" />
</referenceBlock>
<referenceContainer name="main">
<container name="topproduct" htmlTag="div" htmlClass="topproduct">
<block class="Smartwave\Filterproducts\Block\Home\LatestList" name="productlist" template="Smartwave_Filterproducts::grid.phtml">
<block class="Magento\Framework\View\Element\RendererList" name="homepage.swatchrenderer" as="homepage.toprenderers">
<block class="Magento\Framework\View\Element\Template" as="default"/>
</block>
</block>
</container>
</referenceContainer>
</body>
</page>
Here is my Block file
<?php
namespace Smartwave\Filterproducts\Block\Home;
use Magento\Catalog\Api\CategoryRepositoryInterface;
class LatestList extends \Magento\Catalog\Block\Product\ListProduct {
protected $_collection;
protected $categoryRepository;
protected $_resource;
public function __construct(
\Magento\Catalog\Block\Product\Context $context,
\Magento\Framework\Data\Helper\PostHelper $postDataHelper,
\Magento\Catalog\Model\Layer\Resolver $layerResolver,
CategoryRepositoryInterface $categoryRepository,
\Magento\Framework\Url\Helper\Data $urlHelper,
\Magento\Catalog\Model\ResourceModel\Product\Collection $collection,
\Magento\Framework\App\ResourceConnection $resource,
array $data = []
) {
$this->categoryRepository = $categoryRepository;
$this->_collection = $collection;
$this->_resource = $resource;
parent::__construct($context, $postDataHelper, $layerResolver, $categoryRepository, $urlHelper, $data);
}
protected function _getProductCollection() {
return $this->getProducts();
}
protected function _prepareLayout()
{
$this->addChild(
'homepage.toprenderers',
\Magento\Framework\View\Element\RendererList::class
);
$this->getLayout()->getBlock($this->getNameInLayout() . '.homepage.toprenderers')
->addChild('default', \Magento\Framework\View\Element\Template::class);
$this->getLayout()->getBlock($this->getNameInLayout() . '.homepage.toprenderers')
->addChild('configurable', \Magento\Swatches\Block\Product\Renderer\Listing\Configurable::class);
parent::_prepareLayout();
}
public function getProducts() {
$count = $this->getProductCount();
$category_id = $this->getData("category_id");
$collection = clone $this->_collection;
$collection->clear()->getSelect()->reset(\Magento\Framework\DB\Select::WHERE)->reset(\Magento\Framework\DB\Select::ORDER)->reset(\Magento\Framework\DB\Select::LIMIT_COUNT)->reset(\Magento\Framework\DB\Select::LIMIT_OFFSET)->reset(\Magento\Framework\DB\Select::GROUP);
if(!$category_id) {
$category_id = $this->_storeManager->getStore()->getRootCategoryId();
}
$category = $this->categoryRepository->get($category_id);
if(isset($category) && $category) {
$collection->addMinimalPrice()
->addFinalPrice()
->addTaxPercents()
->addAttributeToSelect('name')
->addAttributeToSelect('image')
->addAttributeToSelect('small_image')
->addAttributeToSelect('thumbnail')
->addAttributeToSelect($this->_catalogConfig->getProductAttributes())
->addUrlRewrite()
->addCategoryFilter($category)
->addAttributeToSort('created_at','desc');
} else {
$collection->addMinimalPrice()
->addFinalPrice()
->addTaxPercents()
->addAttributeToSelect('name')
->addAttributeToSelect('image')
->addAttributeToSelect('small_image')
->addAttributeToSelect('thumbnail')
->addAttributeToSelect($this->_catalogConfig->getProductAttributes())
->addUrlRewrite()
->addAttributeToSort('created_at','desc');
}
$collection->getSelect()
->order('created_at','desc')
->limit($count);
return $collection;
}
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(
'homepage.toprenderers'
);
}
public function getLoadedProductCollection() {
return $this->getProducts();
}
public function getProductCount() {
$limit = $this->getData("product_count");
if(!$limit)
$limit = 10;
return $limit;
}
}