5

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?

  • can you add your di.xml configuration for the plugin – fmsthird May 22 '19 at 5:35
  • please make sure this not FPC(full page cache) issue.please check after disbling FPC. If you face this issue only in FPC you have to make different solution for it – Rutvee Sojitra May 22 '19 at 5:55
  • @magefms di.xml added – VIPIN A ROY May 22 '19 at 6:32
  • @RutveeSojitra Do we have any other solution to resolve this issue? – VIPIN A ROY May 22 '19 at 6:35
  • Yes but have you check it disabling cache? @VIPINAROY – Rutvee Sojitra May 22 '19 at 6:36
1

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);
   ...
});
| improve this answer | |
  • I can see some js errors in the console after this code change like below: (index):519 Uncaught TypeError: Cannot read property 'length' of undefined at (index):519 at Object.execCb (require.js:1650) at Module.check (require.js:866) at Module.<anonymous> (require.js:1113) at require.js:132 at require.js:1156 at each (require.js:57) at Module.emit (require.js:1155) at Module.check (require.js:917) at Module.<anonymous> (require.js:1113) – VIPIN A ROY Nov 20 '19 at 10:10
  • Could you please help me on this? – VIPIN A ROY Dec 9 '19 at 7:20

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.