1

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;
    }
}
4
  • I read your question and was keen to give it a go.. but really I don’t understand where you are at the moment and what is your issue..: put your block code maybe so that we can have same problem as you.. – Herve Tribouilloy Sep 13 '20 at 15:58
  • @HerveTribouilloy Actually I want to display color swatches in home page so I have porto theme so,Porto theme inject all statick blocks in cms page to display products in home page so I have a solution to display color swatches of configurable product in home page for that I have to declare block in layout xml file .I want to show color swatches of those blocks who declare as static block in cms home page – Niraj Patel Sep 13 '20 at 20:07
  • @HerveTribouilloy again ,If my all static block declare in cms page,that all static blocks display products in home page but issue is products not showing with color swatches so I got solution to display color swatches declare block and its sub block in layout xml file. QUESTION IS HOW CAN I DECLARE SUB BLOCK IN STATIC BLOCK LIKE ABOVE LAYOUT XML FILE ? – Niraj Patel Sep 13 '20 at 20:13
  • Got it, thanks for the clarification – Herve Tribouilloy Sep 14 '20 at 5:14
1
+100

The below does render some product swatches for the product in the listing.

     <?php

namespace Mbs\HomeProducts\Block;

use Magento\Swatches\Block\Product\Renderer\Configurable;

class LatestList extends \Magento\Catalog\Block\Product\ListProduct
{
    public function getCategoryId()
    {
        return 12;
    }

    /**
     * <block class="Magento\Framework\View\Element\RendererList" name="category.product.type.details.renderers" as="details.renderers">
    <block class="Magento\Framework\View\Element\Template" name="category.product.type.details.renderers.default" as="default"/>
    <block class="Magento\Swatches\Block\Product\Renderer\Listing\Configurable" name="category.product.type.details.renderers.configurable" as="configurable" template="Magento_Swatches::product/listing/renderer.phtml" ifconfig="catalog/frontend/show_swatches_in_product_list" />
    </block>
     *
     * @return LatestList|void
     * @throws \Magento\Framework\Exception\LocalizedExceptioN
     */
    protected function _prepareLayout()
    {
        $this->addChild(
            'details.renderers',
            \Magento\Framework\View\Element\RendererList::class
        );

        $this->getLayout()->getBlock($this->getNameInLayout() . '.details.renderers')
            ->addChild('default', \Magento\Framework\View\Element\Template::class);

        $configurableRenderer = $this->getLayout()->getBlock($this->getNameInLayout() . '.details.renderers')
            ->addChild('configurable', \Magento\Swatches\Block\Product\Renderer\Listing\Configurable::class);
        $configurableRenderer->setTemplate('Magento_Swatches::product/listing/renderer.phtml');

        parent::_prepareLayout();
    }
}
10
  • let me try and then let you know – Niraj Patel Sep 14 '20 at 20:50
  • you did not create this block in block file <referenceBlock name="homepage.swatchrenderer"> <block class="Magento\Swatches\Block\Product\Renderer\Listing\Configurable" as="configurable" template="Magento_Swatches::product/listing/renderer.phtml" /> </referenceBlock> – Niraj Patel Sep 14 '20 at 23:01
  • yes if possible but not mandaotry – Niraj Patel Sep 15 '20 at 14:15
  • it still not working but I appreciated your effort and bytheway I accept your answer – Niraj Patel Sep 15 '20 at 14:46
  • ok sure Please check in weekend,thanks again – Niraj Patel Sep 15 '20 at 17:06

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.