Magento – how to get a product attribute

product-attribute

I have product attribute, tvitemid, but I can't seem to get it's value. below is the code. I tried 4 different methods.

I created the attribute as global in the product admin and I can see values in the admin.

I have read similar questions but it still does not work for me.

require_once 'app/Mage.php';
Mage::app();

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

foreach($products as $product){

    //works
    $sku = $product->getSku();
    echo "sku = $sku  ";


    //from here down, I get no results
    $tvitemid1 = $product->getTvitemid();
    echo " tvitemid1 = $tvitemid1 ";

    $tvitemid2 = $product->getAttributeText('tvitemid');
    echo " tvitemid2 = $tvitemid2 ";

    $prod = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku);
    $tvitemid3 = $prod->getAttributeText('tvitemid');
    echo " tvitemid3 = $tvitemid3 ";

    $tvitemid4 = Mage::getModel('catalog/product')->load($product->getProductId())->getData('tvitemid');
    echo " tvitemid4 = $tvitemid4 \n";

}

Best Answer

Load your collection like this:

$products = Mage::getModel('catalog/product')->getCollection()
              ->addAttributeToSelect('vitemid');

Also rebuild your indexes in System->Index Management.

You can get the attribute value like this:

foreach ($products as $product){
    $value = $product->getAttributeText('tvitemid'); //if the attribute is dropdown or multiselect and you need the label.
    //or
    $value = $product->getTvitemid(); //for simple text attribute.
}
Related Topic