Note that there are some explanatory texts on larger screens.

plurals
  1. POMagento All products with images first (list.phtml)
    primarykey
    data
    text
    <p>I want all products that contain images to be listed before products that do not. I have worked with some code and have been able to achieve this however only on a page per page basis. </p> <p>If I have 100 products and 6 products contain images on page 1, then the 6 products will be displayed first, on all subsequent pages there are products with images. </p> <p>I would like all products with images first regardless of what page they are on.</p> <p>The code bellow works based on the default of my site to sort products by price lowest to highest, I am hopping someone can tweak it for me.</p> <pre><code>&lt;?php $_productCollection=$this-&gt;getLoadedProductCollection(); $_helper = $this-&gt;helper('catalog/output'); ?&gt; &lt;?php if(!$_productCollection-&gt;count()): ?&gt; &lt;p class="note-msg"&gt;&lt;?php echo $this-&gt;__('There are no products matching the selection.') ?&gt;&lt;/p&gt; &lt;?php else: ?&gt; &lt;div class="category-products"&gt; &lt;?php echo $this-&gt;getToolbarHtml() ?&gt; &lt;?php // List mode ?&gt; &lt;?php $imgprd=array(); ?&gt; &lt;?php if($this-&gt;getMode()!='grid'): ?&gt; &lt;?php $_iterator = 0; ?&gt; &lt;ol class="products-list" id="products-list"&gt; &lt;?php foreach ($_productCollection as $_product): ?&gt; &lt;?php //you can create test in any part sting of you link image if (strpos($this-&gt;helper('catalog/image')-&gt;init $_product, 'small_image') , "placeholder") ==false): ?&gt; &lt;li class="item&lt;?php if( ++$_iterator == sizeof($_productCollection) ): ?&gt; last&lt;?php endif; ?&gt;"&gt; &lt;?php // Product Image ?&gt; &lt;a href="&lt;?php echo $_product-&gt;getProductUrl() ?&gt;" title="&lt;?php echo $this-&gt;stripTags($this-&gt;getImageLabel($_product, 'small_image'), null, true) ?&gt;" class="product-image"&gt;&lt;img src="&lt;?php echo $this-&gt;helper('catalog/image')-&gt;init($_product, 'small_image')-&gt;resize(135); ?&gt;" width="135" height="135" alt="&lt;?php echo $this-&gt;stripTags($this-&gt;getImageLabel($_product, 'small_image'), null, true) ?&gt;" /&gt;&lt;/a&gt; &lt;?php // Product description ?&gt; &lt;div class="product-shop"&gt; &lt;div class="f-fix"&gt; &lt;?php $_productNameStripped = $this-&gt;stripTags($_product-&gt;getName(), null, true); ?&gt; &lt;h2 class="product-name"&gt;&lt;a href="&lt;?php echo $_product-&gt;getProductUrl() ?&gt;" title="&lt;?php echo $_productNameStripped; ?&gt;"&gt;&lt;?php echo $_helper-&gt;productAttribute($_product, $_product-&gt;getName() , 'name'); ?&gt;&lt;/a&gt;&lt;/h2&gt; &lt;?php if($_product-&gt;getRatingSummary()): ?&gt; &lt;?php echo $this-&gt;getReviewsSummaryHtml($_product) ?&gt; &lt;?php endif; ?&gt; &lt;?php echo $this-&gt;getPriceHtml($_product, true) ?&gt; &lt;?php if($_product-&gt;isSaleable()): ?&gt; &lt;p&gt;&lt;button type="button" title="&lt;?php echo $this-&gt;__('Add to Cart') ?&gt;" class="button btn-cart" onclick="setLocation('&lt;?php echo $this-&gt;getAddToCartUrl($_product) ?&gt;')"&gt;&lt;span&gt;&lt;span&gt;&lt;?php echo $this-&gt;__('Add to Cart') ?&gt;&lt;/span&gt;&lt;/span&gt;&lt;/button&gt;&lt;/p&gt; &lt;?php else: ?&gt; &lt;p class="availability out-of-stock"&gt;&lt;span&gt;&lt;?php echo $this-&gt;__('Out of stock') ?&gt;&lt;/span&gt;&lt;/p&gt; &lt;?php endif; ?&gt; &lt;div class="desc std"&gt; &lt;?php echo $_helper-&gt;productAttribute($_product, $_product-&gt;getShortDescription(), 'short_description') ?&gt; &lt;a href="&lt;?php echo $_product-&gt;getProductUrl() ?&gt;" title="&lt;?php echo $_productNameStripped ?&gt;" class="link-learn"&gt;&lt;?php echo $this-&gt;__('Learn More') ?&gt;&lt;/a&gt; &lt;/div&gt; &lt;ul class="add-to-links"&gt; &lt;?php if ($this-&gt;helper('wishlist')-&gt;isAllow()) : ?&gt; &lt;li&gt;&lt;a href="&lt;?php echo $this-&gt;helper('wishlist')-&gt;getAddUrl($_product) ?&gt;" class="link-wishlist"&gt;&lt;?php echo $this-&gt;__('Add to Wishlist') ?&gt;&lt;/a&gt;&lt;/li&gt; &lt;?php endif; ?&gt; &lt;?php if($_compareUrl=$this-&gt;getAddToCompareUrl($_product)): ?&gt; &lt;li&gt;&lt;span class="separator"&gt;|&lt;/span&gt; &lt;a href="&lt;?php echo $_compareUrl ?&gt;" class="link-compare"&gt;&lt;?php echo $this-&gt;__('Add to Compare') ?&gt;&lt;/a&gt;&lt;/li&gt; &lt;?php endif; ?&gt; &lt;/ul&gt; &lt;/div&gt; &lt;/div&gt; &lt;/li&gt; &lt;?php else: ?&gt; &lt;?php $imgprd[]=$_product ?&gt; &lt;?php endif; ?&gt; &lt;?php endforeach; ?&gt; &lt;?php foreach ($imgprd as $_product): ?&gt; &lt;li class="item&lt;?php if( ++$_iterator == sizeof($_productCollection) ): ?&gt; last&lt;?php endif; ?&gt;"&gt; &lt;?php // Product Image ?&gt; &lt;a href="&lt;?php echo $_product-&gt;getProductUrl() ?&gt;" title="&lt;?php echo $this-&gt;stripTags($this-&gt;getImageLabel($_product, 'small_image'), null, true) ?&gt;" class="product-image"&gt;&lt;img src="&lt;?php echo $this-&gt;helper('catalog/image')-&gt;init($_product, 'small_image')-&gt;resize(135); ?&gt;" width="135" height="135" alt="&lt;?php echo $this-&gt;stripTags($this-&gt;getImageLabel($_product, 'small_image'), null, true) ?&gt;" /&gt;&lt;/a&gt; &lt;?php // Product description ?&gt; &lt;div class="product-shop"&gt; &lt;div class="f-fix"&gt; &lt;?php $_productNameStripped = $this-&gt;stripTags($_product-&gt;getName(), null, true); ?&gt; &lt;h2 class="product-name"&gt;&lt;a href="&lt;?php echo $_product-&gt;getProductUrl() ?&gt;" title="&lt;?php echo $_productNameStripped; ?&gt;"&gt;&lt;?php echo $_helper-&gt;productAttribute($_product, $_product-&gt;getName() , 'name'); ?&gt;&lt;/a&gt;&lt;/h2&gt; &lt;?php if($_product-&gt;getRatingSummary()): ?&gt; &lt;?php echo $this-&gt;getReviewsSummaryHtml($_product) ?&gt; &lt;?php endif; ?&gt; &lt;?php echo $this-&gt;getPriceHtml($_product, true) ?&gt; &lt;?php if($_product-&gt;isSaleable()): ?&gt; &lt;p&gt;&lt;button type="button" title="&lt;?php echo $this-&gt;__('Add to Cart') ?&gt;" class="button btn-cart" onclick="setLocation('&lt;?php echo $this-&gt;getAddToCartUrl($_product) ?&gt;')"&gt;&lt;span&gt;&lt;span&gt;&lt;?php echo $this-&gt;__('Add to Cart') ?&gt;&lt;/span&gt;&lt;/span&gt;&lt;/button&gt;&lt;/p&gt; &lt;?php else: ?&gt; &lt;p class="availability out-of-stock"&gt;&lt;span&gt;&lt;?php echo $this-&gt;__('Out of stock') ?&gt;&lt;/span&gt;&lt;/p&gt; &lt;?php endif; ?&gt; &lt;div class="desc std"&gt; &lt;?php echo $_helper-&gt;productAttribute($_product, $_product-&gt;getShortDescription(), 'short_description') ?&gt; &lt;a href="&lt;?php echo $_product-&gt;getProductUrl() ?&gt;" title="&lt;?php echo $_productNameStripped ?&gt;" class="link-learn"&gt;&lt;?php echo $this-&gt;__('Learn More') ?&gt;&lt;/a&gt; &lt;/div&gt; &lt;ul class="add-to-links"&gt; &lt;?php if ($this-&gt;helper('wishlist')-&gt;isAllow()) : ?&gt; &lt;li&gt;&lt;a href="&lt;?php echo $this-&gt;helper('wishlist')-&gt;getAddUrl($_product) ?&gt;" class="link-wishlist"&gt;&lt;?php echo $this-&gt;__('Add to Wishlist') ?&gt;&lt;/a&gt;&lt;/li&gt; &lt;?php endif; ?&gt; &lt;?php if($_compareUrl=$this-&gt;getAddToCompareUrl($_product)): ?&gt; &lt;li&gt;&lt;span class="separator"&gt;|&lt;/span&gt; &lt;a href="&lt;?php echo $_compareUrl ?&gt;" class="link-compare"&gt;&lt;?php echo $this-&gt;__('Add to Compare') ?&gt;&lt;/a&gt;&lt;/li&gt; &lt;?php endif; ?&gt; &lt;/ul&gt; &lt;/div&gt; &lt;/div&gt; &lt;/li&gt; &lt;?php endforeach; ?&gt; &lt;/ol&gt; &lt;script type="text/javascript"&gt;decorateList('products-list', 'none-recursive')&lt;/script&gt; &lt;?php else: ?&gt; &lt;?php // Grid Mode ?&gt; &lt;?php $_collectionSize = $_productCollection-&gt;count() ?&gt; &lt;?php $_columnCount = $this-&gt;getColumnCount(); $i=0; ?&gt; &lt;?php foreach ($_productCollection as $_product): ?&gt; &lt;?php if (strpos($this-&gt;helper('catalog/image')-&gt;init($_product, 'small_image') , "placeholder")==false): ?&gt; &lt;?php if ($i++%$_columnCount==0): ?&gt; &lt;ul class="products-grid"&gt; &lt;?php endif ?&gt; &lt;li class="item&lt;?php if(($i-1)%$_columnCount==0): ?&gt; first&lt;?php elseif($i%$_columnCount==0): ?&gt; last&lt;?php endif; ?&gt;"&gt; &lt;a href="&lt;?php echo $_product-&gt;getProductUrl() ?&gt;" title="&lt;?php echo $this-&gt;stripTags($this-&gt;getImageLabel($_product, 'small_image'), null, true) ?&gt;" class="product-image"&gt;&lt;img src="&lt;?php echo $this-&gt;helper('catalog/image')-&gt;init($_product, 'small_image')-&gt;resize(135); ?&gt;" width="135" height="135" alt="&lt;?php echo $this-&gt;stripTags($this-&gt;getImageLabel($_product, 'small_image'), null, true) ?&gt;" /&gt;&lt;/a&gt; &lt;h2 class="product-name"&gt;&lt;a href="&lt;?php echo $_product-&gt;getProductUrl() ?&gt;" title="&lt;?php echo $this-&gt;stripTags($_product-&gt;getName(), null, true) ?&gt;"&gt;&lt;?php echo $_helper-&gt;productAttribute($_product, $_product-&gt;getName(), 'name') ?&gt;&lt;/a&gt;&lt;/h2&gt; &lt;?php if($_product-&gt;getRatingSummary()): ?&gt; &lt;?php echo $this-&gt;getReviewsSummaryHtml($_product, 'short') ?&gt; &lt;?php endif; ?&gt; &lt;?php echo $this-&gt;getPriceHtml($_product, true) ?&gt; &lt;div class="actions"&gt; &lt;?php if($_product-&gt;isSaleable()): ?&gt; &lt;button type="button" title="&lt;?php echo $this-&gt;__('Add to Cart') ?&gt;" class="button btn-cart" onclick="setLocation('&lt;?php echo $this-&gt;getAddToCartUrl($_product) ?&gt;')"&gt;&lt;span&gt;&lt;span&gt;&lt;?php echo $this-&gt;__('Add to Cart') ?&gt;&lt;/span&gt;&lt;/span&gt;&lt;/button&gt; &lt;?php else: ?&gt; &lt;p class="availability out-of-stock"&gt;&lt;span&gt;&lt;?php echo $this-&gt;__('Out of stock') ?&gt;&lt;/span&gt;&lt;/p&gt; &lt;?php endif; ?&gt; &lt;ul class="add-to-links"&gt; &lt;?php if ($this-&gt;helper('wishlist')-&gt;isAllow()) : ?&gt; &lt;li&gt;&lt;a href="&lt;?php echo $this-&gt;helper('wishlist')-&gt;getAddUrl($_product) ?&gt;" class="link-wishlist"&gt;&lt;?php echo $this-&gt;__('Add to Wishlist') ?&gt;&lt;/a&gt;&lt;/li&gt; &lt;?php endif; ?&gt; &lt;?php if($_compareUrl=$this-&gt;getAddToCompareUrl($_product)): ?&gt; &lt;li&gt;&lt;span class="separator"&gt;|&lt;/span&gt; &lt;a href="&lt;?php echo $_compareUrl ?&gt;" class="link-compare"&gt;&lt;?php echo $this-&gt;__('Add to Compare') ?&gt;&lt;/a&gt;&lt;/li&gt; &lt;?php endif; ?&gt; &lt;/ul&gt; &lt;/div&gt; &lt;/li&gt; &lt;?php if ($i%$_columnCount==0 || $i==$_collectionSize): ?&gt; &lt;/ul&gt; &lt;?php endif ?&gt; &lt;?php else: ?&gt; &lt;?php $imgprd[]=$_product ?&gt; &lt;?php endif; ?&gt; &lt;?php endforeach ?&gt; &lt;?php foreach ($imgprd as $_product): ?&gt; &lt;?php if ($i++%$_columnCount==0): ?&gt; &lt;ul class="products-grid"&gt; &lt;?php endif ?&gt; &lt;li class="item&lt;?php if(($i-1)%$_columnCount==0): ?&gt; first&lt;?php elseif($i%$_columnCount==0): ?&gt; last&lt;?php endif; ?&gt;"&gt; &lt;a href="&lt;?php echo $_product-&gt;getProductUrl() ?&gt;" title="&lt;?php echo $this-&gt;stripTags($this-&gt;getImageLabel($_product, 'small_image'), null, true) ?&gt;" class="product-image"&gt;&lt;img src="&lt;?php echo $this-&gt;helper('catalog/image')-&gt;init($_product, 'small_image')-&gt;resize(135); ?&gt;" width="135" height="135" alt="&lt;?php echo $this-&gt;stripTags($this-&gt;getImageLabel($_product, 'small_image'), null, true) ?&gt;" /&gt;&lt;/a&gt; &lt;h2 class="product-name"&gt;&lt;a href="&lt;?php echo $_product-&gt;getProductUrl() ?&gt;" title="&lt;?php echo $this-&gt;stripTags($_product-&gt;getName(), null, true) ?&gt;"&gt;&lt;?php echo $_helper-&gt;productAttribute($_product, $_product-&gt;getName(), 'name') ?&gt;&lt;/a&gt;&lt;/h2&gt; &lt;?php if($_product-&gt;getRatingSummary()): ?&gt; &lt;?php echo $this-&gt;getReviewsSummaryHtml($_product, 'short') ?&gt; &lt;?php endif; ?&gt; &lt;?php echo $this-&gt;getPriceHtml($_product, true) ?&gt; &lt;div class="actions"&gt; &lt;?php if($_product-&gt;isSaleable()): ?&gt; &lt;button type="button" title="&lt;?php echo $this-&gt;__('Add to Cart') ?&gt;" class="button btn-cart" onclick="setLocation('&lt;?php echo $this-&gt;getAddToCartUrl($_product) ?&gt;')"&gt;&lt;span&gt;&lt;span&gt;&lt;?php echo $this-&gt;__('Add to Cart') ?&gt;&lt;/span&gt;&lt;/span&gt;&lt;/button&gt; &lt;?php else: ?&gt; &lt;p class="availability out-of-stock"&gt;&lt;span&gt;&lt;?php echo $this-&gt;__('Out of stock') ?&gt;&lt;/span&gt;&lt;/p&gt; &lt;?php endif; ?&gt; &lt;ul class="add-to-links"&gt; &lt;?php if ($this-&gt;helper('wishlist')-&gt;isAllow()) : ?&gt; &lt;li&gt;&lt;a href="&lt;?php echo $this-&gt;helper('wishlist')-&gt;getAddUrl($_product) ?&gt;" class="link-wishlist"&gt;&lt;?php echo $this-&gt;__('Add to Wishlist') ?&gt;&lt;/a&gt;&lt;/li&gt; &lt;?php endif; ?&gt; &lt;?php if($_compareUrl=$this-&gt;getAddToCompareUrl($_product)): ?&gt; &lt;li&gt;&lt;span class="separator"&gt;|&lt;/span&gt; &lt;a href="&lt;?php echo $_compareUrl ?&gt;" class="link-compare"&gt;&lt;?php echo $this-&gt;__('Add to Compare') ?&gt;&lt;/a&gt;&lt;/li&gt; &lt;?php endif; ?&gt; &lt;/ul&gt; &lt;/div&gt; &lt;/li&gt; &lt;?php if ($i%$_columnCount==0 || $i==$_collectionSize): ?&gt; &lt;/ul&gt; &lt;?php endif ?&gt; &lt;?php endforeach ?&gt; &lt;script type="text/javascript"&gt;decorateGeneric($$('ul.products-grid'), ['odd','even','first','last'])&lt;/script&gt; &lt;?php endif; ?&gt; &lt;div class="toolbar-bottom"&gt; &lt;?php echo $this-&gt;getToolbarHtml() ?&gt; &lt;/div&gt; </code></pre> <p></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload