4

Magento 2.1.1, PHP 7.0.

I want to check inside product page or product list if product is in wishlist. Actually I have block which works, but only if Full Page Cache is disabled or this block in xml is marked as cacheable="false"

Is it a bug or is it by design ? Maybe I have a bug in my code? Block:

namespace [VENDOR]\SkinHelper\Block\Catalog\Product

use \Magento\Catalog\Api\Data\ProductInterface;
use \Magento\Framework\View\Element\Template;
use \Magento\Framework\View\Element\Template\Context;
use \Magento\Wishlist\Helper\Data;


class IsInWishlist extends Template
{


private $customerSession;

private $registry;

private $wishlistHelper;

public function __construct(
    Context $context,
    Data $wishlistHelper,
    array $data
)
{
    parent::__construct($context, $data);

    $this->wishlistHelper = $wishlistHelper;
}


private function getCustomerWishlistItemsCollection()
{
    $itemsCollection = $this->wishlistHelper->getWishlist()->getItemCollection();

    return $itemsCollection;
}


public function isInWishlist(ProductInterface $product)
{
    $productId = $product->getId();

    $itemsCollection = $this->getCustomerWishlistItemsCollection();
    $itemsIds = $itemsCollection->getColumnValues('product_id');

    return in_array($productId, $itemsIds);
}

It's added in catalog_category_view.xml:

<referenceBlock name="category.products.list"> <block class="[VENDOR]\SkinHelper\Block\Catalog\Product\IsInWishlist" name="name_catalog_isinwishlist" cacheable="false"/> </referenceBlock>

Part of list.phtml, where it can be called:

$IsInWishlist = $block->getChildBlock('name_catalog_isinwishlist'); ...

<?php foreach ($_productCollection as $_product): ?> <?php $is_in_wishlist = $IsInWishlist->isInWishlist($_product) ?> <?php echo $is_in_wishlist?>

......

<php? endforeach; ?>

...

1
  • Facing same isuue. Have you got the solution? Aug 14, 2018 at 4:44

1 Answer 1

7

Another approach in order to retrieve wishlist or custumer related information, is to use localStorage combined with Ajax request if the localstorage is empty.

Check information in your console :

JSON.parse(localStorage.getItem('mage-cache-storage')).wishlist
JSON.parse(localStorage.getItem('mage-cache-storage')).wishlist.items[0]

enter image description here

You can based on the javascript file : vendor/magento/module-wishlist/view/frontend/web/js/view/wishlist.js

define([
    'uiComponent',
    'Magento_Customer/js/customer-data'
], function (Component, customerData) {
    'use strict';

    return Component.extend({
        initialize: function () {
            this._super();

            this.wishlist = customerData.get('wishlist');
        }
    });
});

With this code, javascript will retrive the information with an Ajax request if the information is not stored in the local storage.

Then you can create your own Javascript component in order to manage your logic.

4
  • I am only getting the last 3 items even thought I have 5 items in my wishlist do you know how to get all items? May 25, 2018 at 9:07
  • Chek if your data in your localstorage is correct, then debug the files : vendor/magento/module-customer/view/frontend/web/js/customer-data.js:251, vendor/magento/module-wishlist/view/frontend/web/js/add-to-wishlist.js. You can also use the fonction invalidate or customerData.reload(sections, true) of customer-data if your data is not up to date May 28, 2018 at 8:01
  • @FranckGarnier I don't understand the solution you recommend. Your answer is too generic. Can you provide a more detailed solution?
    – zekia
    Nov 11, 2019 at 11:00
  • I had the same problem of showing only last 3 products. Reason is in backend, this api is the same api as sidebar wishlist which should show by default 3 last items. you can modify it by overriding \Magento\Wishlist\CustomerData\Wishlist::getItems here is an article nwdthemes.com/2019/08/23/magento-2-working-with-wishlist/…
    – Erfan
    Apr 3, 2020 at 12:56

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.