繁体   English   中英

在magento2模块中获取产品列表

[英]Getting product list in a magento2 module

我正在尝试将所有产品列表提取到magento2应用程序中的模块中,但无法弄清楚如何。

我的区块代码:

class Crud extends \Magento\Framework\View\Element\Template
{
    protected $objectManager;
    public function __construct(Magento\Framework\View\Element\Template\Context $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory,
        \Magento\Framework\ObjectManagerInterface $objectManager,
        \Magento\Framework\App\State $appState
    )
    {
        $this->objectManager = $objectManager;
      //  $appState->setAreaCode('frontend');
        parent::__construct($context);
    }

    function _prepareLayout(){}

    function getCrudName(){
        return "Products for holmes chat client";
    }

    function getProductList(){
     //   $objectMan= new \Magento\Framework\App\ObjectManager;
        $objectMan= $this->$objectManager;
        $repo = $objectMan->get('Magento\Catalog\Model\ProductRepository');
        $search_criteria = $objectMan->create(
            'Magento\Framework\Api\SearchCriteriaInterface'
        );
        $result = $repo->getList($search_criteria);
      //  $list = $repo->getList();
      $products = $result->getItems();
        return $products;
        // return 'some';
    }

    function getProducts(){
        $objectMan = new Holmes\ChatClient\Api\ProductFetcher;
    }
}

我的模板代码:

<h2>This is a crud html</h2>
<h3>

    <?php
        echo $block->getCrudName();
    ?>
</h3>
<ul>
    <?php
       echo $block->getProductList();
    ?>
</ul>

我的浏览器抛出错误:

3 exception(s):
Exception #0 (Magento\Framework\Exception\LocalizedException): Invalid block type: Holmes\ChatClient\Block\Crud
Exception #1 (ReflectionException): Class Holmes\ChatClient\Block\Magento\Framework\View\Element\Template\Context does not exist
Exception #2 (ReflectionException): Class Holmes\ChatClient\Block\Magento\Framework\View\Element\Template\Context does not exist

我是magento的新手,如果删除构造函数并从代码中删除方法getProductList() ,则块代码正在工作。 我可以在magento Block中编写代码吗?

你忘了宣布功能的可见性。 这可能会解决您的问题。 如果没有,请包括您的布局xml文件,文件顶部的命名空间和控制器。

更改:

function getProducts(){ to public function getProducts() {

function _prepareLayout(){}protected function _prepareLayout(){}

function getCrudName(){ to public function getCrudName(){

function getProductList(){ to public function getProductList(){

暂无
暂无

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

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