0

I need to show the Configurable Product Swatches in Home page by CMS block.

Tried using -:

https://github.com/zaibi099/Magento/tree/master/Pakgentors/Swatches

Its working fine with XML layout, but not showing swatches when I try to call it from CMS block.

2 Answers 2

0

Showing Configurable Swatches in Home Page

Here, You can try the following method, For me it's working

  1. In Admin->content->pages
  2. select Home Page and edit
  3. 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);


3
  • Thanks For the reply... This is not the solution i am looking for..it work same as that XML i have tried.. i need to get it from Static CMS block .. prnt.sc/il8xxv Commented Mar 1, 2018 at 7:38
  • **In CMS HomePage-> Layout Update->add this code only, it will work **<referenceContainer name="content"> <container name="home-page" htmlTag="div" htmlClass="home-page"> <block name="homepage.toprenderers"> <block class="Magento\Swatches\Block\Product\Renderer\Listing\Configurable" as="configurable" template="Magento_Swatches::product/listing/renderer.phtml" /> </block> </container> </referenceContainer> Commented Mar 1, 2018 at 7:56
  • No.. Its not Working.. Commented Mar 1, 2018 at 9:16
0

**In CMS HomePage-> Layout Update->add this code only, Hopefully it will work **

<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="Pakgentors\Swatches\Block\Productslist" name="productlist" template="Pakgentors_Swatches::products.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>
2
  • i tried this before posting.. its working.. But its on CMS page and it's calling the .phtml file in XML itself..its not possible for my condition.. I need to get this via Admin> Content > CMS > Static block.. already share the screen -> prnt.sc/il8xxv Commented Mar 1, 2018 at 10:04
  • @SUBINCHANDRAN you need to create a block else it won't come, In that block you can give condition, what product should come in product collections Commented Mar 1, 2018 at 10:14

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.