Magento 2 – Compare Products Not Showing in Sidebar

javascriptknockoutjsmagento2PHP

On a new site build, I'm trying to figure out why the compare products information is not showing up in the sidebar for the category page or the customer account page. It a new M2 install with a custom theme that is inheriting from Luma. When I click any product's "add to compare" button, the page will re-load, but there is no success message and nothing shows up in the sidebar, even after I wait for the page to fully (and I mean fully) load.

So far, this is the path I have gone down to test why the items are not showing up.

I have checked to make sure that the data is getting set in the DB on the catalog_compare_item table when I hit the "add to compare" button, and it is. It's being stored with the user ID when I'm logged in and not when I'm not. This table is matching a test dev site I set up to play with debugging this.

This lead me to check the compare page itself at {{site-root}}/catalog/product_compare/index/ and the products that I have added are showing up just fine. All the data is being pulled in and I can use all the functionality on this page.

This points to the Knockout JS templates that are being used to display the content. I tracked the template down using this file {{site-root}}/vendor/magento/module-catalog/view/frontend/layout/default.xml which points to this file for display logic {{site-root}}/vendor/magento/module-catalog/view/frontend/web/js/view/compare-products.js. When I put in a console.log for the Customer object that is driving the data for the file:

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

I get back an empty object. So this points to a null value coming back from the file {{site-root}}/vendor/magento/module-customer/view/frontend/web/js/customer-data.js.

So why would this file be returning with nothing?

Best Answer

For whatever reason, in my version of Magento 2.1.4, this functionality actually does work with the full_page cache enabled. This has tripped up my team at least once before with a different issue.

php bin/magento cache:enable full_page
php bin/magento cache:status # if you're curious

Note, as you would imagine, this makes development quite tricky. My workaround workflow:

  • Enable full-page caching
  • Refresh page. Now you should be able to see those products in your Wishlist and Compare list. At this point, I use the Chrome Inspector to make my CSS updates. When ready, paste CSS into your text editor as usual.
  • Since full-page caching is enabled, the page will not reflect your changes on refresh. Run php bin/magento cache:flush full_page.
  • Now, on page refresh, you will see your CSS updates (assuming that you have the reset of the CSS workflow happening correctly).

Other than this odd example, I keep the Magento caches enabled for development except for block_html and full_page. This provides a nice balance between seeing CSS and template changes immediately while retaining the performance benefit of having the cache enabled.

Related Topic