The problem:
I'm running into an issue with the page/block caching and the minicart.
In the template cart/minicart.phtml
we have a permission check to see if the current customer is allowed to add products to the cart like so:
<?php if ($customerViewModel->hasPermission(PermissionManagerInterface::ACL_RESOURCE_4)): ?>
// Render the minicart
<?php endif; ?>
The problem here is that this block is being cached on the first visit. So if the first customer who visits the page is allowed to add products to the cart then customers who don't and logged out visitors will see the cart button in the header.
Fix attempt 1: Override the block and set _isScopePrivate to true
While this fixed the initial issue with the always-visible (or never visible) cart button, a new issue came up: the contents of the cart is never rendered, presumably because the mage-init/knockoutJS stuff is now not executed properly. The dialog which should contain the items is always an empty white box.
Fix attempt 2: Setting cacheable to false
Aside from getting the issue of the full_page cache now never picking up the pages, this did not do anything to the block output. It's still either always visible or never visible.
The current workaround
Currently, I have disabled the block_html cache and this "fixes" all the issues above. But I would prefer if there is a way to make the block just appear conditionally based on the output of the hasPermission
check.
TL;DR
Is there a way to conditionally show/hide the minicart block based on the output of a hasPermission
method.