That data is important for Magento, but there is a way to remove price data from it.
- Create a module
Vendor_Module
- Add all relevant files to make a basic module (
registration.php
, and etc/module.xml
).
- 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>
- 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