0

I've been trying to simply add the product field: UPC to new order transactional emails, in addition to SKU that's the default.

I'm pretty sure I found the template to make the modifications from another help article on making changes to the template. But can't find the right way to pull the actual UPC to show.

I'm making modifications to the template: /store/app/design/frontend/MYTHEME/default/template/email/order/items/order/default.phtml

The table cell next to SKU, I have tried all of the following calls for UPC:

<?php echo $_item->getUpc() 
<?php echo $_item->getData('upc') ?> 
<?php echo $this->getData('upc') ?>
<?php $this->getUpc($_item)  ?>

I'm pretty much just taking stabs at it, but didn't think it would be this difficult considering it's pretty much a key product field, if you want to use an additional code other than an internal SKU.

Help? Thanks

1

I recommend the observer approach foremost.

You can also use a collection instead of loading the product object. This will be less intensive and faster. I don't recommend this as a solution over Rinda's, but it is an alternative that you may not know about.

$upc = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToSelect('upc') // only selecting what you want
    ->addAttributeToFilter('sku', $mySku) // filtering
    ->getFirstItem()
    ->getUpc();
0

The problem is that you're trying to get the data from the $_itemvariable, which is of type Mage_Sales_Model_Order_Item - and it does not exist on that model.

What you can do is to either add that attribute to the Mage_Sales_Model_Order_Item tables (see this thread for an example) or simply get the product represented by the order item and read the data from there.

That would look something like this:

$_productId = $_item->getProductId();
$_product = Mage::getModel('catalog/product')->load($_productId);
echo $_product->getUpc();

The drawback being that this way Magento will load the complete product model of each ordered item when sending that email, which is a quite expensive operation and will slow down the process a bit.

Hope that helps, regards

  • So why do that? You can use $_item->getProduct(), but the better fix is to put the Upc attribute on the item object using either a class rewrite it collection observer. And I see you mentioned that. Please stick to that, there's enough examples of ->load() out there, that slow sites down to a crawl. – Melvyn Oct 6 '14 at 16:48
  • I fully agree with the "right" way of doing it that's less expensive than the inline reading of each product's data. I need to dig into adding to the collection observer. BUT, I did try the quick and dirty method above just to have it working temporarily. It didn't seem to work. I have that block of code right where I want the UPC to display in the default.phtml template under the template/email/order/items/order/ dir. No luck. – Rich Yessian Oct 8 '14 at 15:52
  • Are you completely sure the template you are working on is being used when sending a new order email? The template used is defined in System->Configuration->Sales->Sales Emails. You might want to check System->Transaction Emails for the right template. Try adding some arbitrary string to the email just to verify. – Rinda Oct 9 '14 at 8:25
  • Yes, I did. Since I was trying all of the original (above) attempts to display the UPC in the email, I put a: "1. 2. 3. 4." Next to each attempt. Those printed just fine, just no data with them. I will dig in a little more. Assuming the code you provided is definitely without typos, but looks fine from what I can tell. I assume I can also look at error logs to try and find what it's reporting. – Rich Yessian Oct 9 '14 at 22:20
  • Yeah, not sure what the issue is. I have double checked everything and it is using the correct template file. I can display static text and affect other areas of the template. The inline code method doesn't seem to display. I can work more on the better approach, but was hoping for a quick method for now. – Rich Yessian Oct 10 '14 at 23:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.