0

I am trying to achieve something like this:

When adding new product, The field for "Set Product as New from Date" will be auto filled with Same day date (Current date) and "Set Product as New to Date"= 1/2/3 months from that date.

I found similar post which advise to create observer event Link(automatic add the product's Set Product as New from Date) . But its not very in detail. Can please someone help me step by step on how to do this?

I want to know how to do that - From start to End - Step by Step.

Thanks

1
  • Welcome to MageOverflow! What exactly is your problem? Implementing an observer? Finding the right event? Something else? Please EDIT YOUR QUESTION to add your real problem. (and linking the question you are talking about might be a good idea too) Jul 6, 2016 at 15:08

1 Answer 1

1

In order to do what you want you'd have to create a simple module that would hook into event called catalog_product_save_before.

In that observer you'd have to check if product's new by looking at protected property _origData. If that property would be NULL then set the values for product's properties newsFromDate and newsToDate. Values of those dates would be best to initialize in your class _construct() method.

I'll assume that namespace of this module is my nickname, and the module is called NewProductAuto.

Directories:

  • app/code/local/Versedi/NewProductAuto/Model
  • app/code/local/Versedi/NewProductAuto/etc/
  • app/code/local/Versedi/NewProductAuto/Helper/

Files:

  • app/etc/modules/Versedi_NewProductAuto.xml
  • app/code/local/Versedi/NewProductAuto/Model/Observer.php
  • app/code/local/Versedi/NewProductAuto/etc/config.xml
  • app/code/local/Versedi/NewProductAuto/Helper/Data.php

Two most important files are Observer.php and module's configuration - config.xml, I'll ommit the rest hoping that it's self explanatory or that you can find information about that in a number of places over the internet.

app/code/local/Versedi/NewProductAuto/Model/Observer.php First we have to declare our class which will be named: Versedi_NewProductAuto_Model_Observer and will extend Mage_Core_Model_Abstract.

In that class declare two protected properties - $productNewFromDate and $productNewToDate and two methods - _construct() and setProductNewFromDateToday().

We can set variables with current date when the class is initialized or simply when the product is actually new, I prefer to do that in _construct() method but it's not necessary.

public function _construct() { $now = date('d-m-Y', time()); $this->productNewFromDate = $now; $this->productNewToDate = new \DateTime($now); $this->productNewToDate = $this->productNewToDate->add(new DateInterval('P3M'))->format('d-m-Y'); }

What we do in above code block? We're setting new variable $now with time in number format converted to date using php internal function date(). We're assigning that date to our protected property productNewFromDate. Next thing is initializing a DateTime object to which we then add new DateInterval with value of 3 months (one day - P1D, one month - P1M). This value though still needs converting to our target format - d-m-Y.

 
    /**
     * @param Varien_Event_Observer $observer
     *
     */ 
    public function setProductNewFromDateToday(Varien_Event_Observer $observer)
    {
        $product = $observer->getEvent()->getProduct();
        if ($product->getOrigData() === NULL) { // Only if product is new - no original data exists
            $product->setNewsFromDate($this->productNewFromDate);
            $product->setNewsToDate($this->productNewToDate);
        }
    }

What we do in above code block? When the event is fired our function is called and an observer is passed to it. The observer can retrieve object/class/entity/data from the event that takes place. Our name of the function must be exactly same (lettercase sensitive) as stated inside config.xml node <method>setProductNewFromDateToday</method> which I'll describe few lines below. Next thing is checking if the product is actually new or if it's being edited, we do that with this:

if ($product->getOrigData() === NULL) {

The rest is self explanatory.

config.xml Most important thing in the config.xml is declaring that we want to hook into event. We do that creating an <events></events> node, in which as you can suspect we're typing the name of event that we want to catch on. I've described each node in codeblock below:

<events> <catalog_product_save_before><!-- name of the event that we're catching/hooking --> <observers> <versedi_newproductauto_handler> <!-- name of the observer --> <type>model</type> <!-- class method call type; valid are model, object and singleton --> <class>Versedi_NewProductAuto_Model_Observer</class><!-- name of observer's class --> <method>setProductNewFromDateToday</method> <!-- name of the function in observer that we're calling when the event is fired --> <args /> </versedi_newproductauto_handler> </observers> </catalog_product_save_before> </events> </global>

Since we're using an Observer model we need to declare the model inside config.xml too:

<models> <versedi_newproductauto> <class>Versedi_NewProductAuto_Model</class> <resourceModel>versedi_newproductauto_resource</resourceModel> </versedi_newproductauto>

That's basically it.

In case you still got problems with writing one yourself, You can check the source code here: https://github.com/versedi/Versedi_NewProductAuto

6
  • Hello, I have downloaded your code and saved them in directories as below app/code/local/Versedi/NewProductAuto/Model app/code/local/Versedi/NewProductAuto/etc/ app/code/local/Versedi/NewProductAuto/Helper/ Also, I uploaded file in module ( app/etc/modules/Versedi_NewProductAuto.xml) Cleared the cached. I tested by adding New Product, It did work. Thank You, But it did not work when I updated a already created product. Did I miss anything?
    – Shyam
    Jul 7, 2016 at 16:15
  • I suppose you've skipped the reading part and hit to download it. Second paragraph of my answer. This was something that you've stated yourself - set this attribute on product creation not updating. Comment out if statement that checks the property _origData. If you're satisfied with the answer don't forget to accept it.
    – versedi
    Jul 8, 2016 at 8:51
  • Hello Versedi Thanks for your reply. I have uploaded your files in directory. Do I need to edit them further to make it work for already created products? I see your code is already present. $product = $observer->getEvent()->getProduct(); if ($product->getOrigData() === NULL) { // Only if product is new - no original data exists $product->setNewsFromDate($this->productNewFromDate); $product->setNewsToDate($this->productNewToDate); } } Above code is already there. What did I miss?
    – Shyam
    Jul 8, 2016 at 11:34
  • Comment out if statement that checks the property _origData.
    – versedi
    Jul 8, 2016 at 13:46
  • Hello, which part you want me to comment out in oberver.php ? I tried to put // in front of // if ($product->getOrigData() === NULL) I am getting syntax error. Sorry, I am doing this first time. Little more help be appreciated.
    – Shyam
    Jul 8, 2016 at 19:02

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