Back to Top

How to check if “Use Default Value” checked for a product attribute value in magento 2

Updated 1 March 2024

In this blog, I will explain how can you get if a product attribute value is overridden for a store view. Here for example I am checking for Product Name (name) attribute and store view with id=1.

public function __construct(
    .....
    \Magento\Catalog\Model\Attribute\ScopeOverriddenValue $scopeOverriddenValue
    \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
) {
    .....
    $this->scopeOverriddenValue = $scopeOverriddenValue;
    $this->productRepository = $productRepository;
}
 
public function execute()
{
    .....
    $product = $this->productRepository->get('Test Product');
    $isOverriden = $this->scopeOverriddenValue->containsValue(
        \Magento\Catalog\Api\Data\ProductInterface::class,
        $product,           //Product object
        'name',             //Attribute Name
        1                   //Store view Id
    );
}
1st condition

It will result True if the “Use Default Value” is unchecked. So for below it will result,

Otherwise, it will return False, like below,

2nd condition

Feel free to comment if you find any issues.

Searching for an experienced
Magento 2 Company ?
Find out More
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home