text Eliminar los filtros por defecto de magento y agregar nuevos filtros de atributos custom

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了text Eliminar los filtros por defecto de magento y agregar nuevos filtros de atributos custom相关的知识,希望对你有一定的参考价值。

Reference
https://magento.stackexchange.com/questions/96095/magento-2-how-to-add-custom-sort-by-option

di.xml
<?xml version="1.0" encoding="UTF-8" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Catalog\Model\Config">
        <plugin name="Catalog_Volar::addCustomOptions" type="Woow\CatalogVolar\Plugin\Model\Config" />
    </type>
    <type name="Magento\Catalog\Block\Product\ProductList\Toolbar">
        <plugin name="Catalog_Volar::addPriceMilesFilterInToolbar" type="Woow\CatalogVolar\Plugin\Product\ProductList\Toolbar" />
    </type>
</config>


/var/www/woow/app/code/Woow/CatalogVolar/Plugin/Model/Config.php

<?php

namespace Woow\CatalogVolar\Plugin\Model;

use Magento\Store\Model\StoreManagerInterface;

class Config
{
    protected $_storeManager;

    public function __construct(
        StoreManagerInterface $storeManager
    ) {
        $this->_storeManager = $storeManager;

    }

    /**
     * Adding custom options and changing labels
     *
     * @param \Magento\Catalog\Model\Config $catalogConfig
     * @param [] $options
     * @return []
     */
    public function afterGetAttributeUsedForSortByArray(\Magento\Catalog\Model\Config $catalogConfig, $options)
    {
        $store = $this->_storeManager->getStore();
        $currencySymbol = $store->getCurrentCurrency()->getCurrencySymbol();

        //Remove specific default sorting options
        $options = [];
//        unset($options['position']);
//        unset($options['name']);
//        unset($options['price']);

        //Changing label
        //$customOption['position'] = __('Relevance');

        //New sorting options
        $customOption['woow_price_miles_asc'] = __('Less Miles');
        $customOption['woow_price_miles_desc'] = __('More Miles');

        //Merge default sorting options with custom options
        $options = array_merge($customOption, $options);

        return $options;
    }
}








/var/www/woow/app/code/Woow/CatalogVolar/Plugin/Product/ProductList/Toolbar.php

<?php

namespace Woow\CatalogVolar\Plugin\Product\ProductList;

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) {
        //woow_price_miles  -> Atribute product
            if ($currentOrder == 'woow_price_miles_asc') {
                $subject->getCollection()->setOrder('woow_price_miles', 'asc');
            } elseif ($currentOrder == 'woow_price_miles_desc') {
                $subject->getCollection()->setOrder('woow_price_miles', 'desc');
            }
        }

        return $result;
    }
}

以上是关于text Eliminar los filtros por defecto de magento y agregar nuevos filtros de atributos custom的主要内容,如果未能解决你的问题,请参考以下文章

text desactivaFiltroObligatorio activaFiltroObligatorio,filtro obligatorio,filtrado por centros

text Javascript:教程para eliminar tildes(acentos),ñycaracteres raros

text Sétimopopo:colocando los archivos

text Evitarrotaciónentodos los viewcontrollers menos uno

Javascript-Eliminar标记A en pagina de impresiÃn

text Cuando despues de registrar o登录los datos del customer no aparecen dentro del header