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