【发布时间】:2016-12-07 16:17:48
【问题描述】:
我正在尝试将所有产品列表获取到我在 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 块中编写代码吗?
【问题讨论】: