Magento Get Product Attribute Text in Stock.php

user892970 picture user892970 · Nov 26, 2013 · Viewed 9.2k times · Source

I've been trying to adjust the amount stock reduced by a sale, depending on a product's attribute, I've tried the following methods, but seem to be getting nowhere.

Magento Version: 1.8.0.0

File: /app/code/core/Mage/CatalogInventory/Model/Resource/Stock.php

Around line 158 after: foreach ($productQtys as $productId => $qty)

I have added the following code:

$CheckLength = Mage::getModel('catalog/product')->load($_item['product_id'])->getData('length');
if ( $CheckLength == 'Per Half Metre' )
{
    $qty = $qty / 2;
}

and also tried this:

$myproduct = Mage::getModel('catalog/product');

if ($myproduct->getAttributeText('length')=='per Half Metre')
{
    $qty = $qty / 2;
}

and this:

$storeId = Mage::app()->getStore()->getId(); // return current store id
$CheckLength = Mage::getResourceModel('catalog/product')->getAttributeRawValue($productId, 'length', $storeId);

if ( $CheckLength == 'Per Half Metre' )
{
    $qty = $qty / 2;
}

None of the above seem to work. Without the check, the qty divides by 2 quite nicely as I need, but I only need it to do it if the Product Attribute is "Per Half Metre".

PS: This attribute is in a drop down with 2 other options.

Hope someone can help! Thanks in Advance.

Answer

user892970 picture user892970 · Nov 28, 2013

Finally figured it out, the following works - I must have tried 15 different variations before this worked.

$myproduct = Mage::getModel('catalog/product')->load($productId);
$mylength = $myproduct->getAttributeText('length');

Thanks to BuzzJoe for pointing me in the right direction, I eventually used:

Mage::Log('$productId: '.$productId, null, 'mylogfile.log');
Mage::Log('$mylength: '.$mylength, null, 'mylogfile.log');

to keep a check on things. Couldn't get var_dump to work.