Can’t Get Value from Attribute ‘eancode’

attributescsvPHP

I want to export some orders from Magento 1.7, but I'm stuck on 1 thing. I have an attribute in Magento, named 'eancode'. I want to put the eancode from every product in the export, but it shows a blank field. I checked that the attribute contains some numbers for the exported products.

In csv.php I have:

/**
 * Returns the item specific values.
 * 
 * @param Mage_Sales_Model_Order_Item $item The item to get values from
 * @param Mage_Sales_Model_Order $order The order the item belongs to
 * @return Array The array containing the item specific values
 */
protected function getOrderItemValues($item, $order, $itemInc=1) 
{
    return array(
        $itemInc,
        $this->geteancode($item),
        $item->getName(),
    );
}

}

In abstractcsv.php I have:

/**
 * EAN code output
 *
 * @param Mage_Sales_Model_Order_Item $item The item to return info from
 * @return String The ean
 */
protected function geteancode($item)
{
$eancode = Mage::getModel('catalog/product')->load($item->getProduct()->getId())->geteancode();
return $item->geteancode();
}

Can you see what's going wrong? I've been troubleshooting and Googling for hours now, but I can't find the problem.

Thank you!

Best Answer

I can see two issues with your code:

  1. Even though you may have named the attribute in lower case, you will still need to refer to it in your get method using CamelCase, for example:

    $eancode = Mage::getModel('catalog/product')->load($item->getProduct()->getId())->geteancode();
    
  2. In the first line of protected function geteancode($item) function, you retrieve the ean value, and then you return the geteancode() function again on the following line which would return an empty String.

Your method should read:

  protected function geteancode($item)
 {
     return Mage::getModel('catalog/product')->load($item->getProduct()->getId())->getEancode();
 }
Related Topic