3

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.

2
  • you just have to set the CRON on server when it would reach the end date of the products.. It would make it disabled Jun 25, 2015 at 7:17
  • @Manashvibirla how to add an end date to a product while adding a product?
    – Mujahidh
    Jun 25, 2015 at 7:27

4 Answers 4

2

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;
    }
1
  • I am following this solution but products are not being filtered
    – Butterfly
    Mar 22, 2016 at 9:52
2

You would need to develop this yourself, or maybe take a look to this extension: http://shop.ecomdev.org/scheduled-products.html

If you would like to give it a try you could create a cronjob that checks the end date of the products and disable them.

In your module config.xml:

<crontab>
    <jobs>
        <mymodule_disable>
            <schedule>
                <!-- every 10 min -->
                <cron_expr>*/10 * * * *</cron_expr>
            </schedule>
            <run>
                <model>mymodule/Scheduler::disable</model>
            </run>
        </mymodule_disable>
    </jobs>
</crontab>

And the method to disable the products:

public static function disable()
{
     $date = Mage::getModel('core/date')->gmtDate();
     $collection = Mage::getModel('catalog/product')->getCollection();
     $collection->addfieldtofilter('end_data', array(
         array('gteq' => $date)
     ));
     foreach($collection as $product) {
         $product->setStatus(Mage_Catalog_Model_Product_Status::STATUS_DISABLED);
         $product->save();
     }
}

You would need to adjust this code to your specific needs, but this should do the trick. Credit goes to: https://stackoverflow.com/a/15295686/1016425

If you need to add an attribute to your products, then add a new attribute called end_date and assign the attribute to the attribute set you are using for your products. Check here: https://www.youtube.com/watch?v=MqhCcCpR4DI

Another possibility would be to rewrite the block for category details and product details, and then when you get the product collection filter by end_date there. In this case you don't need a cron.

5
  • how to set the end date in admin panel while adding the product?
    – Mujahidh
    Jun 25, 2015 at 7:25
  • I updated my answer, check how to add the attribute. Then the new attribute will be available at the product edit page in the backend.
    – lloiacono
    Jun 25, 2015 at 7:41
  • yes that's fine, i guess is there any thing already exist in the magento rather than creating an attribute.Thank you very much.
    – Mujahidh
    Jun 25, 2015 at 7:43
  • Then go for one of the existing modules, check the first link in my answer. Also if this helped you consider accepting the answer. Thanks and let me know if you need more details
    – lloiacono
    Jun 25, 2015 at 7:45
  • The ecomdev link doesn't appear to go to an active extension on magento connect, but there is also a detailed description here: stackoverflow.com/a/26365562/3779933
    – MW Millar
    Jun 25, 2015 at 9:24
2

Try This Code:-

<?php
    ini_set('max_execution_time', 36000);
    ini_set('post_max_size', '5000M');
    ini_set("memory_limit","4046M");
    ini_set('upload_max_filesize', '5000M');
    require_once('app/Mage.php'); //Path to Magento
    umask(0);
    Mage::app('default');
    Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
    $todayDate  = Mage::app()->getLocale()->date()->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);


    $_productCollection = Mage::getModel('catalog/product')
                           ->getCollection()                        
                           ->addAttributeToSelect('*')
                           ->addAttributeToFilter('status', array('eq' => 1))
                           ->addAttributeToFilter('news_from_date', array('date' => true, 'to' => $todayDate))
                        ->addAttributeToFilter('news_to_date', array('or'=> array(
                           0 => array('date' => true, 'from' => $todayDate),
                           1 => array('is' => new Zend_Db_Expr('null')))
                       ), 'left')
                        ->addAttributeToSort('news_from_date', 'desc')
                        ->addAttributeToSort('created_at', 'desc')
                           ->load();
    foreach ($_productCollection as $_product)
    {
      echo $_product->getStatus().'</br>';

      echo $_product->getId().'</br>';
      echo $_product->getName().'</br>';
      echo $_product->getProductUrl().'</br>';
      echo $_product->getimage().'</br>';
      echo $_product->getPrice().'</br>';

    }


    ?>
0

You could create a product attribute "Visible to Date" for this.

And than just write a CRON job that checks all the active products for "Visible to Date" values. If the value is before the current date, disable the product. And so the product won't be visible in Frontend.

3
  • This is not a good idea imho, since you could have other modules that use this attribute, or he could alredy be using this himself. In any case this could give you some headaches in the future. It's better to create the attribute you need.
    – lloiacono
    Jun 25, 2015 at 7:48
  • True. In any case, the CRON job will do the work for you.
    – knospe
    Jun 25, 2015 at 7:49
  • Yes, the cron is needed in both cases.
    – lloiacono
    Jun 25, 2015 at 7:51

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