aDvo
aDvo

Reputation: 904

Magento - display product image in sidebar compare block

Note: I am not talking about the pop up comparison page below, I'm refering to the compare block that is displayed by template file sidebar.phtml. mytheme\default\template\catalog\product\compare\sidebar.phtml

I have a custom compare block using sidebar.phtml, based on the standard Magento one. I'm trying to figure out how to display the product image alongside the product that has been selected for comparison.

I see that in the template file sidebar.phtml, it uses a helper to get the products currently listed for comparison.

$_helper = $this->helper('catalog/product_compare');
$_items = $_helper->getItemCount() > 0 ? $_helper->getItemCollection() : null;

And looking at the product listing page template list.phtml for a clue on how the image is retrieved, i can see:

<img id="product-collection-image-<?php echo $_product->getId(); ?>"
                     src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize($_imgSize); ?>"
                     alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" />

Problem is i'm not sure how to use the data I'm getting from the "catalog/product_compare" helper to get the respective small_image.

I did a dump of $_items coming from the catalog/product_compare helper, and the dump is below. I can't identify anything i can use like a product id? Not sure!

object(ET_AdvancedCompare_Model_Resource_Eav_Mysql4_Product_Compare_Item_Collection)[245]
  protected '_customerId' => int 0
  protected '_visitorId' => int 96
  protected '_comparableAttributes' => null
  protected '_flatEnabled' => 
    array (size=1)
      1 => boolean false
  protected '_productWebsiteTable' => string 'catalog_product_website' (length=23)
  protected '_productCategoryTable' => string 'catalog_category_product' (length=24)
  protected '_addUrlRewrite' => boolean true
  protected '_urlRewriteCategory' => string '' (length=0)
  protected '_addMinimalPrice' => boolean false
  protected '_addFinalPrice' => boolean false
  protected '_allIdsCache' => null
  protected '_addTaxPercents' => boolean false
  protected '_productLimitationFilters' => 
    array (size=7)
      'store_table' => string 't_compare' (length=9)
      'visibility' => 
        array (size=3)
          0 => int 3
          1 => int 2
          2 => int 4
      'store_id' => int 1
      'category_id' => string '2' (length=1)
      'use_price_index' => boolean true
      'customer_group_id' => int 0
      'website_id' => string '1' (length=1)
  protected '_productCountSelect' => null
  protected '_isWebsiteFilter' => boolean false
  protected '_priceDataFieldFilters' => 
    array (size=0)
      empty
  protected '_map' => 
    array (size=1)
      'fields' => 
        array (size=6)
          'price' => string 'price_index.price' (length=17)
          'final_price' => string 'price_index.final_price' (length=23)
          'min_price' => string 'price_index.min_price' (length=21)
          'max_price' => string 'price_index.max_price' (length=21)
          'tier_price' => string 'price_index.tier_price' (length=22)
          'special_price' => string 'price_index.special_price' (length=25)
  protected '_priceExpression' => null
  protected '_additionalPriceExpression' => null
  protected '_maxPrice' => null
  protected '_minPrice' => null
  protected '_priceStandardDeviation' => null
  protected '_pricesCount' => null
  protected '_catalogPreparePriceSelect' => null
  protected '_factory' => 
    object(Mage_Catalog_Model_Factory)[190]
      protected '_config' => 
        object(Mage_Core_Model_Config)[5]
          protected '_useCache' => boolean true
          protected '_cacheSections' => 
            array (size=6)
              ...

Upvotes: 0

Views: 1943

Answers (1)

aDvo
aDvo

Reputation: 904

I found an answer after alot of trial and error. There isn't any real proper usable documentation on all this is there?

<?php  
                    $product = Mage::getModel("catalog/product")->load($_item->getProductId());
                    echo  "<img src='" . Mage::helper('catalog/image')->init($product, 'small_image')->keepFrame(false)->resize(50) . "'>";
                    ?>

Upvotes: 1

Related Questions