Magento相关产品不显示

时间:2012-11-05 15:24:34

标签: php magento

我正在尝试将相关产品块显示在我的产品详细信息页面上。 我在相应的.phtml文件中有下拉代码

<?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>

代码上方的回显显示在我的页面上。这当然证明我正确地实现了块。

if语句中的所有内容都没有显示。

我花了一些时间寻找解决方案,我尝试重建索引,我的相关产品在前端可见。

任何人都知道如何解决这个问题?

3 个答案:

答案 0 :(得分: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);
}

答案 1 :(得分:1)

在你的4行:

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

试试吧。

答案 2 :(得分:0)

在代码类型的顶部

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

如果上面的代码打印为NULL或false或小于1,则执行

print_r($this->getItems());

如果以上都不打印您预期的信息,请检查您的块getItems()方法

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

的结束IF也在哪里