Skip to content

Magento 2: how to get swatch options in custom theme product list on home page?

i have created a custom theme, i listed the products in home page, now i need to show the swatch option. i used this code inside the div of my phtml file, still the option is not visible

`       <div class="swatch-options-placeholder">
                                                        <?php
                                                        if ($product->getTypeId() == MagentoConfigurableProductModelProductTypeConfigurable::TYPE_CODE) {
                                                            $swatchBlock = $this->getLayout()->createBlock(MagentoSwatchesBlockProductRendererListingConfigurable::class);
                                                           echo $swatchBlock->setProduct($product)->toHtml();
                                                        }
                                                        ?>

                                                    </div>`