Is hole punching cache method working in Magento2? How to implement in Particular Block in Magento2?

magento.stackexchange https://magento.stackexchange.com/questions/238333

  •  22-01-2021
  •  | 
  •  

Can anyone please explain hole punching Cache method and how to implement in particular one block in Magento 2.

有帮助吗?

解决方案

Whole punching Cache is not the good idea at Magento 2.

Using layout set cacheable="false" for block means you have the disabling cache for the whole page.

If you want to get private of the content of current user then you should use customer-data JS library to store private data in local storage.

Might be you have tried to achieve How to add a remove link on product detail page, if it's already added on wishlist in magento2? question answer.

So in case, my suggestion to push user existing wishlist item product id to local storage and basic of that data active and deactivate the wishlist link.

A good example of this type of work is Magento_InstantPurchase

Here.in module Magento push instance purchase data means customer default shipping address and billing address to local storage

Magento\InstantPurchase\CustomerData\InstantPurchase

See that class:

    public function getSectionData(): array
    {
        if (!$this->customerSession->isLoggedIn()) {
            return ['available' => false];
        }
    ......
        $paymentToken = $instantPurchaseOption->getPaymentToken();
        $shippingAddress = $instantPurchaseOption->getShippingAddress();
        $billingAddress = $instantPurchaseOption->getBillingAddress();
        $shippingMethod = $instantPurchaseOption->getShippingMethod();
        $data += [
            'paymentToken' => [
                'publicHash' => $paymentToken->getPublicHash(),
                'summary' => $this->paymentTokenFormatter->format($paymentToken),
            ],
            'shippingAddress' => [
                'id' => $shippingAddress->getId(),
                'summary' => $this->customerAddressesFormatter->format($shippingAddress),
            ],
            'billingAddress' => [
                'id' => $billingAddress->getId(),
                'summary' => $this->customerAddressesFormatter->format($billingAddress),
            ],
            'shippingMethod' => [
                'carrier' => $shippingMethod->getCarrierCode(),
                'method' => $shippingMethod->getMethodCode(),
                'summary' => $this->shippingMethodFormatter->format($shippingMethod),
            ]
        ];
        return $data;
    }

That data is read using customerData.get('instant-purchase') Checkout show knock js view read this data.

Magento/InstantPurchase/view/frontend/web/js/view/instant-purchase.js

See how to use private data. https://devdocs.magento.com/guides/v2.2/extension-dev-guide/cache/page-caching/private-content.html

许可以下: CC-BY-SA归因
scroll top