0

We have a need to display customer specific prices on the front end. Our current implementation would work if it were not for caching (Full-Page Caching if I've understood correctly).

Is there a way modify the caching process so that the end users would see the correct prices? (assuming that is what we should do)

I read on a post that it should be possible to create a PageCache Identifier, but it threw an exception:

Exception #0 (Magento\Framework\Exception\RuntimeException): Type Error occurred when creating object: Magento\Framework\App\PageCache\Kernel

Here's the code:

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">
    <preference for="Magento\Framework\App\PageCache\Identifier" type="Vendor\Module\PageCache\Identifier" />
</config>

Identifier.php

namespace Vendor\Module\PageCache;

use Magento\Framework\App\ObjectManager;
use Magento\Framework\Serialize\Serializer\Json;

class Identifier
{
    protected $request;
    protected $context;
    private $serializer;
    private $session;

    public function __construct(
        \Magento\Framework\App\Request\Http $request,
        \Magento\Framework\App\Http\Context $context,
        Json $serializer = null
    ) {
        $this->request = $request;
        $this->context = $context;
        $this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);
    }

    public function getValue()
    {
        $data = [
            $this->request->isSecure(),
            $this->request->getUriString(),
            $this->request->get(\Magento\Framework\App\Response\Http::COOKIE_VARY_STRING)
                ?: $this->context->getVaryString()
        ];
        return sha1($this->serializer->serialize($data));
    }
}
2
  • 1
    Consider loading prices via a custom AJAX request that is set to bypass any cacheing. This way you get the needed flexibility without compromising the effectiveness of cacheing in general.
    – jiheison
    Feb 3 '20 at 16:34
  • Yes, this is what we ended up doing.
    – MikeGrove
    Feb 7 '20 at 5:48
1

As jiheison suggests in the comments, we ended up fetching our prices with AJAX.

Related: How to implement customer specific pricing, Magento 2.3

0

Modification of a product's attribute (eg. price) should add the product to the cache queue to be purged and regenerated.

Is your cache queue being flushed? If you are using Varnish you can see what it is doing via varnishlog.

If you are not using Varnish for FPC, you will see files being created/deleted under the var/cache directory under your Magento root. Under the mage-tags directory you will see items with CAT_ in their name. CAT_P_ means product, with the ids afterwards, eg. CAT_P_502 == product id 502. CAT_C_ means category, eg. CAT_C_20 = category id 20.

I used https://github.com/emcrisostomo/fswatch/releases to watch for changes under there, eg. fswatch -r /usr/local/var/www/var/cache

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.