当前位置: 技术文章>> magento2中的范围组件以及代码示例

文章标题:magento2中的范围组件以及代码示例
  • 文章分类: Magento
  • 10810 阅读
系统学习magento二次开发,推荐小册:《Magento中文全栈二次开发 》

本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。


在Magento 2中,您可以使用范围组件来限制搜索、过滤和排序操作的范围。以下是一个基本的范围示例:

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
class Scope extends Action
{
    protected $resultPageFactory;
    protected $collectionFactory;
    public function __construct(Context $context, PageFactory $resultPageFactory, CollectionFactory $collectionFactory)
    {
        $this->resultPageFactory = $resultPageFactory;
        $this->collectionFactory = $collectionFactory;
        parent::__construct($context);
    }
    public function execute()
    {
        $resultPage = $this->resultPageFactory->create();
        $resultPage->getConfig()->getTitle()->set(__('Products'));
        $collection = $this->collectionFactory->create();
        $collection->addAttributeToSelect('*');
        $collection->addAttributeToFilter('status', \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);
        $collection->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH);
        $categoryId = $this->getRequest()->getParam('category_id');
        if ($categoryId) {
            $category = $this->_objectManager->create('\Magento\Catalog\Model\Category')->load($categoryId);
            $collection->addCategoryFilter($category);
            $resultPage->getConfig()->getTitle()->prepend(__($category->getName()));
        }
        $searchQuery = $this->getRequest()->getParam('q');
        if ($searchQuery) {
            $collection->addAttributeToFilter('name', array('like' => '%'.$searchQuery.'%'));
            $resultPage->getConfig()->getTitle()->prepend(__('Search results for: "%1"', $searchQuery));
        }
        $sortOrder = $this->getRequest()->getParam('order');
        if ($sortOrder == 'price_asc') {
            $collection->addAttributeToSort('price', 'asc');
        } elseif ($sortOrder == 'price_desc') {
            $collection->addAttributeToSort('price', 'desc');
        } elseif ($sortOrder == 'name_asc') {
            $collection->addAttributeToSort('name', 'asc');
        } elseif ($sortOrder == 'name_desc') {
            $collection->addAttributeToSort('name', 'desc');
        }
        $pageSize = $this->getRequest()->getParam('limit');
        if ($pageSize) {
            $collection->setPageSize($pageSize);
        }
        $currentPage = $this->getRequest()->getParam('page');
        if ($currentPage) {
            $collection->setCurPage($currentPage);
        }
        $resultPage->getLayout()->getBlock('product_list')->setCollection($collection);
        return $resultPage;
    }
}

在此示例中,我们使用了CollectionFactory来创建一个Product集合,并使用addAttributeToSelectaddAttributeToFiltersetVisibility等方法来设置搜索和过滤条件。我们随后检查了请求中是否包含分类ID和搜索查询,并使用addCategoryFilteraddAttributeToFilter方法来根据需要过滤结果。我们还检查了请求中是否包含排序、分页和结果限制参数,并相应地设置了集合的排序、分页和结果限制属性。最后,我们将集合传递给结果页面,以便在布局中渲染结果。


推荐文章