3

I have a script to show related products in order confirmation emails but it gets the large, main image and not the thumbnail. In some emails clients this splatters these gigantic images and messes up the email layout. Here's the code I'm using which says to get the thumbnail but doesn't:

<?php
    $_order = $this->getOrder();
    $_items = $_order->getAllItems();
    $categories = array();
    $relatedProducts = array();
    $storeId = NULL;

?>
    <?php foreach ($_items as $item): ?>
        <?php $itemId = $item->getProductId(); ?>
        <?php $_product = Mage::getModel('catalog/product')->load($item->getProductId()); ?>
        <?php $cats = $_product->getCategoryIds(); ?>
        <?php $relatedProductIds = $_product->getRelatedProductIds(); ?>

        <!-- add related ids to array -->
        <?php foreach ($relatedProductIds as $id): ?>
            <?php $relatedProducts[] = $id; ?>
        <?php endforeach; ?>

        <!-- related could be already purchased - remove it -->
        <?php if(($key = array_search($itemId, $relatedProducts)) !== false): ?>
            <?php unset($relatedProducts[$key]); ?>
        <?php endif; ?>

        <!-- set store id -->
        <?php if ($storeId == NULL): ?>
            <?php $storeId = $item->getStoreId(); ?>
        <?php endif; ?>

    <?php endforeach; ?>
 <!-- get three related products (after the shuffling) -->
  <?php if (count($relatedProducts) >= 3): ?>
      <?php shuffle($relatedProducts); ?>
<div style="width:100%;float:left">
      <h2>You may also like these...</h2>
    <ul style="padding:0;list-style:none;display:inline-block;width:100%;">
      <?php for ($i=0; $i < 3; $i++): ?>
          <?php $relatedProductId = $relatedProducts[$i]; ?>
            <li style="list-style:none;display:table-cell;padding:5px;width:16.666666667%;text-align:center;background:#f3dce7;border-right:1px solid #fff;">
                    <?php $thumbnail =  Mage::getResourceModel('catalog/product')->getAttributeRawValue($relatedProductId, 'thumbnail', $storeId); ?>
                    <?php echo "<img src='" . Mage::getModel('catalog/product_media_config')->getMediaUrl($thumbnail) . "' alt='" . $name .  "' style='width:150px;height:220px;margin:10px' />"; ?>
                <div>
                    <span>
                    <?php $url = Mage::getResourceModel('catalog/product')->getAttributeRawValue($relatedProductId, 'url_key', $storeId); ?>
                    <?php $name = Mage::getResourceModel('catalog/product')->getAttributeRawValue($relatedProductId, 'name', $storeId); ?>
                    <b><a href="<?php echo Mage::getBaseUrl(); ?><?php echo $url; ?>.html"><?php echo $name;?></a></b>
                    </span>
                </div>
                <div>
                    <?php $this->__('SKU'); ?>
                    <?php echo Mage::getResourceModel('catalog/product')->getAttributeRawValue($relatedProductId, 'supplier_code', $storeId); ?>-<?php echo Mage::getResourceModel('catalog/product')->getAttributeRawValue($relatedProductId, 'sku', $storeId); ?>
                </div>
                <div>
                    <span><?php echo Mage::helper('core')->formatPrice(Mage::getResourceModel('catalog/product')->getAttributeRawValue($relatedProductId, 'price', $storeId), false); ?></span>
                </div>
            </li>
      <?php endfor; ?>
    </ul>
</div>
<?php else: ?>
<?php
$visibility = array(
Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,
Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG
                     );
$storeId = Mage::app()->getStore()->getId();
$_productCollection = Mage::getResourceModel('reports/product_collection')
                            ->addAttributeToSelect('*')
                            ->addOrderedQty()
                            ->addAttributeToFilter('visibility', $visibility)
                            ->setOrder('ordered_qty', 'desc'); ?>
           <?php            foreach($_productCollection as $bs_product): ?> <?php if($i++ == 3) break; ?>
           <?php echo '<a href="'.Mage::getBaseUrl().''.$bs_product->getUrl_key().'.html">'.$bs_product->Name.'</a><br />'; ?>
           <?php echo '<img src="'.$bs_product->getImageurl().'" />'; ?>
           <?php endforeach; ?>  
<?php endif; ?> 

How can I retrieve the real thumbnail or small image?

Thanks

--Update

Here's my updated code:

<?php
    $_order = $this->getOrder();
    $_items = $_order->getAllItems();
    $categories = array();
    $relatedProducts = array();
    $storeId = NULL;

?>
    <?php foreach ($_items as $item): ?>
        <?php $itemId = $item->getProductId(); ?>
        <?php $_product = Mage::getModel('catalog/product')->load($item->getProductId()); ?>
        <?php $cats = $_product->getCategoryIds(); ?>
        <?php $relatedProductIds = $_product->getRelatedProductIds(); ?>

        <!-- add related ids to array -->
        <?php foreach ($relatedProductIds as $id): ?>
            <?php $relatedProducts[] = $id; ?>
        <?php endforeach; ?>

        <!-- related could be already purchased - remove it -->
        <?php if(($key = array_search($itemId, $relatedProducts)) !== false): ?>
            <?php unset($relatedProducts[$key]); ?>
        <?php endif; ?>

        <!-- set store id -->
        <?php if ($storeId == NULL): ?>
            <?php $storeId = $item->getStoreId(); ?>
        <?php endif; ?>

    <?php endforeach; ?>
 <!-- get three related products (after the shuffling) -->
  <?php if (count($relatedProducts) >= 3): ?>
  <?php shuffle($relatedProducts); ?>   

  <?php $productIds = array_slice($relatedProducts, 0, 3);

