Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to load products between two dates ?

    • 0
    • 0
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 497
    Comment on it

    If we are required to load products between two dates lest between previous month to current date.

    lets see how we can load them in the function loadproduct()   

     public function loadproduct() {
       
        $first = date('Y-m-01', strtotime('-1 months'));
        $first = $first.'00:00:00';
        $last = date('Y-m-d');
        $last = $last.'00:00:00';
    
        $collection = Mage::getModel('catalog/product');
        $products = $collection->getCollection()
            ->addAttributeToSelect('*')
            ->addAttributeToFilter('updated_at', array('gteq' =>$first))
            ->addAttributeToFilter('updated_at', array('lteq' => $last))
            ->addAttributeToFilter('status', 1)
            ->load();
    
        return $products;
      }
    
    }

    In the above code we are loading the products between two dates lest between previous month to current date. For that purpose we first get the dates of the previous month date from the current month date using core php functions strtotime  and date. 
    Afterwards we loaded the collection of products from the model 'catalog/product'. Then we extracted those products from the array which are updated between those dates for that we write: 

     

    addAttributeToFilter('updated_at', array('gteq' =>$first))
    addAttributeToFilter('updated_at', array('lteq' => $last))

     

    We cheched the updated_at field value of products from data base and status if active or not. The loaded arry is stored in $products array which we returned.


    In this way we can change the date parameter values to load the desired products with that condition.

 1 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: