0

how can we remove the "items" text from the wishlist count I searched a bit and know the count is displayed from app/design/frontend/.../.../Magento_Wishlist/templates/link.phtml

<span data-bind="text: wishlist().counter ? wishlist().items.length : '0'" class="counter qty">0</span>
2
  • Do you want to remove only the text 'items' or the entire text with the count? Commented Apr 28, 2023 at 5:27
  • yes @CharmiPatel you have any suggestions
    – mj king
    Commented May 2, 2023 at 10:11

2 Answers 2

0

There are many ways to do it.

  • by the plugin
  • by the preference
  • by overriding phtml and js component

By the preference:

In di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
        <preference for="Magento\Wishlist\CustomerData\Wishlist" type="Dotsquares\Custom\Rewrite\CustomerData\Wishlist"/>
    </config>

Create Dotsquares\Custom\Rewrite\CustomerData\Wishlist.php

<?php

namespace Dotsquares\Custom\Rewrite\CustomerData;

use Magento\Catalog\Model\Product\Image\NotLoadInfoImageException;
use Magento\Customer\CustomerData\SectionSourceInterface;
use Magento\Framework\App\ObjectManager;

/**
 * Wishlist section
 */
class Wishlist extends \Magento\Wishlist\CustomerData\Wishlist
{
    /**
     * @var string
     */
    const SIDEBAR_ITEMS_NUMBER = 3;

    /**
     * @var \Magento\Wishlist\Helper\Data
     */
    protected $wishlistHelper;

    /**
     * @var \Magento\Catalog\Helper\ImageFactory
     */
    protected $imageHelperFactory;

    /**
     * @var \Magento\Framework\App\ViewInterface
     */
    protected $view;

    /**
     * @var \Magento\Wishlist\Block\Customer\Sidebar
     */
    protected $block;

    /**
     * @var \Magento\Catalog\Model\Product\Configuration\Item\ItemResolverInterface
     */
    private $itemResolver;

    /**
     * @param \Magento\Wishlist\Helper\Data $wishlistHelper
     * @param \Magento\Wishlist\Block\Customer\Sidebar $block
     * @param \Magento\Catalog\Helper\ImageFactory $imageHelperFactory
     * @param \Magento\Framework\App\ViewInterface $view
     * @param \Magento\Catalog\Model\Product\Configuration\Item\ItemResolverInterface|null $itemResolver
     */
    public function __construct(
        \Magento\Wishlist\Helper\Data $wishlistHelper,
        \Magento\Wishlist\Block\Customer\Sidebar $block,
        \Magento\Catalog\Helper\ImageFactory $imageHelperFactory,
        \Magento\Framework\App\ViewInterface $view,
        \Magento\Catalog\Model\Product\Configuration\Item\ItemResolverInterface $itemResolver = null
    ) {
        parent::__construct($wishlistHelper, $block, $imageHelperFactory, $view, $itemResolver);
    }

    /**
     * @inheritdoc
     */
    public function getSectionData()
    {
        $counter = str_replace(" item", "", str_replace(" items", "", $this->getCounter())) ;
        return [
            'counter' => $counter,
            'items' => $counter ? $this->getItems() : [],
        ];
    }
}

The best way to do this is by using the plugin.

0

For Remove "items" text in wishlist header icon count follow below steps:

1.Create a di.xml file in the Vendor/Module/etc directory and add below code:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Wishlist\CustomerData\Wishlist" type="Vendor\Module\CustomerData\Wishlist" />
</config>

2.Create a Wishlist.php file at Vendor/Module/CustomerData directory and add the below code:

<?php
namespace Vendor\Module\CustomerData;

use Magento\Wishlist\CustomerData\Wishlist as CoreWishlist;

class Wishlist extends CoreWishlist
{
    public function __construct(\Magento\Wishlist\Helper\Data $wishlistHelper, \Magento\Wishlist\Block\Customer\Sidebar $block, \Magento\Catalog\Helper\ImageFactory $imageHelperFactory, \Magento\Framework\App\ViewInterface $view, \Magento\Catalog\Model\Product\Configuration\Item\ItemResolverInterface $itemResolver = null)
    {
        parent::__construct($wishlistHelper, $block, $imageHelperFactory, $view, $itemResolver);
    }

    /**
     * @param $count
     * @return \Magento\Framework\Phrase|null
     */
    protected function createCounter($count)
    {
        if ($count > 1) {
            //you can also add your custom text
            return __('%1', $count);
        } elseif ($count == 1) {
            return __('1');
        }
        return null;
    }
}

3.then run the below commands and update wishlist,

php bin/magento setup:upgrade
php bin/magento setup:di:compile
php bin/magento cache:flush

please check the result,

Let me know if you have any query.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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