3

If I load a product's data in store view or website scope, every attribute set to "use default value" is populated with that default value. This is fine in most cases, but now I'm trying to find out which products have a price defined at website level and which ones don't.

Product X's price is £10 in "default values" and £9 at the website level.
Product Y's price is £12 in "default values" and not set at the website level.

I'm trying to get just the website-level data programmatically, i.e. 9.0000 and NULL, but whatever I try, the price for product Y comes back as £12.

This post - https://stackoverflow.com/a/6623565/1493346 - describes how to set / unset attributes at store view level, but it hasn't helped with getting them.

This is what I've tried:

Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$_products = Mage::getModel('catalog/product')->getCollection()
                   ->addAttributeToSelect(array('price'))
                   ->setStoreId(STORE_ID)
                   ->addStoreFilter(STORE_ID);
foreach ($_products as $_product) {
    echo $_product->getPrice(); // no good
    $_product->setStoreId(STOREID)->load($_product->getId());
    echo $_product->getPrice(); // still no good
}

Is there a way to do this without resorting to direct database queries?

1 Answer 1

5

Let's say that $product is the instance of your product loaded in a specific store.
You can check this

$basePrice = $product->getAttributeDefaultValue('price');
if ($basePrice === false) {
     //Use default values is checked
} else {
    //$basePrice is the price in the default values.
}

Unfortunately this works only if you call load on the product model, not if you get the product from a collection.

I mean, this works

$product = Mage::getModel('catalog/product')->setStoreId($storeId)->load($productId);
var_dump($product->getAttributeDefaultValue('price'));

This will produce a value if the price is different on the website that contains the store view with the id $storeId and it will produce false if "Use default values" is checked.

This won't work.

$collection = Mage::getModel('catalog/product')->setStorId($storeId)->getCollection()
    ->addAttributeToSelect('entity_id', array('eq' => $productId));
foreach ($collection as $product) {
    var_dump($product->getAttributeDefaultValue('price'));
}

This will always produce false.

Your Answer

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

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