1

How to get Special price less or not

  <?php 

$categoryIds = array(469);//category id

$todayDate = date('m/d/y');
        $tomorrow = mktime(0, 0, 0, date('m'), date('d'), date('y'));
        $tomorrowDate = date('m/d/y', $tomorrow);

$collection = Mage::getModel('catalog/product')
                             ->getCollection()
                             ->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left')
                             ->addMinimalPrice()->addFinalPrice()
                             ->addAttributeToSelect('*')
                             ->addAttributeToFilter('category_id', array('in' => $categoryIds))
                             ->addAttributeToFilter('special_from_date', array('date' => true, 'to' => $todayDate))

                             ->addFinalPrice()

                   ->getSelect()
                   ->where('price_index.final_price < price_index.price')


                             ->addAttributeToFilter('special_to_date', array('or'=> array(
                                0 => array('date' => true, 'from' => $tomorrowDate))
                                ), 'left')




?>

I want to display if special price less then the original price how to get that ?

2
  • you want to get special price product collection? Commented Feb 11, 2016 at 11:15
  • Yes if product have special price and less then the original price
    – Magento 2
    Commented Feb 11, 2016 at 11:17

3 Answers 3

3
    $categoryIds = array(469);//category id



    $products = Mage::getResourceModel('catalog/product_collection')

->addCategoryFilter(Mage::getModel('catalog/category')->load($categoryIds));
$_productCollection = Mage::getModel('catalog/product')->getCollection()->addFieldToFilter('entity_id', array('in' => $products->getAllIds()));;
$_productCollection->addAttributeToSelect(array(
                                   'image',
                                   'name',
                                   'short_description'
                   ))
                   ->addFieldToFilter('visibility', array(
                               Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,
                               Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG
                   )) 
                   ->addFinalPrice()

                   ->getSelect()
                   ->where('price_index.final_price < price_index.price')


                   print_r($_productCollection->getAllIds());
32
  • I tried previousley
    – Magento 2
    Commented Feb 11, 2016 at 11:23
  • Check my updated question anythin wrong
    – Magento 2
    Commented Feb 11, 2016 at 11:26
  • try update my updated and feedback me. Commented Feb 11, 2016 at 11:36
  • No it's not working after that i echo $collection->count() but it didn't display anything
    – Magento 2
    Commented Feb 11, 2016 at 11:40
  • i think it's not move after this
    – Magento 2
    Commented Feb 11, 2016 at 11:41
1

Add ->addMinimalPrice()->addFinalPrice() to collection for getting price.

1
  • Where i can add this under collection
    – Magento 2
    Commented Feb 11, 2016 at 9:02
1

A good thing to do is to use the Mage_Catalog_Model_Layer class to prepare your product collections. Increases performance (as opposed to just use a wildcard in addAttributeToSelect) and fully prepares your collection, the same way it happens in generic category product listings.

So for example, you'd do something like this:

$products = Mage::getModel('catalog/layer')
    ->setCurrentCategory(469)
    ->getProductCollection();

As far as I know (haven't tested this right now), it should automatically apply the special price and everything you'd need to create a product listing. Doing it yourself is asking for trouble.

2
  • it will display all product under this category there is no filtered
    – Magento 2
    Commented Feb 11, 2016 at 9:05
  • @Kothari you can still add your own filters to it. $products is a collection
    – Erfan
    Commented Feb 11, 2016 at 16:38

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.