0

I have a custom phtml and block file. I have called the phtml page in my cms home page.I want to display the product image, name ,price.I have dispalyed the name and price.But Iam not able to display product image.The path of the image is getting as /T/G/TGM-35SD.jpg

This is my block function

public function getProductCollection()
        {
           $todayDate = date('Y-m-d');
           $limit = 5;
           $collection = $this->_productCollectionFactory->create();
           $collection->setVisibility($this->_catalogProductVisibility->getVisibleInCatalogIds());
           $collection->addAttributeToSelect('*');
           $collection->addAttributeToFilter(
                'news_from_date',
                [
                    'or' => [
                        0 => ['date' => true, 'to' => $todayDate],
                        1 => ['is' => new \Zend_Db_Expr('null')],
                    ]
                ])->addAttributeToFilter(
                [
                    ['attribute' => 'news_from_date', 'is' => new \Zend_Db_Expr('not null')],
                ]
            );
           $collection->getSelect()->orderRand()->limit($limit);

           return $collection;
        }

This is my phtml.

<?php 

 $collection = $block->getProductCollection();?>
<div class="products wrapper products">
<?php $iterator = 1; ?>
<ol class="products list items product-items">
<?php foreach($collection as $product){ ?>
<?php /* @escapeNotVerified */ echo($iterator++ == 1) ? '<li class="item product product-item">' : '</li><li class="item product product-item">' ?>
   <div class="product-item-info" data-container="product-grid">
                    <?php
                    $productImage = $product->getImage();

                    ?>

                    <a href="<?php /* @escapeNotVerified */ //echo $product->getProductUrl() ?>" class="product photo product-item-photo" tabindex="-1">
                        <?php //echo $productImage->toHtml(); ?>
                    </a>
                    <div class="product details product-item-details">
                        <?php
                            $_productNameStripped = $product->getName();
                            echo $_productNameStripped;
                        ?>
                        <strong class="product name product-item-name">
                            <a class="product-item-link"
                               href="#">

                            </a>
                        </strong>
                        <?php //echo $block->getReviewsSummaryHtml($_product, $templateType); ?>
                        <?php /* @escapeNotVerified */ echo $product->getPrice(); ?>
                        <?php //echo $block->getProductDetailsHtml($_product); ?>


                    </div>
                </div>
                <?php echo($iterator == count($collection)+1) ? '</li>' : '' ?>
    <?php //echo $product->getName();echo '<br>';?>
    <?php //echo $product->getPrice();?>


    <?php } ?>
</ol>     
</div>

Please help.

1

You have to just use,

$imageBlock = $block->getLayout()->createBlock('Magento\Catalog\Block\Product\ListProduct');
$productImage = $imageBlock->getImage($product, 'category_page_list');

Now php code look like,

<?php 
 $collection = $block->getProductCollection();
 $imageBlock = $block->getLayout()->createBlock('Magento\Catalog\Block\Product\ListProduct');
 ?>
<div class="products wrapper products">
<?php $iterator = 1; ?>
<ol class="products list items product-items">
<?php foreach($collection as $product){ ?>
<?php /* @escapeNotVerified */ echo($iterator++ == 1) ? '<li class="item product product-item">' : '</li><li class="item product product-item">' ?>
   <div class="product-item-info" data-container="product-grid">
                    <?php
                        $productImage = $imageBlock->getImage($product, 'category_page_list');
                    ?>
                     <a class="product-image" href="<?php echo $product->getProductUrl() ?>"><?php echo $productImage->toHtml()  ?></a>
                    <div class="product details product-item-details">
                        <?php
                            $_productNameStripped = $product->getName();
                            echo $_productNameStripped;
                        ?>
                        <strong class="product name product-item-name">
                            <a class="product-item-link"
                               href="#">

                            </a>
                        </strong>
                        <?php //echo $block->getReviewsSummaryHtml($_product, $templateType); ?>
                        <?php /* @escapeNotVerified */ echo $product->getPrice(); ?>
                        <?php //echo $block->getProductDetailsHtml($_product); ?>


                    </div>
                </div>
                <?php echo($iterator == count($collection)+1) ? '</li>' : '' ?>
    <?php //echo $product->getName();echo '<br>';?>
    <?php //echo $product->getPrice();?>
  • @E.B You can close this question after accept as answer so other user have useful. – Rakesh Jesadiya Mar 27 '17 at 7:17

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.