按产品列表中的价格从低到高和从高到低排序 [英] Sort by price low to high and high to low in product listing magento2
问题描述
我是magento2的新手.我正在使用Magento ver. 2.1.1
I am new in magento2. I am using Magento ver. 2.1.1
我想在产品列表页面的排序依据"下拉列表中添加自定义price low to high
和price high to low
.
I want to add custom price low to high
and price high to low
in Sort By dropdown in product listing page.
我没有获得toolbar.phtml页面.另外,我在Google上也没有得到任何有关这方面的东西.
I didn't get toolbar.phtml page. Also I didn't get any stuff regarding this in google.
如果有人有任何想法,请帮助我. 谢谢!
If anyone have any idea, then please help me. Thanks!
推荐答案
步骤1:在
app/code/Vendor/Module/etc/di.xml
app/code/Vendor/Module/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\Catalog\Block\Product\ProductList\Toolbar">
<plugin name="custom_custom_block_toolbar" type="Vendor\Module\Plugin\Catalog\Block\Toolbar" />
</type>
<type name="Magento\Catalog\Model\Config">
<plugin name="custom_catalog_model_config" type="Vendor\Module\Plugin\Catalog\Model\Config" />
</type>
</config>
步骤2:在
app/代码/供应商/模块/插件/目录/模型/Config.php
app/code/Vendor/Module/Plugin/Catalog/Model/Config.php
<?php
namespace Vendor\Module\Plugin\Catalog\Model;
class Config
{
public function afterGetAttributeUsedForSortByArray(
\Magento\Catalog\Model\Config $catalogConfig,
$options
) {
$options['low_to_high'] = __('Price - Low To High');
$options['high_to_low'] = __('Price - High To Low');
return $options;
}
}
步骤3:在
app/代码/供应商/模块/插件/目录/块/Toolbar.php
app/code/Vendor/Module/Plugin/Catalog/Block/Toolbar.php
<?php
namespace Vendor\Module\Plugin\Catalog\Block;
class Toolbar
{
/**
* Plugin
*
* @param \Magento\Catalog\Block\Product\ProductList\Toolbar $subject
* @param \Closure $proceed
* @param \Magento\Framework\Data\Collection $collection
* @return \Magento\Catalog\Block\Product\ProductList\Toolbar
*/
public function aroundSetCollection(
\Magento\Catalog\Block\Product\ProductList\Toolbar $subject,
\Closure $proceed,
$collection
) {
$currentOrder = $subject->getCurrentOrder();
$result = $proceed($collection);
if ($currentOrder) {
if ($currentOrder == 'high_to_low') {
$subject->getCollection()->setOrder('price', 'desc');
} elseif ($currentOrder == 'low_to_high') {
$subject->getCollection()->setOrder('price', 'asc');
}
}
return $result;
}
}
这篇关于按产品列表中的价格从低到高和从高到低排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!