浏览文章
文章信息
Magento2 自定义产品排序(带分页)畅销,热销,最新,浏览最多| Magento2 custom product sorting with pagenation
12582
主要程序:
di.xml依赖中注入插件:
... <type name="Magento\Catalog\Model\Config\Source\ListSort"> <plugin name="Aiweline_ProductSorting::system_config_source_list_sort" type="Aiweline\ProductSorting\Plugin\Model\Config\Source\ListSort" /> </type> ...插件类:
<?php /** * @DESC | 文件描述 * * @Author 秋枫雁飞/Aiweline * @Email aiweline@qq.com * @Company 成都潮派元素科技 * */ namespace Aiweline\ProductSorting\Plugin\Product\ProductList; use Aiweline\ProductSorting\Helper\Data; use Magento\Framework\App\ResourceConnection; use Magento\Framework\DB\Select; class Toolbar { private $_subQueryApplied = false; private $_collection = null; private $_conn = null; /** * Constructor * * @param \Magento\Framework\App\ResourceConnection $resource */ public function __construct(ResourceConnection $resource) { $this->_conn = $resource->getConnection('catalog'); } /** * @DESC | 环绕 设置集合 * * @Author 秋枫雁飞/Aiweline * @Email aiweline@qq.com * @Company 成都潮派元素科技 * * @参数区 * @param \Magento\Catalog\Block\Product\ProductList\Toolbar $subject * @param \Closure $proceed * @param $collection * @return mixed */ public function aroundSetCollection( \Magento\Catalog\Block\Product\ProductList\Toolbar $subject, \Closure $proceed, $collection ) { $this->_collection = $collection; $currentOrder = $subject->getCurrentOrder(); // 判断当前排序 if ($currentOrder) { /**@var $subject_collection \Magento\Catalog\Model\ResourceModel\Product\Collection */ $directionReverse = $this->getCurrentDirectionReverse($subject); switch ($currentOrder) { case 'newest': $collection->getSelect()->reset(Select::ORDER)->order('created_at ' . $directionReverse); $collection->setPageSize($subject->getLimit())->setCurPage($subject->getCurrentPage()); break; case 'mostviewed': $reportEventTable = $collection->getResource()->getTable('report_event'); $subSelect = $this->_conn->select()->from( ['report_event_table' => $reportEventTable], 'COUNT(report_event_table.event_id)' )->where( 'report_event_table.object_id = e.entity_id' ); $collection->getSelect()->reset(Select::ORDER)->columns( ['views' => $subSelect] )->order( 'views ' . $directionReverse ); $collection->setPageSize($subject->getLimit())->setCurPage($subject->getCurrentPage()); break; case 'bestseller': $reportEventTable = $collection->getResource()->getTable('sales_order_item'); $subSelect = $this->_conn->select()->from( ['order_item' => $reportEventTable], 'COUNT(order_item.product_id)' )->where( 'order_item.product_id = e.entity_id' ); $collection->getSelect()->reset(Select::ORDER)->columns( ['ordered' => $subSelect] )->order( 'ordered ' . $directionReverse ); $collection->setPageSize($subject->getLimit())->setCurPage($subject->getCurrentPage()); break; default: break; } } return $proceed($collection); } /** * @param \Magento\Catalog\Block\Product\ProductList\Toolbar $subject * @return string */ function getCurrentDirectionReverse(\Magento\Catalog\Block\Product\ProductList\Toolbar $subject): string { if ($subject->getCurrentDirection() == 'desc') return 'asc'; return 'desc'; } }