2012-11-05 51 views
3

我正試圖讓相關商品塊出現在我的產品詳細信息頁面上。 我有各自的一個.phtml文件Magento相關商品沒有顯示

<?php 

<?php echo "Related product block"?> 
<?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') ?>&nbsp;<a href="#" onclick="selectAllRelated(this); return false;"><?php echo $this->__('select all') ?></a></p> 
    <ol class="mini-products-list" id="block-related"> 
    <?php foreach($this->getItems() as $_item): ?> 
     <li class="item"> 
      <?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; ?> 
      <div class="product"> 
       <a href="<?php echo $_item->getProductUrl() ?>" title="<?php echo  $this->htmlEscape($_item->getName()) ?>" class="product-image"><img src="<?php echo $this->helper('catalog/image')->init($_item, 'thumbnail')->resize(50) ?>" width="50" height="50" alt="<?php echo $this->htmlEscape($_item->getName()) ?>" /></a> 
       <div class="product-details"> 
        <p class="product-name"><a href="<?php echo $_item->getProductUrl() ?>"><?php echo $this->htmlEscape($_item->getName()) ?></a></p> 
        <?php echo $this->getPriceHtml($_item, true, '-related') ?> 
        <?php if ($this->helper('wishlist')->isAllow()) : ?> 
         <a href="<?php echo $this->getAddToWishlistUrl($_item) ?>" class="link-wishlist"><?php echo $this->__('Add to Wishlist') ?></a> 
        <?php endif; ?> 
       </div> 
      </div> 
     </li> 
    <?php endforeach ?> 
    </ol> 
    <script type="text/javascript">decorateList('block-related', 'none-recursive')</script> 
</div> 
<script type="text/javascript"> 
//<![CDATA[ 
$$('.related-checkbox').each(function(elem){ 
    Event.observe(elem, 'click', addRelatedToProduct) 
}); 

var relatedProductsCheckFlag = false; 
function selectAllRelated(txt){ 
    if (relatedProductsCheckFlag == false) { 
     $$('.related-checkbox').each(function(elem){ 
      elem.checked = true; 
     }); 
     relatedProductsCheckFlag = true; 
     txt.innerHTML="<?php echo $this->__('unselect all') ?>"; 
    } else { 
     $$('.related-checkbox').each(function(elem){ 
      elem.checked = false; 
     }); 
     relatedProductsCheckFlag = false; 
     txt.innerHTML="<?php echo $this->__('select all') ?>"; 
    } 
    addRelatedToProduct(); 
} 

function addRelatedToProduct(){ 
    var checkboxes = $$('.related-checkbox'); 
    var values = []; 
    for(var i=0;i<checkboxes.length;i++){ 
     if(checkboxes[i].checked) values.push(checkboxes[i].value); 
    } 
    if($('related-products-field')){ 
     $('related-products-field').value = values.join(','); 
    } 
} 
//]]> 
</script> 

上面的代碼中的回聲顯示我的網頁上folling代碼。當然,這證明我正確地實現了該塊。

if語句中的所有內容都不顯示。

我花了一些時間尋找解決方案,我試着重建索引,我的相關產品在前端可見。

任何人都知道我該如何解決這個問題?

回答

0

在您的4號線:

<?php if($this->getItems()->getSize() > 0 ? true : false) ?> 

試試吧。

0

在你的代碼類型

var_dump($this->getItems()->getSize()) // does this print NUll, False or > 0 

如果頂部打印NULL或虛假或小於1上面的代碼,然後做

print_r($this->getItems()); 

如果上述打印你所期望的信息然後檢查您的區塊getItems()方法

另外其中是關閉IF爲if($this->getItems()->getSize()):

3

的Magento總是會返回一個較低的數字或0的條件:

$this->getItems()->getSize() 

如果一些/所有相關產品都在用戶的車,因此,雖然他們缺少的項目會出現。

爲了防止這種行爲,複製的核心Magento的 '相關' 類:

/app/code/core/Mage/Catalog/Block/Product/List/Related.php 

本地:

/app/code/local/Mage/Catalog/Block/Product/List/Related.php 

然後註釋掉以下行中的if語句:

if (Mage::helper('catalog')->isModuleEnabled('Mage_Checkout')) { 
    //mod - show all related products, even if they have been added to the cart 
    //Mage::getResourceSingleton('checkout/cart')->addExcludeProductFilter($this->_itemCollection, 
    // Mage::getSingleton('checkout/session')->getQuoteId() 
    //); 
    $this->_addProductAttributesAndPrices($this->_itemCollection); 
} 
+0

儘管不推薦使用這種覆蓋,但在自定義模塊中創建覆蓋值得花時間,我的答案確實解決了我的問題。那謝謝啦! – circlesix

相關問題