How to Add New Products Automatically in Magento 1.9? [closed]
-
30-09-2020 - |
Question
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
Solution
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