简体   繁体   English

产品集合未在前端显示产品

[英]Product collection is not showing Products on frontend

here is my block file: path:-Vendor\Module\Block这是我的块文件:路径:-Vendor\Module\Block

<?php 

namespace Vendor\Module\Block;

class Product extends \Magento\Framework\View\Element\Template
{
    protected $_productCollectionFactory ;

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

    public function getProductCollection()
    {
        $collection = $this->_productCollectionFactory->create();
        $collection->addAttributeToSelect('*');
        return $collection;
    }
}

here is my template file: path:-Vendor\Module\view\frontend\templates这是我的模板文件:路径:-Vendor\Module\view\frontend\templates

<?php 

    $collection = $block->getProductCollection();
    print_r($collection);
?>

can any one help me with this part?有人可以帮我做这部分吗? Thanks in Advance!提前致谢!

<?php
$collection = $block->getProductCollection();
foreach($collection as $product){
    print_r($product->getData());
    echo "<br>";
}

Try $block->getProductCollection()->getItems() in template.在模板中尝试 $block->getProductCollection()->getItems()。 It returns an array of products.它返回一个产品数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM