Magento产品pag - 显示缺货的相关产品

时间:2016-07-08 19:00:38

标签: javascript php jquery magento product

我在PHP方面不是很有经验,但我的目标是,如果缺货,相关的产品样本不会显示在页面上。(在屏幕截图中突出显示)

Related product swatches are highlighted

这是PHP代码:

    <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->escapeHtml($_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->escapeHtml($_item->getName()) ?>" /></a>
                <div class="product-details">
                    <p class="product-name"><a href="<?php echo $_item->getProductUrl() ?>"><?php echo $this->escapeHtml($_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>

这是相关的jQuery / JS:

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

任何帮助将不胜感激,谢谢!

2 个答案:

答案 0 :(得分:1)

您确定显示缺货产品选项设置为吗? (系统 - &gt;配置 - &gt;广告资源)

您还可以尝试将$_item->isAvailable()添加到if语句。

答案 1 :(得分:0)

The Solution I ended up going with: adding $_item->isAvailable() to if statement gives the desired effect of related products that are out of stock will not display as a related product swatch on the product page.