Php 在magento2模块中获取产品列表
我正在尝试将所有产品列表提取到magento2应用程序中的模块中,但不知道如何提取 我的区块码:Php 在magento2模块中获取产品列表,php,magento,magento2,Php,Magento,Magento2,我正在尝试将所有产品列表提取到magento2应用程序中的模块中,但不知道如何提取 我的区块码: class Crud extends \Magento\Framework\View\Element\Template { protected $objectManager; public function __construct(Magento\Framework\View\Element\Template\Context $context, \Magento\Fr
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>
我是magento的新手,如果我从代码中删除构造函数并删除方法
getProductList()
,则块代码可以正常工作。我可以在magento块中编写代码。您忘记了声明函数的可见性。这可能会解决你的问题。如果没有,请包括布局xml文件、文件顶部的名称空间和控制器
更改:
函数getProducts(){
到公共函数getProducts(){
function\u prepareLayout(){}
toprotectedfunction\u prepareLayout(){}
函数getCrudName(){
到公共函数getCrudName(){
函数getProductList(){
到公共函数getProductList(){
您忘记声明函数的可见性。这可能会解决您的问题。如果没有,请包括布局xml文件、文件顶部的命名空间和控制器
更改:
函数getProducts(){
到公共函数getProducts(){
function\u prepareLayout(){}
toprotectedfunction\u prepareLayout(){}
函数getCrudName(){
到公共函数getCrudName(){
函数getProductList(){
到公共函数getProductList(){
我尝试更改可见性。但我认为问题不在那里。对于其他方法,如getCrudName()
,该块工作正常。如果getProductList(),视图将正确呈现
未调用。我认为获取产品列表的逻辑或实现存在问题。我发现了另一个错误:$objectMan=$objectManager;
应该是$objectMan=$this->objectManager;
这解决了什么问题吗?另外请注意,如果函数工作,返回的对象将是巨大的。不,我仍然得到一个异常为3异常:异常0(Magento\Framework\exception\LocalizedException):无效块类型:Holmes\ChatClient\block\Crud异常1(ReflectionException):类Holmes\ChatClient\block\Magento\Framework\View\Element\Template\Context不存在异常2(ReflectionException):Class Holmes\ChatClient\Block\Magento\Framework\View\Element\Template\Context不存在
(Magento\Framework\View\Element\Template\Context$Context,
您应该将其更改为(\Magento\Framework\View\Element\Template\Context$Context,
然后在您的Magento根文件夹中运行php-bin/Magento安装程序:di:compile&&chmod-R 777 var
。奇怪的是,如果您不调用getProductList()
。我尝试更改可见性。但我认为问题不存在。对于其他方法,如getCrudName()
,该块工作正常。如果getProductList(),则视图会正确呈现
未调用。我认为获取产品列表的逻辑或实现存在问题。我发现了另一个错误:$objectMan=$objectManager;
应该是$objectMan=$this->objectManager;
这解决了什么问题吗?另外请注意,如果函数工作,返回的对象将是巨大的。不,我仍然得到一个异常为3异常:异常0(Magento\Framework\exception\LocalizedException):无效块类型:Holmes\ChatClient\block\Crud异常1(ReflectionException):类Holmes\ChatClient\block\Magento\Framework\View\Element\Template\Context不存在异常2(ReflectionException):Class Holmes\ChatClient\Block\Magento\Framework\View\Element\Template\Context不存在
(Magento\Framework\View\Element\Template\Context$Context,
您应该将其更改为(\Magento\Framework\View\Element\Template\Context$Context,
然后在Magento根文件夹中运行php-bin/Magento安装程序:di:compile&&chmod-R 777 var
。奇怪的是,如果不调用getProductList()
,它会工作。
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