Question

I need to remove all the disabled products from wishlist and for this I write a product status check in Magento_Wishlist/templates/item/list.phtml like this:

if($product->getStatus() == \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED):
------------
endif;

I also add a plugin to update wishlist count in the header part like below:

/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\Wishlist\CustomerData\Wishlist">
        <plugin name="vendor-customer-wishlist"
                type="Vendor\Customer\Plugin\WishlistPlugin" sortOrder="1" />
    </type>
</config>

Vendor/Customer/Plugin/WishlistPlugin

<?php
namespace Vendor\Customer\Plugin;

use Magento\Catalog\Model\Product\Attribute\Source\Status;

/**
 * Class WishlistPlugin
 * @package Vendor\Customer\Plugin
 */
class WishlistPlugin
{
    /**
     * @var \Magento\Wishlist\Helper\Data
     */
    protected $wishlistHelper;

    /**
      * WishlistPlugin constructor.
      * @param \Magento\Wishlist\Helper\Data $wishlistHelper
      * @param \Magento\Wishlist\Model\Wishlist $wishlist
      * @param \Magento\Customer\Model\Session $customerSession
    */
   public function __construct(
     \Magento\Wishlist\Helper\Data $wishlistHelper,
     \Magento\Wishlist\Model\Wishlist $wishlist,
     \Magento\Customer\Model\Session $customerSession
   ) {
     $this->wishlistHelper = $wishlistHelper;
     $this->wishlist = $wishlist;
     $this->customerSession = $customerSession;
  }

    /**
     * Plugin function after get section data
     *
     * @param \Magento\Wishlist\CustomerData\Wishlist $subject
     * @param $result
     * @return mixed
     */
    public function afterGetSectionData(\Magento\Wishlist\CustomerData\Wishlist $subject, $result)
    {
        $disabledProductsCount = 0;
        $customerId = $this->customerSession->getCustomerId();
        $wishlistItems = $this->wishlist->loadByCustomerId($customerId, true)->getItemCollection();

        foreach ($wishlistItems as $item) {
          if($item->getProduct()->getStatus() == Status::STATUS_DISABLED) $disabledProductsCount++;
        }

        $counterNumber = $this->wishlistHelper->getItemCount();

        if($disabledProductsCount) {
         $counterNumber -= $disabledProductsCount;
        }

        $result['counternumber'] = $counterNumber;

        return $result;
    }


}

But when I add three items in wishlist and disable one product from that, then the wishlist counter not updating properly and it remains three. Anybody please help me to resolve this?

Was it helpful?

Solution

It's up to you where you add this, probably the best place to call this is in the footer (it will trigger the wishlist count for that customer to update on every page).

There is no easy way to invalidate data from your plugin (backend), so for what you are doing, since you never know when you are disabling a product on the frontend, so we can assume refreshing the wishlist data on every page load is the best option...

For example (add this in footer, change to your requirements):

require([
   ....
   'Magento_Customer/js/customer-data'
], function (...,customerData) {
   "use strict";
   ...
   var sections = ['wishlist'];
   customerData.invalidate(sections);
   customerData.reload(sections, true);
   ...
});
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top