#StackBounty: #magento2 #product #product-collection #product-list Display Product collection issue in Magento 2

Bounty: 50

I have a product collection like below.

 <ol class="products list items product-items">
 <?php 
 $productCollectionSku = array('111', '222', '333', '444', '555', '666'); // these are the comma separated skus.

        foreach($productCollectionSku as  $productSku){ 
            $product =  $block->getProductBySku($productSku);
            $productName = $product->getName();
            $shortDescription = $product->getshortDescription();
            $productUrl = $product->getProductUrl();
            $productId = $product->getId(); 

            $productPrice = $product->getPrice();   
            $addtoCartUrl = $block->getAddToCartUrlForProduct($productSku);
        ?>
        <li class="item product product-item">              
            
" class="product photo product-item-photo" tabindex="-1"> " width="240" height="300" alt="">
">
<form action="<?php echo $addtoCartUrl; ?>" method="post" class="best-items"> <?php echo $block->getBlockHtml('formkey')?> <input type="hidden" name="productId" value="<?php echo $productId; ?>"> <button type="submit" title="Add to Cart" class="action tocart primary"> <span>Add to Cart</span> </button> </form> </div> </div> </li> <?php } ?> </ol>

Here is my getProductBysku function

  public function getProductBySku($sku)
   {
    return $this->productRepository->get($sku);// injecting this class in my block file MagentoCatalogApiProductRepositoryInterface $productRepository,

   }

The above code working fine if all of the SKUs are simple products,

I need to prepare for configurable products also. Can we get the product info
and display here config products with the variation items.

All I am looking here is how to display configurable products here like in the category list page.

Where all the color and size option shows with clickable option.

Can anyone help me to implement this one?

Thanks in Advance!!


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.