0

I need to display product collection in home page in magento2.

Thanks.

0

In Layout file:

<container name="home.lookbook" htmlTag="div" htmlClass="lookbook-block home-cards">
                <block class="Magento\Framework\View\Element\Template" name="lookbook"
                       template="Magento_Theme::html/home/lookbook.phtml"/>
            </container>

In Template file:

<?php
$collection = $this->getProductDetails();
$productCollection = $collection['collection'];
$categoryName = $collection['name'];
if (count($productCollection)) {
    ?>
    <div class="shy-block mgb20 product-recently">
        <div class="most-viewed">
            <div class="lg-title">
                <?php /* @escapeNotVerified */ echo $categoryName; ?>
            </div>

            <div class="product-carousel" data-mage-init='{"viewPageSlickCarousel": {}}'>

                <?php
                foreach ($productCollection as $product) {
                    $productName = $product->getName();
                    $productUrl = $block->productRender($product);
                    $templateType = \OX\ProductReview\Block\Product\ReviewRenderer::STAR_VIEW;
                    $image = 'upsell_products_list';
                    ?>
                    <?php if ($product->isSaleable()): ?>

                        <div class="product-carousel-item text-center">
                            <div class="product-carousel-item-image">
                                <!--To display Product label -->
                                <?php
                                $productAttributeCode = $this->getAttributeCode($product);
                                if (!empty($productAttributeCode)) {
                                    $attributeOptionlabel = $product->getAttributeText($productAttributeCode);
                                    if (!empty($attributeOptionlabel)) {
                                        $attributeOptionText = strtolower($attributeOptionlabel);
                                        $optionLabel = str_replace(" ", "-", $attributeOptionText);
                                        $mediaUrl = $block->getMediaUrl();
                                        $productLabelUrl = $mediaUrl . 'attribute/options/' . $optionLabel . '.png';
                                        ?>                                        
                                        <div class="product-label shy-product-label">
                                            <img src="<?php echo $productLabelUrl ?>" alt="<?php echo $optionLabel ?>">
                                        </div>
                                        <?php
                                    }
                                }
                                ?>
                                <!-- To display Product label  End -->

                                <a href="<?= $productUrl ?> ">
                                    <?= $block->getImage($product, $image)->toHtml(); ?>
                                </a>
                                <?= $block->getReviewsSummaryHtml($product, $templateType) ?>
                            </div>
                            <div class="product details product-carousel-item-details">
                                <div class="product-carousel-item-name">
                                    <a class="" href="<?= $productUrl ?>">
                                        <?= $productName ?>                        
                                    </a>
                                </div>
                                <?= /* @escapeNotVerified */ $block->getProductPrice($product) ?>
                                <div class="product-carousel-item-inner">
                                    <div class="product actions product-item-actions">
                                        <div class="actions-primary">
                                            <?php $controller = $block->getUrl('ajaxcart/index/productdata/') ?>
                                            <form data-product-type="<?= /* @NoEscape */ $product->getTypeId() ?>" data-role="tocart-form"  data-url="<?php echo $controller ?>"  data-product-name="<?= /* @NoEscape */ $product->getName() ?>" data-product-sku="<?= /* @NoEscape */ $product->getSku() ?>" action="<?= $block->getUrl("checkout/cart/add") ?>" method="post" >
                                                <input type="hidden" value="<?= $product->getId() ?>" name="product">
                                                <input type="hidden" value="<?= $block->getEncodeUrl() ?>" name="uenc">
                                                <?= $block->getBlockHtml('formkey') ?>
                                                <button type="submit" 
                                                        title="<?= $block->escapeHtml(__('Add to Cart')) ?>"
                                                        class="action tocart primary">
                                                    <span><?= /* @escapeNotVerified */ __('Add to Cart') ?></span>
                                                </button>
                                            </form>

                                        </div>
                                    </div>
                                </div>
                            </div>
                            <!--- Item end -->
                        </div>
                        <?php
                    endif;
                }
            }
            ?>

        </div>
    </div>
</div>
<script type="text/x-magento-init">
    {
    "[data-role=tocart-form], .form.map.checkout": {
    "productInfoPopup": {}
    }
    }
</script>

<div id="product-popup"> </div>

And also refer Magento 2 colour swatches not showing on homepage

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.