0

Trying to get products where special_price is set and special_from_date is less than todays date and special_to_date is more than todays date, so:

$currentTime = Mage::getSingleton('core/date')->gmtDate();
$_products = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToSelect('*')
    ->addAttributeToFilter(
        array(
            array(
                'attribute' => 'promotionactive','promotionactive' => true,
                'and' => array(
                        'attribute' => 'special_from_date','date' => true,'lt' => $currentTime,
                    'and' => array(
                            'attribute' => 'special_to_date','date' => true,'gt' => $currentTime,
                        'and' => array( 
                                'special_price' => true)
                        ),
                    )
                )
            )
        )
    ;

Today I simplified it a bit but it still won't get products properly. The filter is set to select only if promotionactive is true but in results I can see all the products regardless if is true or false.

Finally the code is doing it's job:

$currentTime = Mage::getSingleton('core/date')->gmtDate();
$_products = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('promotionactive' , true);
$id = $_products->getFirstItem()->getId();
$_product = Mage::getModel('catalog/product')->load($id);
if(($_product['promotionactive'] == True) && (($_product['special_price'])==True) && (strtotime($_product->getSpecialFromDate()) < strtotime($currentTime)) && (strtotime($currentTime) < strtotime($_product->getSpecialToDate()))){
    $milliEnd = strtotime($_product->getSpecialToDate()) * 1000;
    $productUrl = $_product->getUrlInStore();
}else{
    //some code
}

Problem solved, thanks everyone.

If there is any way to make it better please post below ;)

1

Use below code :

$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addMinimalPrice()
            ->addFinalPrice()
            ->addTaxPercents()
            ->addUrlRewrite(); 
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);

$collection->getSelect()->where("`price_index`.price !=price_index.min_price");
$now = Mage::getSingleton('core/date')->gmtDate();
$collection->addAttributeToFilter('special_from_date', array('or'=> array(
    0 => array('date' => true, 'to' => $now),
    1 => array('is' => new Zend_Db_Expr('null')))
), 'left');
  • Your example is giving me a huge chunk of data in which actually I can't find anything interesting like products with adequate parameters, I changed a bit my script and now it gives me only a product with selected attributes. – Rob D. A. Feb 22 '17 at 11:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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