2012-07-15 76 views
2

我需要幫助隱藏沒有圖像的Magento產品或首先使用圖像顯示產品。Magento - 如何隱藏沒有圖像的產品

我看到這個$ this-> getLoadedProductCollection() - > setOrder('image'/ *或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

基於答案here但添加非等效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集合,因此需要對其進行克隆,清除,然後裝。請參閱here

+0

任何兼容請?我真的需要完成這件事。請幫幫我。 – user1527556 2012-07-17 15:18:03

+0

我更新了我的答案。對不起,以前的代碼不會工作,因爲集合已經加載。新的應該爲你工作,我測試了它。 – 2012-07-18 02:14:12

+0

非常感謝你。這工作完美。 – user1527556 2012-07-18 16:21:19

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's answer他通過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(For線#157分頁以反映過濾)並將其複製到Folder:/ app/code/local/Mage/Catalog/Block/Product /(本地更改)

  1. Then Admin> Configuration> Catalog >前端>使用平面目錄產品到「否」

由於這種過濾不符合使用「平目錄產品」