3

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";

}

3 Answers 3

9

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.
}
3
  • What code do I use to get the value? What code would I use to change the value
    – sdfor
    Commented Jan 13, 2014 at 13:08
  • @sdfor. See my update
    – Marius
    Commented Jan 13, 2014 at 13:12
  • This is Right Way. :)
    – Keyul Shah
    Commented Jan 13, 2014 at 13:15
4

Your code is not working because you have to load the product object again. Because You fetch the data form Collection object and they give you just basic values and If you want to fetch that Custom value you have to load the data again.

Try Simple One

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

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

foreach($products as $product){
    //works
    $sku = $product->getSku();
    echo "sku = $sku  ";
    $prod=Mage::getSingleton('catalog/product')->load($product->getId());
    echo $prod->getAttributeText('tvitemid');
}
?>
15
  • 2
    This works but it's a performance killer. Loading entities inside a foreach is not recommended.
    – Marius
    Commented Jan 13, 2014 at 13:02
  • 1
    Yes @Marius I agree with you Its take too Much time. but I gave answer as per question asked.
    – Keyul Shah
    Commented Jan 13, 2014 at 13:06
  • 3
    I'm not questioning the validity of the answer. Like I said 'It works'. I just don't recommend it. It's just my opinion
    – Marius
    Commented Jan 13, 2014 at 13:07
  • this does not work for me, the return value is empty.
    – sdfor
    Commented Jan 13, 2014 at 13:08
  • @Marius It's ok :)
    – Keyul Shah
    Commented Jan 13, 2014 at 13:09
0

Attribute settings are OK? Show on front-end needs to be set to YES and then you can echo/show the attribute.

i use this for sku: <?php echo nl2br($_product->getSku()) ?> or this for my custom attribute: <?php echo $_product->getData('procent'); ?>

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.