Check If Product Attribute Has Store View Specific Value

databaseextensionsproduct

I want to check whether the "use default value" flag is set for a specific product attribute in the admin for a specific store view.

If I have the full product model, I can easily check it like that:

$product = Mage::getModel('catalog/product')->setStoreId($storeViewId)->load($productId);
$hasStoreViewSpecificValue = $product->getExistsStoreValueFlag('my_attribute');

Is it possible to get this information in a smart way without loading the whole product (optimally without any direct SQL queries, but if there is no other way…)?

Best Answer

Thanks @DavidManners for the hint. In the end, it was easier to look how it is done Mage_Catalog_Model_Resource_Abstract::_setAttributeValue though. I came up with the following script which checks if a product has the "Use Default Value" flag set for a specific attribute:

<?php

error_reporting(E_ALL);
ini_set('display_errors', '1');

require_once 'app/Mage.php';
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

$store = 7;
$entityId = 15087;
$attributeCode = 'meta_title';

// easy with loading the whole product
/** @var Mage_Catalog_Model_Product $product */
$product = Mage::getModel('catalog/product')->setStoreId($store)->load($entityId);
echo 'Has Product a Store View Specific Value? ';
var_dump($product->getExistsStoreValueFlag($attributeCode));

// tricky to do it without loading the whole product
$adapter = Mage::getSingleton('core/resource')->getConnection('core_read');
$attributeModel = Mage::getModel('eav/entity_attribute')->loadByCode(Mage_Catalog_Model_Product::ENTITY, $attributeCode);
$table = $attributeModel->getBackend()->getTable();
$entityTypeIdField = Mage::getModel('eav/entity')->setType(Mage_Catalog_Model_Product::ENTITY)->getEntityIdField();
$select = $adapter->select()
    ->from($table, array())
    ->where($entityTypeIdField . ' =?', $entityId)
    ->where('attribute_id =?', $attributeModel->getId())
    ->where('store_id =?', $store)
    ->columns(
        '*'
    );
$values = $adapter->fetchAll($select);
echo 'Has Product a Store View Specific Value? ';
if (empty($values)) {
    var_dump(false);
} else {
    var_dump(true);
}

I did not test it, but the second version should be much faster as the product does not have to be fully loaded.