访问 success.phtml 上的自定义属性


Accessing custom attribute on success.phtml

我创建了一个自定义属性来跟踪每个项目的利润。

出于分析目的,我需要将其添加到 JS 中的变量中,并将其附加到 url 字符串中。

不幸的是,我

似乎无法访问该属性,每次我尝试回显该值时,它都会返回 null。

这是代码;

$orderObj = Mage::getModel(’sales/order’)->loadByIncrementId($this->getOrderId()); 
$orderItems = $orderObj->getAllItems(); 
$basket = ‘’; 
$mail_body = ‘’; 
foreach($orderItems as $item) 
{ 
$basket .= $item->getSku() .’|’. number_format($item->getRowTotal(), 2, ‘.’, ‘,’) .’|’. round($item->getQtyOrdered()) . ‘,’; 
}
foreach($orderItems as $item) { 
$product_item = Mage::getModel(’catalog/product’)->load($this->getProductId()); 
$mail_body .= $product_item->getAttributeText(’profit’); 
$mail_body .= “---'n'n”; 
}

我正在尝试工作的主要代码在foreach中。

知道为什么这不返回值吗?

尝试

$order = Mage::getModel('sales/order')->loadByIncrementId($this->getOrderId());
$items = $order->getItemsCollection();
foreach($items as $item){
  $product = Mage::getModel('catalog/product')->load($item->getProductId());
  echo $product->getAttributeText('profit');
}
$custom = Mage::getModel('catalog/product')->load($item->getProductId());
echo $custom->getAttributeText('profit');

这是可行的解决方案,它是如此简单,但我不明白:

<?php
$custom = Mage::getModel('catalog/product')->load($_item->getProductId());
echo $custom->getAttributeText('profit');
?>

希望这肯定会对您有所帮助。