回声相关产品的简短描述

时间:2012-12-28 12:56:01

标签: php magento

有没有办法回应foreach相关产品的简短说明?

我尝试使用此代码但不显示每种产品的说明:

<?php echo nl2br($this->getProduct()->getDescription()) ?>

<?php echo $_helper->productAttribute($_item, nl2br($_item->getShortDescription()), 'short_description') ?>

有没有办法为相关产品做到这一点?如果有人知道,请指出我正确的方向。

<?php if($this->getItems()->getSize()): ?>
<div class="block block-related">
    <div class="block-title">
        <strong><span><?php echo $this->__('Related Products') ?></span></strong>
    </div>
    <div class="block-content">
        <p class="block-subtitle"><?php echo $this->__('Check items to add to the cart or add to your wishlist') ?>&nbsp;<br /></p>
        <div class="form-horizontal">
        <?php foreach($this->getItems() as $_item): ?>
             <div class="control-group">
                    <label for="related-checkbox<?php echo $_item->getId() ?>" class="control-label">
                        <a class="fancybox static-thumbs pull-left" href="<?php echo $this->helper('catalog/image')->init($_item, 'small_image')->resize(500, 450); ?>" title="<?php echo $this->htmlEscape($_item->getName()) ?>" ><img src="<?php echo $this->helper('catalog/image')->init($_item, 'small_image')->resize(135, 135) ?>" width="135" height="135" alt="<?php echo $this->htmlEscape($_item->getName()) ?>" /></a>
                    </label>
<div class="controls">
<label class="checkbox">
        <?php if(!$_item->isComposite() && $_item->isSaleable()): ?>
            <?php if (!$_item->getRequiredOptions()): ?>
                <input type="checkbox" class="checkbox related-checkbox" id="related-checkbox<?php echo $_item->getId() ?>" name="related_products[]" value="<?php echo $_item->getId() ?>" />
        <?php endif; ?>
    <?php endif; ?>
<?php if ($this->helper('wishlist')->isAllow()) : ?>
    <a href="<?php echo $this->getAddToWishlistUrl($_item) ?>" class="pull-right" title="<?php echo $this->__('Add to Wishlist') ?>" rel="tooltip"><span class="icon-check"></span></a>

<p class="product-name span6">
 <a href="<?php echo $_item->getProductUrl() ?>" class="product-title"><?php echo $this->htmlEscape($_item->getName()) ?></a>
 <br />
<?php echo nl2br($this->getProduct($_item)->getDescription()) ?>
</p>
<form action="<?php echo $this->getAddToCartUrl($_item) ?>" method="post">
<fieldset>
  <label class="product-name"><?php echo $this->__('Quantity:'); ?></label>
  <select name="qty" class="span1">
  <?php $i = 1 ?>
  <?php do { ?>
    <option value="<?php echo $i?>">
      <?php echo $i?>
      <?php $i++ ?>
    </option>
    <?php } while ($i <= (int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($_item)->getMaxSaleQty()) ?>
</select>
<div class="clearfix"></div>
<button class="btn btn-danger" data-loading-text="PLease wait..."><span><?php echo $this->__('Add to Cart') ?></span></button>
<span id='ajax_loader' style='display:none'><img src='<?php echo $this->getSkinUrl('images/opc-ajax-loader.gif')?>'/></span>            
</fieldset>
</form>
<?php endif; ?>
<?php echo $this->getPriceHtml($_item, true, '-related') ?>
</label>
</div>
    </div>
    <hr />
        <?php endforeach ?>
        </div>
    </div>

2 个答案:

答案 0 :(得分:1)

您是否尝试过nl2br($this->getProduct($_item)->getShortDescription())

默认情况下,可能没有为相关产品加载简短描述的值。您可以尝试$_item->load($_item->getId()),然后使用$_item->getShortDescription()

答案 1 :(得分:0)

在尝试$_item->load($_item->getId())并回显shortDescription之后,您应该查找加载相关产品的Collection。在此集合中,属性short_description未加载。

如果您加载每个产品,则会对数据库进行大量查询,这将耗费大量性能。这不是必需的。替代方案是:

$relatedProductCollection->addAttributeToSelect('short_description');

问题是找到$relatedProductCollection

更新: 我挖掘了代码(参考文献是1.7.0.2):

// app/code/core/Mage/Catalog/Block/Product/List/Related.php:61
$this->_addProductAttributesAndPrices($this->_itemCollection);

// app/code/core/Mage/Catalog/Block/Product/Abstract.php:410
$collection
->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())

// app/code/core/Mage/Catalog/Model/Config.php:260
$this->getAttributesUsedInProductListing()

这应解释为:加载“相关产品”时,将加载所有属性,这些属性也会在产品列表中使用。问题可能是:默认情况下加载short_desciption。因此,删除load()调用并检查,属性short_description是否在“产品列表中使用”。如果设置为yes,我的解释是错误的。

尽管如此,加载每件产品的解决方案都是垃圾。

编辑: 我已将以下行添加到Related.php

$this->_itemCollection = $product->getRelatedProductCollection()
->addAttributeToSelect('required_options') 
->addAttributeToSelect('short_description') 
->setPositionOrder() 
->addStoreFilter() 

使用此行确实回应了一个descrtiption foreach产品,但它是相同的描述getProduct() - &gt; getDescription())?&gt;

您需要<?php echo ($this->getProduct()->getShortDescription()) ?>

但编辑核心代码也是改变magento行为的一种坏方法。这里描述了如何重写块,但我不认为这是必要的。 http://prattski.com/2010/06/24/magento-overriding-core-files-blocks-models-resources-controllers/