0

enter image description here

This is my code:

$wishlistIds = $this->_checkoutSession->getWishlistIds();
$wishlist = $this->_wishlist->create()->loadByCustomerId($this->_customerSession->getCustomerId(), true);
$wishlists = $wishlist->getItemCollection()->load();
            foreach ($wishlists as $wishlistItem) {
                if ($wishlistItem->getId() ) {
                    $wishlistItem->delete();
                }
            }
0

Yo have to add another code $wishlist->save(); which will update wishlist completelty.

$wishlistIds = $this->_checkoutSession->getWishlistIds();
$wishlist = $this->_wishlist->create()->loadByCustomerId($this->_customerSession->getCustomerId(), true);
$wishlists = $wishlist->getItemCollection()->load();
foreach ($wishlists as $wishlistItem) {
    if ($wishlistItem->getId() ) {
        $wishlistItem->delete();
    }
}
// save wishlist
 $wishlist->save();

Edit

I Assume that we have written this code at Action.

As per as magento2, the wishlist is private content of specific to individual users.So Magento Use our customer-data JS library to store private data in local storage, invalidate private data using customizable rules, and synchronize data with the backend.

See that magento article https://devdocs.magento.com/guides/v2.2/extension-dev-guide/cache/page-caching/private-content.html

For this,you have to hit your controller using POST Request not GET request. because of private only update when post request happen

Also You have to add your action at

Vendor/ModuleName/etc/frontend/section.xml

like

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Customer:etc/sections.xsd">
    <action name="{FrontendName}/{Controllername}/{ActionName}">
        <section name="wishlist"/>
    </action>
</config>
  • I tried that but still it is not going.Also I tries by flushing cache but still it is not going. – Mguru48 Sep 12 '18 at 5:47
  • add $wishlist->save(); after ` $wishlistItem->delete();` check what happen – Amit Bera Sep 12 '18 at 5:48
  • Not working , it is still visible – Mguru48 Sep 12 '18 at 5:58
  • after run this code have to tried to flush cache and check what happen? – Amit Bera Sep 12 '18 at 6:02
  • still not working!! – Mguru48 Sep 12 '18 at 6:31

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.