Magento - 如何隐藏没有图片的产品

3
我需要帮助隐藏没有图片的Magento产品或首先显示带有图片的产品。
我在另一个问题的回答中看到了这个代码:$this->getLoadedProductCollection()->setOrder('image'/*or small_image*/, 'desc') ,它可以优先加载所有带有图片的产品。但是对我来说,它不起作用。
我使用的是Magento 1.6.2版本。
谢谢提前。
更新:请查看我的完整list.phtml代码。我认为我做错了什么。谢谢。
<?php
$_productCollection=$this->getLoadedProductCollection();
$_productCollection->addAttributeToFilter('image', array('neq' => 'no_selection'));
$_helper = $this->helper('catalog/output');
$now = date("Y-m-d H:m:s");     

?>
<?php if(!$_productCollection->count()): ?>
<p class="note-msg"><?php echo $this->__('There are no products matching the selection.') ?></p>
<?php else: ?>
<div class="category-products">
<?php echo $this->getToolbarHtml() ?>
<?php // List mode ?>
<?php if($this->getMode()!='grid'): ?>
<?php $_iterator = 0; ?>
<ol class="products-list" id="products-list">
<?php foreach ($_productCollection as $_product): ?>
    <li class="item<?php if( ++$_iterator == sizeof($_productCollection) ): ?> last<?php endif; ?>">
            <?php $newFromDate = $_product->getNewsFromDate(); ?>
            <?php $newToDate = $_product->getNewsToDate(); ?>                                               
            <?php if($newFromDate < $now && $newToDate > $now): ?>
                    <div class="newproduct_grid"><?php echo $this->__('New Product') ?></div>
            <?php endif; ?>     
        <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" class="product-image"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(192); ?>" width="192" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" /></a>
        <?php // Product description ?>
        <div class="product-shop">
            <div class="f-fix">
                <?php $_productNameStripped = $this->stripTags($_product->getName(), null, true); ?>
                <h2 class="product-name"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $_productNameStripped; ?>"><?php echo $_helper->productAttribute($_product, $_product->getName() , 'name'); ?></a></h2>
                <?php if($_product->getRatingSummary()): ?>
                <?php echo $this->getReviewsSummaryHtml($_product) ?>
                <?php endif; ?>
                <div id="productimgover<?php echo $_product->getId()?>" style="display: none;"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(64); ?>" width="64" height="64" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" /></div>
                <div id='productname<?php echo $_product->getId()?>' style='display:none'><?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?></div>
                <div class="desc std">
                    <?php echo $_helper->productAttribute($_product, $_product->getShortDescription(), 'short_description') ?>
                    <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $_productNameStripped ?>" class="link-learn"><?php echo $this->__('Learn More') ?></a>
                </div>
                <ul class="add-to-links">
                    <?php if ($this->helper('wishlist')->isAllow()) : ?>
                        <li><a href="<?php echo $this->helper('wishlist')->getAddUrl($_product) ?>" class="link-wishlist"><?php echo $this->__('Add to Wishlist') ?></a></li>
                    <?php endif; ?>
                    <?php if($_compareUrl=$this->getAddToCompareUrl($_product)): ?>
                        <li><span class="separator">|</span> <a href="<?php echo $_compareUrl ?>" class="link-compare"><?php echo $this->__('Add to Compare') ?></a></li>
                    <?php endif; ?>
                </ul>
            </div>
            <div style="clear: both;"></div>
        </div>
        <div class="addtocont">
            <?php echo $this->getPriceHtml($_product, true) ?>
            <?php if($_product->isInStock()): ?>
                                    <?php if ( !($_product->getTypeInstance(true)->hasRequiredOptions($_product) || $_product->isGrouped()) ) { ?>
                            <p><button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocationAjax('<?php echo $this->getAddToCartUrl($_product) ?>','<?php echo $_product->getId()?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button></p>

                        <?php } else { ?>
                            <button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="showOptions('<?php echo $_product->getId()?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button>
                            <a href='<?php echo $this->getUrl('ajax/index/options',array('product_id'=>$_product->getId()));?>' class='fancybox' id='fancybox<?php echo $_product->getId()?>' style="display: none;" ><?php echo $this->__('Add to Cart') ?></a>
                        <?php }  ?>
            <?php else: ?>
            <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
            <?php endif; ?>            
        </div>
    </li>
<?php endforeach; ?>
</ol>
<script type="text/javascript">decorateList('products-list', 'none-recursive')</script>

<?php else: ?>

<?php // Grid Mode ?>

<?php $_collectionSize = $_productCollection->count() ?>
<?php $_columnCount = $this->getColumnCount(); ?>
<?php $i=0; foreach ($_productCollection as $_product): ?>
    <?php if ($i++%$_columnCount==0): ?>
    <ul class="products-grid">
    <?php endif ?>
        <li class="item<?php if(($i-1)%$_columnCount==0): ?> first<?php elseif($i%$_columnCount==0): ?> last<?php endif; ?>">
            <?php if($_product->getRatingSummary()): ?>
                <?php echo $this->getReviewsSummaryHtml($_product, 'short') ?>
            <?php endif; ?>
            <?php $newFromDate = $_product->getNewsFromDate(); ?>
            <?php $newToDate = $_product->getNewsToDate(); ?>                                               
            <?php if($newFromDate < $now && $newToDate > $now): ?>
                    <div class="newproduct_grid"><?php echo $this->__('New Product') ?></div>
            <?php endif; ?>
            <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" class="product-image"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(220); ?>" width="220" height="220" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" /></a>
            <div id="productimgover<?php echo $_product->getId()?>" style="display: none;"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(64); ?>" width="64" height="64" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" /></div>
            <div class="moreinfo">
                <h2 class="product-name"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($_product->getName(), null, true) ?>"><?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?></a></h2>
                <div id='productname<?php echo $_product->getId()?>' style='display:none'><?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?></div>
                <?php echo $this->getPriceHtml($_product, true) ?>
                    <?php if($_product->isInStock()): ?>
                        <?php if ( !($_product->getTypeInstance(true)->hasRequiredOptions($_product) || $_product->isGrouped()) ) { ?>
                            <p><button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocationAjax('<?php echo $this->getAddToCartUrl($_product) ?>','<?php echo $_product->getId()?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button></p>

                        <?php } else { ?>
                            <button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="showOptions('<?php echo $_product->getId()?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button>
                            <a href='<?php echo $this->getUrl('ajax/index/options',array('product_id'=>$_product->getId()));?>' class='fancybox' id='fancybox<?php echo $_product->getId()?>' style="display: none;" ><?php echo $this->__('Add to Cart') ?></a>
                        <?php }  ?>
                    <?php else: ?>
                        <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
                    <?php endif; ?>
                    <div class="clear"></div>
                    <ul class="add-to-links">
                        <?php if ($this->helper('wishlist')->isAllow()) : ?>
                            <li><a href="<?php echo $this->helper('wishlist')->getAddUrl($_product) ?>" class="link-wishlist"><?php echo $this->__('Add to Wishlist') ?></a></li>
                        <?php endif; ?>
                        <?php if($_compareUrl=$this->getAddToCompareUrl($_product)): ?>
                            <li><span class="separator">|</span> <a href="<?php echo $_compareUrl ?>" class="link-compare"><?php echo $this->__('Add to Compare') ?></a></li>
                        <?php endif; ?>
                    </ul>
                    <div class="clear"></div>
            </div>
        </li>
    <?php if ($i%$_columnCount==0 || $i==$_collectionSize): ?>
    </ul>
    <?php endif ?>
    <?php endforeach ?>
    <script type="text/javascript">decorateGeneric($$('ul.products-grid'), ['odd','even','first','last'])</script>

<?php endif; ?>
    <script type="text/javascript">
    jQuery.noConflict();
    jQuery('.fancybox').fancybox({
        hideOnContentClick : true,
        width: 582,
        autoDimensions: true,
        type : 'iframe',
        showTitle: false,
        scrolling: 'no',
        onComplete: function(){ //Resize the iframe to correct size
        jQuery('#fancybox-frame').load(function() { // wait for frame to load and then gets it's height
            jQuery('#fancybox-content').height(jQuery(this).contents().find('body').height()+30);
            jQuery.fancybox.resize();
         });

        }
    });        
    function showOptions(id){
        jQuery('#fancybox'+id).trigger('click');
    }
    function setAjaxData(data,iframe){
        if(data.status == 'ERROR'){
            alert(data.message);
        }else{
            if(jQuery('.block-cart')){
                jQuery('.block-cart').replaceWith(data.sidebar);
            }
            if(jQuery('.header .block-cart-header')){
                jQuery('.header .block-cart-header').replaceWith(data.topcart);
            } 
            jQuery.fancybox.close();
        }
    }
    function setLocationAjax(url,id){
        url += 'isAjax/1';
        url = url.replace("checkout/cart","ajax/index");
        var msgHtml;
        var productImg = jQuery('#productimgover' + id).html();
        var windowOver = jQuery('#addedoverlay');
        var windowBox = jQuery('#added');
        var titleForBox = jQuery('#productname' + id).text();
        windowOver.show();
        windowBox.show().css({
            backgroundImage: "url('<?php echo $this->getSkinUrl('images/loading.gif')?>')"
        });
        try {
            jQuery.ajax( {
                url : url,
                dataType : 'json',
                success : function(data) {
                    if(data.status == 'SUCCESS'){    
                        msgHtml = '<div style="float:left;">' + productImg + '</div><em>' + titleForBox + '</em> <?php echo $this->__('was successfully added to your shopping cart.') ?><div style="clear:both;"></div><a id="hidewindow" href="javascript:void(0);"><?php echo $this->__('Continue shopping') ?></a>&nbsp;<a href="<?php echo $this->getUrl('checkout/cart')?>"><?php echo $this->__('View cart & checkout') ?></a>';             
                    }else{
                        msgHtml = '<p class="error-msg" style="margin-bottom:15px;">' + data.message + '</p><a id="hidewindow" href="javascript:void(0);"><?php echo $this->__('Continue shopping') ?></a>&nbsp;<a href="<?php echo $this->getUrl('checkout/cart')?>"><?php echo $this->__('View cart & checkout') ?></a>';
                    }            
                    setAjaxData(data,false);

                    windowBox.css({
                          backgroundImage: 'none'
                    }).html(msgHtml);                      
                    windowOver.one('click',function(){
                        hidewindow(windowBox,windowOver);                    
                    });        

                    jQuery('#hidewindow').click(function(){
                        hidewindow(windowBox,windowOver);                    
                    });                        
                }
            });
        } catch (e) {
        }
    }
    function hidewindow(windowBox,windowOver){
        windowOver.fadeOut(400);
        windowBox.fadeOut(400).html('');    
    }        
    </script>
<div class="toolbar-bottom">
    <?php echo $this->getToolbarHtml() ?>
</div>

3个回答

5

基于这里的回答,但是添加neq no_selection,这将使您仅获取带有图片的产品。

//this builds a collection that's analagous to 
//select * from products where image != 'no_selection'
$products = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('image', array('neq' => 'no_selection'));

foreach($products as $product)
{

}    

在产品列表页面,您可以像这样做:
//$_productCollection=$this->getLoadedProductCollection();
$_productCollection = clone $this->getLoadedProductCollection();
$_productCollection->clear()
                   ->addAttributeToFilter('image', array('neq' => 'no_selection'))
                   ->load();

编辑

getLoadedProductCollection集合已经加载,因此需要进行克隆、清除然后重新加载。请参见此处


有人可以帮忙吗?我真的需要完成这个任务。请帮帮我。 - user1527556
我更新了我的回答。之前的代码不可行,因为集合已经载入。新的代码应该可以解决你的问题,我已经测试过了。 - Dreaded semicolon
没问题。很高兴它对你有用。如果它对你有用,请将其标记为答案。 - Dreaded semicolon
@Dreadedsemicolon 这段代码之前可用,但现在不工作了,我发现我的列表页上有带图像的产品。你有什么猜测为什么这段代码不起作用了吗? - shnozolla
不确定,特别是在Magento中,可能有很多因素在起作用,您是否升级?您是否安装了任何新插件?您是否更换了模板?我认为最好将其放在一个单独的问题中,然后您可以提供更多细节。 - Dreaded semicolon

0

我尝试了Dreaded Semicolon的解决方案,它很好用,但后来我发现它停止工作了。此外,我注意到它有时候可以工作,然后莫名其妙地随机停止工作。

我猜测这可能与模型有关,也许只有在某些情况下才会使主图像可用于产品集合(这就解释了为什么有时候它可以工作,有时候不行)。

无论如何,如果您在使用该解决方案时遇到问题,并且注意到没有图像的产品仍在显示,请尝试以下操作:

替换:

'image'

使用:

'small_image'

像这样:

//$_productCollection=$this->getLoadedProductCollection();
$_productCollection = clone $this->getLoadedProductCollection();
$_productCollection->clear()
                   ->addAttributeToFilter('small_image', array('neq' => 'no_selection'))
                   ->load();

您也可以尝试 R.S的回答,他通过XML将图像添加到页面中。您可能还需要使用以下方式将所有属性添加到集合中:

->addAttributeToSelect('*')

0

我的版本是Magento 1.8.0.1,我进行了以下操作:

  1. 添加:

$collection->addAttributeToFilter('small_image', array('neq' => "no_selection"));

到/app/code/core/Mage/Catalog/Block/Product/List.php的第157行(为了使分页反映筛选结果),并将其复制到文件夹:/app/code/local/Mage/Catalog/Block/Product/(本地更改)

  1. 然后进入管理界面 > 配置 > 目录 > 前端 > 使用平面目录产品设置为“否”

由于此筛选与“平面目录产品”的使用不兼容。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接