Magento 1.9 – Show Product Only Within a Date Range

datemagento-1.8magento-1.9product

I want to create a product with start date and end date.
Once the end date comes the product should be hidden from the product listing.
Are there any methods to complete this task?

I tried with product new from date and product new to date, it is not giving me the desired result.

Best Answer

I suggest,

Create new two date attribute product attribute.W

  • available_from_date ->the start date from where the product will be show on product listing
  • available_to_date ->the end date from where the product wwill be stop on product listing

This two attribute manage the product wise available time .Just like h product new from date and product new to date.

Now you need to set product those two field value from admin

Then using below events filter the product collection by :

  • catalog_block_product_list_collection (used by review module, after collection is loaded its not editable anymore)

  • catalog_product_collection_load_before

  • catalog_product_collection_load_after

See catalog_product_collection_load_before and getLoadedProductCollection

Then make event area fronted thus event will fire on frontend area

Example using event:

<frontend>
        <events>
            <catalog_product_collection_load_after> <!- event -->

                <observers>
                    <pset_product_avaliable>
                        <class>YourModuleNameSpace_ModuleName_Model_Observer</class>
                        <method>collectiondatefilter</method>
                    </pset_product_avaliable>>              
                </observers>
            </catalog_product_collection_load_after>        
        </events>

On event filter the collection by below code:

         $todayStartOfDayDate  = Mage::app()->getLocale()->date()
            ->setTime('00:00:00')
            ->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);

        $todayEndOfDayDate  = Mage::app()->getLocale()->date()
            ->setTime('23:59:59')
            ->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);

  $collection->addAttributeToFilter('available_from_date', array('or'=> array(
                0 => array('date' => true, 'to' => $todayEndOfDayDate),
                1 => array('is' => new Zend_Db_Expr('null')))
            ), 'left')
            ->addAttributeToFilter('available_to_date', array('or'=> array(
                0 => array('date' => true, 'from' => $todayStartOfDayDate),
                1 => array('is' => new Zend_Db_Expr('null')))
            ), 'left')
        ->addAttributeToFilter(
                array(
                    array('attribute' => 'available_from_date', 'is'=>new Zend_Db_Expr('not null')),
                    array('attribute' => 'available_to_date, 'is'=>new Zend_Db_Expr('not null'))
                    )
              );

Observer look like:

 public function collectiondatefilter($observer)
    {
        $collection = $observer->getEvent()->getCollection();

        $todayStartOfDayDate  = Mage::app()->getLocale()->date()
            ->setTime('00:00:00')
            ->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);

        $todayEndOfDayDate  = Mage::app()->getLocale()->date()
            ->setTime('23:59:59')
            ->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);


    $collection->addAttributeToFilter('available_from_date', array('or'=> array(
                0 => array('date' => true, 'to' => $todayEndOfDayDate),
                1 => array('is' => new Zend_Db_Expr('null')))
            ), 'left')
            ->addAttributeToFilter('available_to_date', array('or'=> array(
                0 => array('date' => true, 'from' => $todayStartOfDayDate),
                1 => array('is' => new Zend_Db_Expr('null')))
            ), 'left')
        ->addAttributeToFilter(
                array(
                    array('attribute' => 'available_from_date', 'is'=>new Zend_Db_Expr('not null')),
                    array('attribute' => 'available_to_date, 'is'=>new Zend_Db_Expr('not null'))
                    )
              );

        return $this;
    }
Related Topic