0

I am new to Magento2 and working on the custom theme. I am trying to include product image, price, and name but they are not displaying.

This is my code.

<?php
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $abstractProductBlock = $block->getLayout()->createBlock('\Magento\Catalog\Block\Product\AbstractProduct');
    $productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
    $collection = $productCollection->create()
                ->addAttributeToSelect('*')
                ->addAttributeToSort('created_at', 'DESC')
                ->setPageSize(8)
                ->load();
?>

<div class="productinfo text-center">
          <?php foreach ($collection as $product) :?>
      <img src=<?php echo $abstractProductBlock->getImage($product, 'latest_collection_list')->getImageUrl(); ?>" alt="<?php echo $product->getName(); ?>" />
        <h2>$56</h2>
        <p>Easy Polo Black Edition</p>
         <a href="<?php echo $product->getProductUrl(); ?>"> class="btn btn-default add-to-cart"><i class="fa fa-shopping-cart"></i>Add to cart</a>
</div>
<div class="product-overlay">
    <div class="overlay-content">

 <h2><?php echo $abstractProductBlock->getProductPrice($product) ?></h2>
 <?php endforeach;  ?>

Products are not showing but there is no error.

0

Do not use objectmanager it's not Magento standard. Make one block and get the collection from there.

<?php
namespace Test\test\Block;
class Test extends \Magento\Framework\View\Element\Template
{    
    protected $_productCollectionFactory;
protected $_imageHelper;

public function __construct(
    \Magento\Backend\Block\Template\Context $context,        
    \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
    \Magento\Catalog\Helper\Image $imageHelper,
    array $data = []
)
{    
    $this->_productCollectionFactory = $productCollectionFactory;
    $this->_imageHelper = $imageHelper;
    parent::__construct($context, $data);
}

public function getProductCollection()
{
    $collection = $this->_productCollectionFactory->create();
    $collection->addAttributeToSelect('*');
    $collection->setPageSize(3);        
    return $collection;
}
public function getImageUrl($product) {
    $image_url = $this->_imageHelper->init($product, 'product_page_image_small')->setImageFile($product->getFile())->resize(200, 200)->getUrl();
    return $image_url;
}

?>

Now you can get your collection in your template file like test.phtml below.

<?php
$productCollection = $block->getProductCollection();
foreach ($productCollection as $product) {    
    echo $product->getName().' => ';    
    echo $product->getPrice().' => ';
    echo $block->getImageUrl($product);
    echo '<br>';
}
die;
?>

Still if you facing any problem let me know.

| improve this answer | |

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.