类别页面(前端)的产品网格是通过catalog_category_view.xml中的布局呈现的。
可以说我有一个自定义产品集合(通过
ProductRepositoryInterface::getList($searchCriteria) method
在自定义块类中并希望呈现此集合。呈现的结果应在前端显示为产品网格(就像任何类别页面一样)。
如何才能做到这一点 ?
通过查看catalog_category_view.xml
有两条重要的线,它们负责呈现产品集合:
<block class="Magento\Catalog\Block\Category\View" name="category.products" template="Magento_Catalog::category/products.phtml">
<block class="Magento\Catalog\Block\Product\ListProduct" name="category.products.list" as="product_list" template="Magento_Catalog::product/list.phtml">
如何为这些模板文件提供自定义产品集合,以便它们呈现我的集合?
如果我错了,请纠正我。
这是我的代码块的样子:
<?php
namespace Mod\Mod1\Block;
use Magento\Framework\View\Element\Template;
class Main extends Template
{
protected $_filterBuilder;
protected $_filterGroupArray;
protected $_filterGroupBuilder;
protected $_searchCriteriaBuilder;
protected $_productRepository;
protected $_productFactory;
protected $_list;
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
\Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder,
\Magento\Framework\Api\Search\FilterGroupBuilder $filterGroupBuilder,
\Magento\Framework\Api\FilterBuilder $filterBuilder,
\Magento\Catalog\Model\ProductFactory $productFactory,
array $data = [])
{
$this->_productRepository = $productRepository;
$this->_searchCriteriaBuilder = $searchCriteriaBuilder;
$this->_filterGroupBuilder = $filterGroupBuilder;
$this->_filterBuilder = $filterBuilder;
parent::__construct($context, $data);
}
public function getLoadedProductCollection(){
$searchCrit = $this->buildSearchCriteria('','','','','','5-',1);
$list = $this->_productRepository->getList($searchCrit);
return $list;
}
public function buildSearchCriteria(...){
....
return $searchCriteria;
}
}