0

In database, I find data like this

SELECT * FROM catalog_product_entity_text where entity_id = 70430 AND attribute_id = 76 AND store_id = 5;

How can I do it programmatically?

I need info if it is checked Use Default Value on store view for the product.

enter image description here

This gives me if is the default store view or not.

public function __construct(
    Context $context,
    \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
    \Magento\Catalog\Model\Attribute\ScopeOverriddenValue $scopeOverriddenValue
) {
    $this->productCollectionFactory = $productCollectionFactory;
    $this->scopeOverriddenValue = $scopeOverriddenValue;
    parent::__construct($context);
}
public function translateProducts(){
foreach($products as $product)
        {
            $product_id = $product->getId();
            $p = $objectManager->create('Magento\Catalog\Model\Product')->setStoreId($store_id)->load($product_id);
            $defaultProduct = $objectManager->create('Magento\Catalog\Model\Product')->load($product_id);


            $isOverriden = $this->scopeOverriddenValue->containsValue(
                \Magento\Catalog\Api\Data\ProductInterface::class,
                $product,
                'name', 
                $store_id                   
            );
            
            $logger->info( "store id: ".$store_id." id: ".$product->getId(). " sku: ".$product->getSku()." Name ". $p->getName()." Default name ". $defaultProduct->getName()." isOverriden: ".$isOverriden. "xxxxx ". $product->getExistsStoreValueFlag("name"));
            
            /*if($isOverriden == 0){*/ //here I want to check if value is override

    

2 Answers 2

1

Magento doesn't store any flags like use default

Magento EAV value tables designed with following rules:

  • Row with store_id=0 means default or fallback value
  • Row with store_id=1 means value for store
  • If doesn't exists row with store_id=1 will be used row with store_id=0 and it's means Use Default Value
  • Each attribute has own scope and this applicable only for is_global=0 in catalog_eav_attribute

When the system loads product data per store all global values will be replaced to store values if they are present.

In which place are you want to detect the system uses default or store value for attribute and what is the goal?

Anyway, you can use \Magento\Catalog\Model\Attribute\ScopeOverriddenValue in any logic on frontend as well.

[Update]

The issue with your fragment that you cannot use the same instance of ScopeOverriddenValue for different products and you need to use new for each

Just try this code

foreach ($this->collection as $product) {
    // instance of \Magento\Catalog\Model\Attribute\ScopeOverriddenValueFactory in __constructor
    $scopeOverriddenValue = $this->scopeOverriddenValueFactory->create();
    $isOverridden         = $scopeOverriddenValue->containsValue(
        \Magento\Catalog\Api\Data\ProductInterface::class,
        $product,
        'name',
        $storeId
    );

    // you can create single instance of \Magento\Catalog\Model\ResourceModel\Product in __constructor
    $productResource = $product->getResource();
    $defaultName     = $isOverridden
        ? $productResource->getAttributeRawValue($product->getId(), 'name', 0)
        : $product->getName();

    $message = "store id: " . $storeId . ", product id: " . $product->getId() . ", sku: " . $product->getSku() . ", name: " . $product->getName() . ", default: " . $defaultName . ", overridden: " . ($isOverridden ? 'yes' : 'no');
    $this->logger->info($message);
}

Example from log:

store id: 1, product id: 8, sku: 10-1-1040, name: Store Value, default: Global value, overridden: yes
6
  • I want to translate products in-store view, but if the Use Default value is not checked then translate otherwise exit and check another product. Jun 23 at 6:23
  • Please specify more details about your logic (front\backend) and I can advise best way. Maybe make sense to create some custom resource model with load logic or something else. Jun 23 at 8:21
  • I change the post. Jun 23 at 8:27
  • See updated answer Jun 23 at 9:19
  • For all collections, I got the same value: overridden: yes, What I am doing wrong? Jun 24 at 6:47
0

When I use \Magento\Catalog\Model\Attribute\ScopeOverriddenValue was not working correctly, but with this class \Magento\Catalog\Model\Attribute\ScopeOverriddenValueFactory everything work as should.

public function __construct(
    Context $context,
    \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
    \Magento\Catalog\Model\Attribute\ScopeOverriddenValueFactory $scopeOverriddenValue
) {
    $this->productCollectionFactory = $productCollectionFactory;
    $this->scopeOverriddenValue = $scopeOverriddenValue;
    parent::__construct($context);
}
1
  • It's exactly what I've pointed in answer ;) Jun 24 at 12:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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