【问题标题】:Sort by price low to high and high to low in product listing magento2在产品列表中按价格从低到高和从高到低排序 magento2
【发布时间】:2017-03-24 01:52:10
【问题描述】:

我是 Magento 2 的新手。我正在使用 Magento 2.1.1

我想在产品列表页面的排序方式下拉列表中添加自定义price low to highprice high to low

我没有得到toolbar.phtml 页面。我也没有在谷歌上得到任何关于这个的东西。

【问题讨论】:

    标签: magento2


    【解决方案1】:

    第 1 步:

    中创建插件

    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>
    

    第二步:

    中创建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;
    
        }
    
    }
    

    第三步:

    中创建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;
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-06
      • 2020-12-28
      • 2016-04-01
      • 2017-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-10
      相关资源
      最近更新 更多