0

我正在为 magento 2.3 创建我的自定义模块,但我遇到了小部件的问题。我已经创建了我的小部件类型,当我将它包含到主页时,我必须查看我选择呈现的产品的产品列表。我想使用 .../magento/vendor/magento/module-catalog/view/frontend/templates/product/list.phtml 作为模板

问题是 Widget 类必须扩展 Magento\Framework\View\Element\Template 类,并且要呈现 ProductList 我必须扩展 Magento\Catalog\Block\Product\ListProduct。

也许有人可以给我一些建议如何解决这个问题。

这是我的代码的一部分

文件 my-vendor/my-module/Block/Widget/MyWidget.php

class PromotedWidget extends Template implements BlockInterface
{
    public function __construct(
        Context $context,
      \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
         array $data = []
    )
    {
        $this->productCollectionFactory = $productCollectionFactory;
        parent::__construct($context, $data);
    }

    public function getLoadedProductCollection()
    {
        $collection = $this->productCollectionFactory->create();
        $collection->addAttributeToSelect('*');
        $collection->addAttributeToFilter('necessary_attribute', ['necessary_attribute' => 'attr_value']);
        return $collection;
    }

    public function toHtml()
    {
        $this->setTemplate('Magento_Catalog::product/list.phtml');
        return parent::_toHtml();
    }
}
4

1 回答 1

0

我认为扩展 Magento\Catalog\Block\Product\ListProduct 类不是一个好习惯。如果您想加载自定义产品集合并希望它与产品列表页面一样工作,那么我建议您在自定义小部件中为 Magento\CatalogSearch\Model\Search\IndexBuilder 类创建一个插件,如下所示:

命名空间\模块名\etc\di.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\CatalogSearch\Model\Search\IndexBuilder">
        <plugin name="Namespace_Modulename::custom_collection" type="Namespace\Modulename\Plugin\CatalogSearch\Model\Search\IndexBuilder" />
    </type>
</config>

命名空间\模块名称\插件\目录搜索\模型\搜索\IndexBuilder.php

<?php
namespace Namespace\Modulename\Plugin\CatalogSearch\Model\Search;

use Magento\Framework\Search\RequestInterface;

class IndexBuilder {    
    protected $_request;
    protected $_categoryHelper;

    public function __construct(
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
        \Magento\Catalog\Helper\Category $categoryHelper,
        \Magento\Framework\App\Request\Http $request
    ) {
        $this->_productCollectionFactory    = $productCollectionFactory;
        $this->_categoryHelper              = $categoryHelper;
        $this->_request                     = $request;
    }

    public function aroundBuild($subject, callable $proceed, RequestInterface $request) {
        $select = $proceed($request);

        $collection = $this->_productCollectionFactory->create();
        $collection->addAttributeToSelect('*');
        $collection->addAttributeToFilter('necessary_attribute', ['necessary_attribute' => 'attr_value']);
        $getProductAllIds = $collection->getAllIds();
        $productUniqueIds = array_unique($getProductAllIds);
        $select->where("search_index.entity_id IN ('" . join("','", $productUniqueIds) . "')");     
        return $select;
    }
}

此外,您可以参考链接了解更多详情。

于 2019-01-22T05:21:03.387 回答