1

I have create one popup. In popup get configurable product collection and it working fine. But i have faced one issue. In configurable products collection configurable product options not showing. Like Color and Size. So how to show configurable product options html in popup.

Updated Code :-

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');
$productCollection->addAttributeToSelect('*');
$productCollection->addAttributeToFilter(
    [
       ['attribute' => 'sku', 'in' => array($productMains)]
    ]
);
$collection = $productCollection->load();

Please Help me.

THANKS.

1
  • Please specify your requirement clearly.
    – ZealousWeb
    Jul 26 at 11:10
2
+50

Add to Your phtml file where you want to show Configurable product Swatches.

/* For Option show Like Listing */
<?= $this->getLayout()->createBlock(\Magento\Swatches\Block\Product\Renderer\Listing\Configurable::class)->setTemplate("Magento_Swatches::product/listing/renderer.phtml")->setProduct($product)->toHtml(); ?>

/* For Option show Like Product View page  */
<?= $this->getLayout()->createBlock(\Magento\Swatches\Block\Product\Renderer\Configurable::class)->setTemplate("Magento_Swatches::product/view/renderer.phtml")->setProduct($product)->toHtml(); ?>

Note: Here $product is Configurable product object comes from Magento\Catalog\Model\ProductFactory. after add code please run cache clean command php bin/magento c:c

Please check magento Magento_Swatches module in Vendor Folder path root/vendor/magento/module-swatches

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');
$productCollection->addAttributeToSelect('*');
$productCollection->addAttributeToFilter(
    [
       ['attribute' => 'sku', 'in' => array($productMains)]
    ]
);
$collection = $productCollection->load();

foreach ($collection as $key => $value) {
    $product = $objectManager->create('Magento\Catalog\Model\Product')->load($value->getId());
    echo $this->getLayout()->createBlock(\Magento\Swatches\Block\Product\Renderer\Listing\Configurable::class)->setTemplate("Magento_Swatches::product/listing/renderer.phtml")->setProduct($product)->toHtml();
    echo "<br>";
}
15
1

You should extend your custom block to \Magento\Catalog\Block\Product\ListProduct like below and load your custom collection as per you want in block

<?php
namespace PackageName\HomeContent\Block;

use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use Magento\Catalog\Model\Product\Visibility;
use Magento\Framework\Stdlib\DateTime\DateTime;

class NewProductCollection extends \Magento\Catalog\Block\Product\ListProduct
{
    protected $_productCollectionFactory;

    protected $_catalogProductVisibility;

    protected $_date;
    
    protected $_storeManager;

    public function __construct(
        \Magento\Catalog\Block\Product\Context $context,
        \Magento\Framework\Data\Helper\PostHelper $postDataHelper,
        \Magento\Catalog\Model\Layer\Resolver $layerResolver,
        \Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository,
        \Magento\Framework\Url\Helper\Data $urlHelper,
        CollectionFactory $productCollectionFactory,
        Visibility $catalogProductVisibility,
        DateTime $dateTime,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        array $data = []
    ) {
        $this->_productCollectionFactory = $productCollectionFactory;
        $this->_catalogProductVisibility = $catalogProductVisibility;
        $this->_date                     = $dateTime;
        $this->_storeManager = $storeManager;
        parent::__construct($context, $postDataHelper, $layerResolver,
            $categoryRepository, $urlHelper, $data);
    }

    public function getLoadedProductCollection()
    {
        $visibleProducts = $this->_catalogProductVisibility->getVisibleInCatalogIds();
        $collection = $this->_productCollectionFactory->create()->setVisibility($visibleProducts);
        $collection = $collection->addAttributeToFilter('type_id', 'configurable')->setPageSize(10);
        return $collection;
    }
}

Now in your custom phtml file copy content from vendor/magento/module-catalog/view/frontend/templates/product/list.phtml

Please check.

Cheers!

5
  • i have already tried this solution but my question is how to get configurable products collection with its options.like Color and Size. Jul 26 at 10:59
  • In list page of your website, color and size options are showing or not? Like this - prnt.sc/1g4izux
    – ZealousWeb
    Jul 26 at 11:03
  • You have asked in question like you have already got configurable products collection and you are just facing issue with color and size option
    – ZealousWeb
    Jul 26 at 11:04
  • Please edit your question.
    – ZealousWeb
    Jul 26 at 11:05
  • Please copy content from vendor/magento/module-catalog/view/frontend/templates/product/list.phtml from your custom phtml file. I have edited answer and shown you that how to get configurable products collection. Your custom block needs to extend with \Magento\Catalog\Block\Product\ListProduct. It will display configurable product options as well.
    – ZealousWeb
    Jul 26 at 11:09

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.