2

I created a Yes or No attribute called 'feature'. I have a Product Tab slider on the home page. On that tab it had Best Seller, Latest Product and Latest Deal. I want to add Feature Product (product is 'feature' attribute is set to "Yes") also on that tab.

<div class="container">
<?php
/**
 * Tab Links
 */
$class = $_compareUrl = $currentDate = '';
?>
    <div class="best-new-product-tab">
        <ul class="TabNav">
            <li id="defaultTab"><a href="#bestsellingproducts"><?php echo Mage::helper('marketplace')->__('Bestsellers'); ?></a></li>
            <li><a href="#newproducts"><?php echo Mage::helper('marketplace')->__('Latest Products'); ?></a></li>
            <li><a href="#deal"><?php echo Mage::helper('marketplace')->__('Latest Deal'); ?></a></li>




        </ul>
    </div>
    <?php 
    /**
     * Best seller products
     */?>
    <div class="best-new-product-tab-content">
        <div id="bestsellingproducts">
         <?php $bestSellingCollection = $this->bestSellerCollection ();
                $categoryCount = count ( $bestSellingCollection );
                if ($categoryCount >= 1) { ?>                   

                <div class="products-grid <?php if($categoryCount>4){ echo 'slider-carousel'; } else { echo 'no-slider'; } ?>">     
                    <?php $increment =1; 
                    foreach ( $bestSellingCollection as $_bestSellingCollection ) { ?>
                        <div class="slider <?php if($increment==4) { echo 'last';} ?>">

                            <div class="product_image_div">
                              <?php
                                /**
                                 * Display product image
                                 */
                                ?>
                                 <a href="<?php echo $_bestSellingCollection->getProductUrl() ?>" title="<?php echo $this->escapeHtml($_bestSellingCollection->getName()) ?>" class="product-image">
                                    <?php $_imgSize = 260; ?>
                                    <img src="<?php echo $this->helper('catalog/image')->init($_bestSellingCollection, 'small_image')->resize($_imgSize); ?>" alt="<?php echo $this->stripTags($this->getImageLabel($_bestSellingCollection, 'small_image'), null, true) ?>" />
                                </a>
                        <h3 class="product-name">
                            <a href="<?php echo $_bestSellingCollection->getProductUrl(); ?>">
                               <?php $prName = $_bestSellingCollection->getName ();
                                    $lenPrName = strlen ( $_bestSellingCollection->getName () );
                                    $subPrName = substr ( $prName, 0, 26 );
                                    if ($lenPrName >= 26) {
                                        $prNameFix = $subPrName . "...";
                                    } else {
                                        $prNameFix = $prName;
                                    }
                                    echo $prNameFix;?>
                            </a>
                        </h3>
                        <div class="products_gridview_page">
                            <div class="price-box">
                                <span class="regular-price"> 
                                    <span class="price">
                                <?php
                                    /**
                                     * Display Product Price
                                     */
                        $newPrice = $_bestSellingCollection->getPrice ();
                        $newSplPrice = $_bestSellingCollection->getSpecialPrice ();
                        $newDealExpireDate = $_bestSellingCollection->getspecial_to_date ();
                        if (! empty ( $newSplPrice ) && ($newDealExpireDate != '') && ($newDealExpireDate >= $currentDate)) {   
                            echo Mage::helper ( 'core' )->currency ( $newSplPrice, true, false );
                        } else {
                            echo Mage::helper ( 'core' )->currency ( $newPrice, true, false );
                        }
                        ?>
                                   </span>
                               </span>
                            </div>
                        </div>
                        <?php
                        /**
                         * Display Add to cart, add to wishlist and compare
                         */
                        ?>
                            <div class="actions">

                            <ul class="add-to-links">
                                <li>
                                    <a  href="<?php echo $this->helper('wishlist')->getAddUrl($_bestSellingCollection) ?>"  title="<?php echo $this->__('Add to Wishlist') ?>"  class="link-wishlist <?php echo $class; ?>">
                                        <?php echo $this->__('Add to Wishlist') ?>
                                    </a>
                                </li>
                                <li>
                                    <span class="separator">|</span> 

                                    <a href="<?php 
                                    echo Mage::helper('catalog/product_compare')->getAddUrl($_bestSellingCollection);
                                    ?>" class="link-compare" title="<?php echo $this->__('Add to Compare') ?>">

                                        <?php echo $this->__('Add to Compare') ?>
                                    </a>
                                </li>
                            </ul>               
                                 <?php if($_bestSellingCollection->isSaleable()){ ?>
                                    <button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo Mage::helper('checkout/cart')->getAddUrl($_bestSellingCollection) ?>')">
                                        <span><span><?php echo $this->__('Add to Cart') ?></span></span>
                                    </button>
                                <?php } else {?>
                                    <p class="availability out-of-stock">
                                        <span><?php echo $this->__('Out of stock') ?></span>
                                    </p>
                                <?php } ?>
                            </div>                  
                        <?php
                        /**
                         * Check product is set as new or not
                         */
                        ?>
                    <?php echo $this->helper('marketplace/marketplace')->getLabel($_bestSellingCollection);  ?> 
                     </div> 
                     </div>         
                    <?php $increment++; } ?>                                
                </div>  
                <?php } 
                else {          
                    echo $this->__("No bestselling product[s] available to show");
                }?>
            </div>

        <div id="newproducts">
                    <?php $newProducts = $this->getNewproduct ();
                    $newCount = count ( $newProducts );
                    if ($newCount >= 1) {?>

                    <div class="products-grid <?php if($newCount>4){ echo 'slider-carousel'; } else { echo 'no-slider'; } ?>">
                    <?php $increment =1;    
                    foreach ( $newProducts as $new ) {      ?>
                             <div class="slider <?php if($increment==4) { echo 'last';} ?>">                    

                            <?php   $newproduct = $this->helper ( 'catalog/image' )->init ( $new, 'small_image' )->constrainOnly ( TRUE )->keepAspectRatio ( FALSE )->keepFrame ( FALSE )->resize ( 226, 226 );?>  
                            <?php
                            /**
                             * Display New Products of this seller
                             */
                            ?>    


                        <div class="product_image_div">
                        <a href="<?php echo $new->getProductUrl() ?>" title="<?php echo $this->escapeHtml($new->getName()) ?>" class="product-image">
                                    <?php $_imgSize = 260; ?>
                                    <img src="<?php echo $this->helper('catalog/image')->init($new, 'small_image')->resize($_imgSize); ?>" alt="<?php echo $this->stripTags($this->getImageLabel($new, 'small_image'), null, true) ?>" />
                                </a>


                        <div class="products-grid-details">
                            <h2 class="product-name">
                                <a href="<?php echo $new->getProductUrl(); ?>">
                                            <?php
                            $newProductName = $new->getName ();
                            $newLengthProductName = strlen ( $new->getName () );
                            $newSubProductName = substr ( $newProductName, 0, 26 );
                            if ($newLengthProductName >= 26) {
                                $newProductNameFix = $newSubProductName . "...";
                            } else {
                                $newProductNameFix = $newProductName;
                            }
                            echo $newProductNameFix;
                            ?>

                                        </a>
                            </h2>


                                </div>
                        <div class="products_gridview_page">
                            <div class="price-box">
                                <span class="regular-price"> <span class="price">

                                                <?php
                            /**
                             * Display Product Price
                             */
                            $newPrice = $new->getPrice ();
                            $newSplPrice = $new->getSpecialPrice ();
                            $newDealExpireDate = $new->getspecial_to_date ();
                            if (! empty ( $newSplPrice ) && ($newDealExpireDate != '') && ($newDealExpireDate >= $currentDate)) {

                                echo Mage::helper ( 'core' )->currency ( $newSplPrice, true, false );
                            } else {
                                echo Mage::helper ( 'core' )->currency ( $newPrice, true, false );
                            }
                            ?>
                                            </span></span>
                            </div>
                        </div>        

                    </div>
                                <?php
                            /**
                             * Display Add to cart, add to wishlist and compare
                             */
                            ?>
                             <div class="actions">
                        <ul class="add-to-links">
                            <li><a
                                href="<?php echo $this->helper('wishlist')->getAddUrl($new) ?>"
                                title="<?php echo $this->__('Add to Wishlist') ?>"
                                class="link-wishlist <?php echo $class; ?>"><?php echo $this->__('Add to Wishlist') ?></a></li>
                            <li><span class="separator">|</span> <a
                                href="<?php echo Mage::helper('catalog/product_compare')->getAddUrl($new);  ?>" class="link-compare" title="<?php echo $this->__('Add to Compare') ?>"><?php echo $this->__('Add to Compare') ?></a></li>

                        </ul>
                       <?php if($new->isSaleable()): ?>
                        <button type="button"
                            title="<?php echo $this->__('Add to Cart') ?>"
                            class="button btn-cart"
                            onclick="setLocation('<?php echo Mage::helper('checkout/cart')->getAddUrl($new) ?>')">
                            <span><span><?php echo $this->__('Add to Cart') ?></span></span>
                        </button>
                    <?php else: ?>
                        <p class="availability out-of-stock">
                            <span><?php echo $this->__('Out of stock') ?></span>
                        </p>
                    <?php endif; ?>
                    </div>

                    <?php
                            /**
                             * Check product is set as new or not
                             */
                            ?>
                    <?php echo $this->helper('marketplace/marketplace')->getLabel($new);  ?>                        
                            </div>   
                        <?php $increment++; } ?>
             </div>
          <?php } 
          else {

                echo $this->__("No new product[s] available to show");
                }?> 
