【问题标题】:Magento - display product image in sidebar compare blockMagento - 在侧边栏比较块中显示产品图像
【发布时间】:2014-10-06 21:54:44
【问题描述】:

注意:我不是说下面弹出的比较页面,我指的是模板文件sidebar.phtml显示的比较块。 mytheme\default\template\catalog\product\compare\sidebar.phtml

我有一个基于标准 Magento 的使用 sidebar.phtml 的自定义比较块。 我正在尝试弄清楚如何在选择进行比较的产品旁边显示产品图片。

我看到在模板文件sidebar.phtml中,它使用了一个帮助器来获取当前列出的产品进行比较。

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

查看产品列表页面模板 list.phtml 以获取有关如何检索图像的线索,我可以看到:

<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) ?>" />

问题是我不确定如何使用从“catalog/product_compare”帮助程序获得的数据来获取相应的 small_image。

我做了一个来自 catalog/product_compare 帮助器的 $_items 的转储,转储在下面。我无法识别我可以使用的任何东西,比如产品 ID?不确定!

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)
              ...

【问题讨论】:

    标签: image magento compare


    【解决方案1】:

    经过多次反复试验,我找到了答案。 没有关于这一切的任何真正适当的可用文档吗?

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

    【讨论】:

    • 这种方式会因为循环内的查询而导致性能问题。问题会更大,因为您在比较列表中有很多产品。
    • 比较列表中最多只能有 7 或 10 个产品。还不足以成为性能问题吗?