$products = Mage::getResourceModel('catalog/product_collection')
    ->addAttributeToSelect(array('name', 'url_key', 'thumbnail', 'sku', 'supplier_code'))
    ->addAttributeToFilter('status', 1)
    ->addFinalPrice()
    ->addIdFilter($productIds);

$imageHelper = Mage::helper('catalog/image'); ?>

<tr>
    <td>      
        <div style="width:100%;float:left">
      <h2 style="font-size:16px">You may also like these...</h2>
    <ul style="margin-left:0;padding:0;list-style:none;display:inline-block;width:100%;">
      <?php foreach ($products as $product): ?>
            <li style="list-style:none;display:table-cell;padding:5px;width:16.666666667%;text-align:center;background:#f3dce7;border-right:1px solid #fff;">
                    <a href="<?php echo $url = $product->getProductUrl(); ?>">
                    <img src="<?php echo $thumbail_src = $imageHelper->init($product, 'thumbnail')->resize(150,220); ?>" style="width:150px;height:220px;margin:10px" /></a>
                <div>
                    <span>

                    <b><a href="<?php echo $url = $product->getProductUrl(); ?>"><?php echo $name = $product->getName(); ?></a></b>
                    </span>
                </div>
                <div>
                    <?php echo $this->__('SKU'); ?>
                    <?php echo $supplierCode = $product->getSupplierCode(); ?>-<?php echo $sku = $product->getSku(); ?>
                </div>
                <div>
                    <span><?php echo $price = $product->getFinalPrice(); ?></span>
                </div>
            </li>
      <?php endforeach; ?>
            </ul>
        </div>
    </td>
</tr>
<?php else: ?>
<?php
$visibility = array(
Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,
Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG
                     );
$storeId = Mage::app()->getStore()->getId();
$_productCollection = Mage::getResourceModel('reports/product_collection')
                            ->addAttributeToSelect('*')
                            ->addOrderedQty()
                            ->addAttributeToFilter('visibility', $visibility)
                            ->setOrder('ordered_qty', 'desc'); ?>
           <?php            foreach($_productCollection as $bs_product): ?> <?php if($i++ == 3) break; ?>
           <?php echo '<a href="'.Mage::getBaseUrl().''.$bs_product->getUrl_key().'.html">'.$bs_product->Name.'</a><br />'; ?>
           <?php echo '<img src="'.$bs_product->getImageurl().'" />'; ?>
           <?php endforeach; ?>  
<?php endif; ?>

In case someone comes along and wants to use it here is the code to add to the new order transactional email: {{block type='core/template' area='frontend' template='mycmsblocks/email/related.phtml' order=$order}}

  • So you are pulling the Base value instead of the cache in this report already? – Bill Murray Jul 7 '16 at 18:40
4

The attribute thumbnail will give you the filename of the image to use as a thumbnail for the product. There's no constraints on the size of that image though and you're trying to resize the image using the style attribute.

If you look through the magento source you'll see that you need to use the image helper to resize it, for instance in app/design/frontend/base/default/template/checkout/cart/crosssell.phtml

you'll see $this->helper('catalog/image')->init($_item, 'thumbnail')->resize(75);

Parameter 1 is an instance of Mage_Catalog_Model_Product, Parameter 2 is the name of the attribute.

For you to get an instance of Mage_Catalog_Model_Product rather than looping 3 times through $relatedProducts you could use a product collection and filter on those three product ids. I'd try and suggest some more improvements but your code is really hard to read with the constant

--edit

your code becomes unusual after you shuffle the products. Instead of using a for loop like you do, you could use a collection. I've left out all the formatting code etc:

shuffle($relatedProducts);
$productIds = array_slice($relatedProducts, 0, 3);

$products = Mage::getResourceModel('catalog/product_collection')
    ->addAttributeToSelect(array('name', 'url_key', 'thumbnail', 'sku', 'supplier_code'))
    ->addAttributeToFilter('status', 1)
    ->addFinalPrice()
    ->addIdFilter($productIds)
;
$imageHelper = Mage::helper('catalog/image');

foreach ($products as $product) {
    $url = $product->getProductUrl();
    $price = $product->getFinalPrice();
    $name = $product->getName();
    $thumbail_src = $imageHelper->init($product, 'thumbnail')->resize(150,220);
    $supplierCode = $product->getSupplierCode();
    $sku = $product->getSku();
}

If you're using the flat tables you'll probably need to make sure that supplier_code is in the flat tables

| improve this answer | |
  • I understand about the thumbnail just getting the filename (interesting) but I don't know how to implement your suggestion. Sounds like a complete redo :) I'm not really to concerned about looping through the products to much because it only has to do this when an order is placed so speed isn't really to important but having optimized code is better. I just found this code on another website and customized it a lot. – Freejoy Jul 7 '16 at 22:11
  • The code is strange, I updated the answer with how to use a product collection. – Richard Jul 8 '16 at 8:32
  • It works great! Thanks. Don't know what I would do without magento.stackexchange. – Freejoy Jul 8 '16 at 10:35
  • you don't need to assign variables just to echo them (echo $name = $product->getName();) I was just doing that to show you how to get them – Richard Jul 8 '16 at 10:45
  • Yeah I thought about that a few minutes ago. : ) – Freejoy Jul 8 '16 at 11:01
2

Try this.

echo Mage::getModel('catalog/product_media_config') ->getMediaUrl( $product->getThumbnail() );

| improve this answer | |
  • I get Uncaught Error: Call to a member function getThumbnail() on null when I tried that. – Freejoy Jul 7 '16 at 21:57
  • Because you don't have a product object – Richard Jul 8 '16 at 8:13

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.