我正在尝试将所有产品列表提取到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中编写代码。
答案 0 :(得分:0)
您忘记了声明功能的可见性。这可能会解决您的问题。如果没有,请包括您的布局xml文件,文件顶部的命名空间和控制器。
更改:
function getProducts(){
至public function getProducts() {
function _prepareLayout(){}
至protected function _prepareLayout(){}
function getCrudName(){
至public function getCrudName(){
function getProductList(){
至public function getProductList(){
。