Skip to content

Showing customer eav attrribute in product page always shows cached data

I’ve created a customer EAV attribute and trying to show this attribute in the product details page but it always return chaced data till I run bin/magento cache:flush full_page.

I’ve tried to use sections.xml in the frontend area but it’s not working.

section.xml file content:

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Customer:etc/sections.xsd">
    <action name="catalog/product/view">
        <section name="customer"/>
    </action>
</config>

I also tried to add isScopePrivate and getCacheLifetime methods in my custom block but it also makes no changes.

I also tried getIdentities method and it’s also still getting cached data even when I can see the tags has been added.

Full block class code:

<?php

namespace MeVendorBlockProfile;


use MagentoEavModelCacheType;
use MagentoEavModelEntityAttribute;


class Address extends VnecomsVendorsBlockProfileAddress
{
    public function getIdentities()
    {
        $identities = parent::getIdentities();

        $identities[] = Type::CACHE_TAG;
        $identities[] = Attribute::CACHE_TAG;
        $identities[] = Attribute::CACHE_TAG . '_' . $this->getVendor()
            ->getCustomer()->getAttribute('location')->getId();
        $identities[] = 'customer' . '_' . $this->getVendor()
            ->getCustomer()->getId();

        return $identities;
    }

    /**
     * Get vendor address
     *
     * @return string
     */
    public function getAddress()
    {
        $customer = $this->getVendor()->getCustomer();

        if ($countryId = $customer->getData('location')) {
            $locationAttrOptions = $this->getVendor()
                ->getCustomer()->getAttribute('location')->getSource()->getAllOptions();

            foreach ($locationAttrOptions as $locationAttrOption) {
                if ($locationAttrOption['value'] == $countryId) {
                    $data =  $locationAttrOption['label'];

                    return $data;
                }
            }
        }
    }

    public function isScopePrivate()
    {
        return true;
    }

    protected function getCacheLifetime()
    {
        return 0;
    }
}

Critical notes:

1- When I was debugging, I found that the block class method is called only once, the next request not calling my class method.

2- When I disable FPC everything works perfectly.

Any help please?