I am using Magento 2.2.3 and need to hide the Sort By Price on category page in toolbar if the customer is not logged in.

Can someone please elaborate how this can be done.


I have created a viewModel see below

namespace Mbs\HidePriceInSortingFeature\ViewModel;

use Magento\Framework\View\Element\Block\ArgumentInterface;

class SortingViewModel implements ArgumentInterface
     * @var \Magento\Customer\Model\Session
    private $customerSession;

    public function __construct(
        \Magento\Customer\Model\Session $customerSession
    ) {
        $this->customerSession = $customerSession;

    public function isCustomerLoggedIn()
        return $this->customerSession->getCustomerId()!='';

    public function isAttributeToBeShowedInSortingFeature($attributeKey)
        if ($this->isCustomerLoggedIn()) {
            return true;

        if ($attributeKey == 'price') {
            return false;

        return true;

then I did link the viewModel to the Toolbar block:

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        <referenceBlock name="product_list_toolbar" template="Mbs_HidePriceInSortingFeature::sorter.phtml">
                <argument name="viewModel" xsi:type="object">\Mbs\HidePriceInSortingFeature\ViewModel\SortingViewModel</argument>

finally, in the sorter.phtml template, I made use of the viewModel. The orter.phtml template can be either in a module like in this post or in your theme folder, you would need to adjust the layout snippet removing the mention of the template in this case

 * Product list toolbar
 * @var $block \Magento\Catalog\Block\Product\ProductList\Toolbar
use Magento\Catalog\Model\Product\ProductList\Toolbar;
<?php /** @var \Mbs\HidePriceInSortingFeature\ViewModel\SortingViewModel $viewModel */ ?>
<?php $viewModel = $block->getData('viewModel'); ?>

<div class="toolbar-sorter sorter">
    <label class="sorter-label" for="sorter"><?= /* @escapeNotVerified */ __('Sort By') ?></label>
    <select id="sorter" data-role="sorter" class="sorter-options">
        <?php foreach ($block->getAvailableOrders() as $_key => $_order): ?>
            <?php if (!$viewModel->isAttributeToBeShowedInSortingFeature($_key)) continue; ?>
            <option value="<?= /* @escapeNotVerified */ $_key ?>"
                <?php if ($block->isOrderCurrent($_key)): ?>
                <?php endif; ?>
                <?= $block->escapeHtml(__($_order)) ?>
        <?php endforeach; ?>
    <?php if ($block->getCurrentDirection() == 'desc'): ?>
        <a title="<?= /* @escapeNotVerified */ __('Set Ascending Direction') ?>" href="#" class="action sorter-action sort-desc" data-role="direction-switcher" data-value="asc">
            <span><?= /* @escapeNotVerified */ __('Set Ascending Direction') ?></span>
    <?php else: ?>
        <a title="<?= /* @escapeNotVerified */ __('Set Descending Direction') ?>" href="#" class="action sorter-action sort-asc" data-role="direction-switcher" data-value="desc">
            <span><?= /* @escapeNotVerified */ __('Set Descending Direction') ?></span>
    <?php endif; ?>
  • @muhammed answers are to help you. If the answer did help you, it is helpful for me and for the community to have your feedback whether the answer works for you? Jan 15 '19 at 21:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.