0

I have following code

$promoCatId = (int)Mage::getStoreConfig('promoprodsec/promoprodgroup/promocategoryid');

$products = Mage::getModel('catalog/category')->setStoreId($store)->load($promoCatId)
    ->getProductCollection();
$allProductsInPromoCategory = $products->getAllIds();

It gives me all the products of $promoCatId category.

Now I want to get all the products which are not having special price or the special price date range has expired from all the products of $allProductsInPromoCategory

How can I accomplish this without using foreach loop and again loading every product collection again?

foreach ($allProductsInPromoCategory as $promoprod) {
    Mage:getModel('catalog/product')->load( $promoprod);

    //do validation for the special price criteria
}
0

Take a look at Magento - Show Only Special Priced Products In A Category

    $todayDate = strftime("%Y-%m-%d",Mage::app()->getLocale()->storeTimeStamp(Mage::app()->getStore()->getId()));
    $storeId    = Mage::app()->getStore()->getId();  
    $product    = Mage::getModel('catalog/product');  

    //To Do - load category here
    $product->addCategoryFilter($category)

    $this->_productCollection = $product->setStoreId($storeId)  
      ->getCollection()  
      ->addAttributeToSelect(array('name','status', 'price', 'special_price', 'small_image','required_options','special_from_date', 'special_to_date'), 'inner')
                ->joinField('stock_status','cataloginventory/stock_status','stock_status',
                    'product_id=entity_id', array(
                      'stock_status' => Mage_CatalogInventory_Model_Stock_Status::STATUS_IN_STOCK,
                      'website_id' => Mage::app()->getWebsite()->getWebsiteId(),
                    ))
                ->addAttributeToFilter('special_price', array('gt' => 0), 'left')
                ->addAttributeToFilter('special_from_date', array('date' => true, 'to' => $todayDate))
                ->addAttributeToFilter('special_to_date', array('or'=> array(
                    0 => array('date' => true, 'from' => $todayDate),
                    1 => array('is' => new Zend_Db_Expr('null')))
                ), 'left')
                //->setOrder('created_at', 'desc')
                ->addAttributeToSort('created_at', 'desc')
                ->addFinalPrice()
                ->addStoreFilter()
                ->setPageSize($this->getProductsLimit())
                ->setCurPage($page)
                ->addAttributeToFilter('status', 1)
                ->addUrlRewrite();

            Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($this->_productCollection);  
            Mage::getSingleton('catalog/product_visibility')->addVisibleInSearchFilterToCollection($this->_productCollection);


      $checkedProducts = new Varien_Data_Collection();
      foreach ($this->_productCollection as $k => $p) {
          $p = $p->loadParentProductIds();
          $parentIds = $p->getData('parent_product_ids');

          if (is_array($parentIds) && !empty($parentIds)) {
              if (!$checkedProducts->getItemById($parentIds[0])) {
                  $parentProduct = Mage::getModel('catalog/product')->setStoreId($storeId)->load($parentIds[0]);
                  if ($parentProduct->isVisibleInCatalog()) {
                      $checkedProducts->addItem($parentProduct);
                  }
              }
          } else {
              if (!$checkedProducts->getItemById($k)) {
                  $checkedProducts->addItem($p);
              }
          }
          if (count($checkedProducts) >= $this->getProductsLimit()) {
              break;
          }
      }

      return $this->_productCollection; 

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.