</div>

<div id ="deal">


<?php $products = $this->getProductCollection()->setPageSize(100);
$coreHelper = $this->helper('core');
$class = '';
 if (count($products) > 0){ ?>


<div class="products-grid <?php if(count($products)>4){ echo 'slider-carousel-deal'; } else { echo 'no-slider'; } ?>">  
    <?php $i = 1;  
    foreach ($products as $item){ ?>
                <div class="slider <?php if($i==4) { echo 'last';} ?>"> 
                    <?php
                    $originalPrice = $item->getPrice(); 
                    $discountPrice = $item->getSpecialPrice(); 
                    $savings = $originalPrice - $discountPrice; 
                    $savingsPercentage = round(($savings / $originalPrice) * 100, 0);
                    for ($increment = 0; $increment < 1; $increment++) { ?>                       
                          <div class="product_image_div">
                          <?php 
                          /**
                           * Display product image
                           */
                          ?>
                            <a class="product-image" href="<?php echo $item->getProductUrl() ?>" >
                               <?php $_imgSize = 260; ?>
                                    <img src="<?php echo $this->helper('catalog/image')->init($item, 'small_image')->resize($_imgSize); ?>" alt="<?php echo $this->stripTags($this->getImageLabel($item, 'small_image'), null, true) ?>" />
                            </a>
                             <h3 class="product-name">
                        <a href="<?php echo $item->getProductUrl(); ?>">
                           <?php
                           $prName = $item->getName();
                           $lenPrName = strlen($item->getName());
                           $subPrName = substr($prName, 0, 26);
                            if ($lenPrName >= 26) {
                               $prNameFix = $subPrName . "...";
                            } else {
                               $prNameFix = $prName;
                            }
                               echo $prNameFix;
                            ?>
                        </a>
                </h3>
                <?php 
                    /**
                     * Display of price details
                     */
                    ?>
                            <div class="product-details">

                            <?php if($item->getPrice() > $item->getSpecialPrice()){ ?>

                                <span class="original_price"> 
                                    <?php echo $coreHelper->currency($item->getPrice(), true, false); ?>
                                </span> 
                                <span class="offer_price"> 
                                    <?php echo $coreHelper->currency($item->getSpecialPrice(), true, false); ?>
                                </span>                                
                                <span class="discount_label">
                                     <?php echo $this->__('Discount: ') ?> 
                                </span> 
                                <span class="discount_price" <?php echo $this->getIdSuffix() ?>>
                                      <?php echo $savingsPercentage, '%';?>
                                      <?php echo $this->__('Off');
                                      ?>
                                </span>

                                <?php }else{ ?>
                                <span> 
                                    <?php echo $coreHelper->currency($item->getPrice(), true, false); ?>
                                </span> 
                                <?php } ?>
                            </div>
                            <?php 
                            /**
                             * Display Add to cart, add to wishlist and compare
                             */
                            ?>  <div class="actions">
                    <ul class="add-to-links">
                              <li><a href="<?php echo $this->helper('wishlist')->getAddUrl($item) ?>" title="<?php echo $this->__('Add to Wishlist') ?>" class="link-wishlist <?php echo $class; ?>"><?php echo $this->__('Add to Wishlist') ?></a></li>
                            <li><span class="separator">|</span> <a href="<?php 
                            $_compareUrl = Mage::helper('catalog/product_compare')->getAddUrl($item);
                            echo $_compareUrl ?>" class="link-compare" title="<?php echo $this->__('Add to Compare') ?>"><?php echo $this->__('Add to Compare') ?></a></li>
                        </ul>
                       <?php if($item->isSaleable()): ?>
                        <button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo Mage::helper('checkout/cart')->getAddUrl($item) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button>
                    <?php else: ?>
                        <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
                    <?php endif; ?>
                    </div>
                    <?php 
                    /**
                     * Check product is set as new or not
                     */
                    ?>
                    <?php echo $this->helper('marketplace/marketplace')->getLabel($item);  ?>
                </div>
                    <?php } ?>
                     </div>
                    <?php $i++; } ?>


            <script>
                decorateList('best-sidebar', 'none-recursive');
            </script>               
            </div>
 <?php } ?>

<div id="feature">



</div>


</div>




    </div>
</div>

1 Answer 1

2

You can add addAttributeToFilter to the collection for feature attribute like

$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToFilter('feature', array('eq' => 'Yes')); // or 0 or 1 iff you are using Boolean values

foreach ($collection as $product) {
    //var_dump($product);
    var_dump($product->getData());
}
5
  • 1
    Product Collection is uses EAV so, it you can not use addFieldToFilter, You need to use addAttributeToFIlter. Commented Oct 28, 2015 at 13:01
  • and where did i used "addFieldToFilter" ? Commented Oct 28, 2015 at 13:02
  • On the very first line of your answer. Commented Oct 28, 2015 at 13:04
  • my bad , code is correct though. rather than down voting could have edited it :p Commented Oct 28, 2015 at 13:05
  • Sure, i will upvote, and edit your answer. Commented Oct 28, 2015 at 13:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.