How do I remove the SORT BY Price, Position, Name in Magento 2.1?
3 Answers
- Do not understand why you want to remove/hide this options.
- You can hide or remove this visibility as added.
- If you are using any THEME, change the code in theme level.
/vendor/magento/module-catalog/view/frontend/templates/product/list/toolbar/sorter.phtml
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
// @codingStandardsIgnoreFile
?>
<?php
/**
* Product list toolbar
*
* @var $block \Magento\Catalog\Block\Product\ProductList\Toolbar
*/
use Magento\Catalog\Model\Product\ProductList\Toolbar;
?>
<div class="toolbar-sorter sorter">
<label class="sorter-label" for="sorter"><?php /* @escapeNotVerified */ echo __('Sort By') ?></label>
<select id="sorter" data-role="sorter" class="sorter-options">
<?php foreach ($block->getAvailableOrders() as $_key => $_order): ?>
<option value="<?php /* @escapeNotVerified */ echo $_key; ?>"
<?php if ($block->isOrderCurrent($_key)): ?>
selected="selected"
<?php endif; ?>
>
<?php echo $block->escapeHtml(__($_order)) ?>
</option>
<?php endforeach; ?>
</select>
<?php if ($block->getCurrentDirection() == 'desc'): ?>
<a title="<?php /* @escapeNotVerified */ echo __('Set Ascending Direction') ?>" href="#" class="action sorter-action sort-desc" data-role="direction-switcher" data-value="asc">
<span><?php /* @escapeNotVerified */ echo __('Set Ascending Direction') ?></span>
</a>
<?php else: ?>
<a title="<?php /* @escapeNotVerified */ echo __('Set Descending Direction') ?>" href="#" class="action sorter-action sort-asc" data-role="direction-switcher" data-value="desc">
<span><?php /* @escapeNotVerified */ echo __('Set Descending Direction') ?></span>
</a>
<?php endif; ?>
</div>
Replace to
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
// @codingStandardsIgnoreFile
?>
<?php
/**
* Product list toolbar
*
* @var $block \Magento\Catalog\Block\Product\ProductList\Toolbar
*/
use Magento\Catalog\Model\Product\ProductList\Toolbar;
?>
<div class="toolbar-sorter sorter">
<?php if ($block->getCurrentDirection() == 'desc'): ?>
<a title="<?php /* @escapeNotVerified */ echo __('Set Ascending Direction') ?>" href="#" class="action sorter-action sort-desc" data-role="direction-switcher" data-value="asc">
<span><?php /* @escapeNotVerified */ echo __('Set Ascending Direction') ?></span>
</a>
<?php else: ?>
<a title="<?php /* @escapeNotVerified */ echo __('Set Descending Direction') ?>" href="#" class="action sorter-action sort-asc" data-role="direction-switcher" data-value="desc">
<span><?php /* @escapeNotVerified */ echo __('Set Descending Direction') ?></span>
</a>
<?php endif; ?>
</div>
Not recommended to edit core module. create a new module/theme for use case.
-
1how do you remove only
Position
from Sort by Option ? Can we do it without extending template and just by using xml or something ? Commented Jan 17, 2018 at 9:01 -
@Slimshadddyyy did you find a way to do this in the end? i'm trying to achieve the same thing– JohnCommented Feb 19, 2019 at 15:42
-
1@John, you may extend the template in your custom theme and remove the
Position
option. You may also want to test it thoroughly Commented Feb 19, 2019 at 15:47
You can use plugin for this case, because when you exclude the key in the template file, it will still sort the order of the products by position by default.
Vendor/Module/Plugin/Magento/Catalog/Block/Product/ProductList/Toolbar.php
namespace Vendor\Module\Plugin\Magento\Catalog\Block\Product\ProductList;
class Toolbar
{
public function aroundGetAvailableOrders(
\Magento\Catalog\Block\Product\ProductList\Toolbar $subject,
\Closure $proceed
) {
$result = $proceed();
//make sure that each array key does exist, and then remove them
if (array_key_exists('position', $result)) unset($result['position']);
if (array_key_exists('name', $result)) unset($result['name']);
if (array_key_exists('price', $result)) unset($result['price']);
return $result;
}
}
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 disabled="false" name="Vendore_Module_Plugin_Magento_Catalog_Block_Product_ProductList_Toolbar" sortOrder="10" type="Vendor\Module\Plugin\Magento\Catalog\Block\Product\ProductList\Toolbar"/>
</type>
</config>
If you need to remove only the "Position" option in the select you can edit your sorter.phtml in this way (don't edit core module, create a new module)
<select id="sorter" data-role="sorter" class="sorter-options">
<option value="placeholder"><?= /* @escapeNotVerified */ __('Sort By') ?></option>
<?php foreach ($block->getAvailableOrders() as $_key => $_order):
if ($_key == 'position'){ continue; }
?>
<option value="<?= /* @escapeNotVerified */ $_key ?>"
<?php if ($block->isOrderCurrent($_key)): ?>
selected="selected"
<?php endif; ?>
>
<?= $block->escapeHtml(__($_order)) ?>
</option>
<?php endforeach; ?>
</select>