Lucas Scholten
Lucas Scholten

Reputation: 915

Magento, just can't select a custom attribute

Hi I'm trying to select a custom attribute in my script.

Here is my code

$collection = Mage::getModel('catalog/product')->getCollection()

->addAttributeToSelect('jan')

->addAttributeToSelect('name')

->addAttributeToSelect('upc')

->addAttributeToSelect('ean')

->addAttributeToSelect('price')

->addAttributeToSelect('cost_price')

->addAttributeToSelect('subtitle')

->addAttributeToSelect('url_key')

->addAttributeToSelect('brand')

->addAttributeToFilter('sku', array('eq' => '30000387'));

Then I do a foreach loop to go through the $collection as $item

I am able to pull all these values using $item->getData('insert atttribute code here')

Except for the cost_price no matter what I do I can't seem to pull cost_price I have tried

$item->getData('cost_price')
$item->getCostPrice()

I have tried reindexing and removing the cache just in case, to no avail. The value is definitely filled in the product I am selecting.

Help much appreciated.

Upvotes: 0

Views: 2214

Answers (2)

Erfan
Erfan

Reputation: 3019

If you always want the cost_price attribute to be loaded (without every time having to call load on the product), it might be a better solution to make it always load by either:

  • going to the backend, Catalog -> Manage Attributes -> select your attribute, and selecting "Yes" for "Visible on Product View Page on Front-end" and "Used in Product Listing"
  • adding the eav attribute in the catalog config.xml:

    <product>
        <collection>
            <attributes>
                <name/>
                <url_key/>
                <price/>
                <special_price/>
                .....
                <cost_price/>
            </attributes>
        </collection>
    </product>
    

(don't edit core files though, create your own module)

Upvotes: 2

Swapna Taru
Swapna Taru

Reputation: 688

Try this

 <?php echo $_product->getResource()->getAttribute('cost_price')->getFrontend()->getValue($_product); ?>

Upvotes: 0

Related Questions