1

On the product page I have this code:

"Magento_Catalog/js/product/view/provider": {//etc

which contains a serialized array with product data, including price.

The problem is that Google reads this array and extracts the price, previewing it in the serp. In the rest of the page the price appears only to logged in users.

Can I disable this block or at least remove the price? Is this a necessary block to the page? The block is \Magento\Catalog\Block\Ui\ProductViewCounter

1 Answer 1

0

That data is important for Magento, but there is a way to remove price data from it.

  1. Create a module Vendor_Module
  2. Add all relevant files to make a basic module (registration.php, and etc/module.xml).
  3. Create a etc/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">
    <type name="Magento\Catalog\Block\Ui\ProductViewCounter">
        <plugin name="Vendor_Module::ProductViewCounter" type="Vendor\Module\Plugin\Block\Ui\ProductViewCounter" />
    </type>
</config>
  1. Create the plugin file Plugin/Block/Ui/ProductViewCounter.php
<?php

declare(strict_types=1);

namespace Vendor\Module\Plugin\Block\Ui;

use Magento\Catalog\Block\Ui\ProductViewCounter as BaseProductViewCounter;
use Magento\Customer\Model\Session;
use Magento\Framework\Serialize\SerializerInterface;

class ProductViewCounter
{

    /**
     * @param Session $customerSession
     * @param SerializerInterface $serializer
     */
    public function __construct(
        private readonly Session $customerSession,
        private readonly SerializerInterface $serializer
    ) {
    }

    /**
     * @param BaseProductViewCounter $subject
     * @param string $result
     *
     * @return string
     */
    public function afterGetCurrentProductData(BaseProductViewCounter $subject, string $result): string
    {
        // If customer is logged in, return the original result
        if ($this->customerSession->isLoggedIn()) {
            return $result;
        }

        $data = $this->serializer->unserialize($result);

        // If the items array is empty, return the original result
        if (empty($data['items'])) {
            return $result;
        }

        $productId = array_key_first($data['items']);

        // Remove the price_info from the serialized data
        unset($data['items'][$productId]['price_info']);

        return $this->serializer->serialize($data);
    }
}

In the above implementation we are removing the price_info block from the product data when customer is not logged in.

I have tested the above code and it works fine on Magento 2.4.6-p1

1
  • 1
    Perfect! I only changed the constructor, removing the readonly and declaring the two variables outside because I use php7.
    – Infocurci
    Jul 17 at 18:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.