0

how can I have Magento automatically set

Set Product as New to Date

for 7 days whenever I create a new product, that way I don't have to be manually selecting the dates whenever I create a new product.

1

You can use event

catalog_product_save_before

to add value in new from today's date using date function and in new to date after seven days using date function with +7 days.

This is the simple way to achieve this. You can also oveeride controller but better is use event observer. Please follow below step to make module for save date automatically.

Create xml file to register module

app/etc/modules/Company_Date.xml

add below code to this Company_Date.xml

<?xml version="1.0"?>
<config>
  <modules>
    <Company_Date>
      <active>true</active>
      <codePool>local</codePool>
      <version>0.1.0</version>
    </Company_Date>
  </modules>
</config>

Now create config.xml file in app/code/local/Company/Date/etc/

Add below code to config.xml

<?xml version="1.0"?>
<config>
  <modules>
    <Company_Date>
      <version>0.1.0</version>
    </Company_Date>
  </modules>
  <global>
    <helpers>
      <date>
        <class>Company_Date_Helper</class>
      </date>
    </helpers>
    <models>
      <date>
        <class>Company_Date_Model</class>
        <resourceModel>date_mysql4</resourceModel>
      </date>
    </models>
    <events>
      <catalog_product_save_before> <!-- identifier of the event we want to catch -->
        <observers>
          <catalog_product_save_before_handler> <!-- identifier of the event handler -->
            <type>singleton</type> <!-- class method call type; valid are model, object and singleton -->
            <class>date/observer</class> <!-- observers class alias -->
            <method>SaveDate</method>  <!-- observer's method to be called -->
          </catalog_product_save_before_handler>
        </observers>
      </catalog_product_save_before>
    </events>
  </global>
</config> 

Now create Observer file app/code/local/Company/Date/Model/Observer.php

class Company_Date_Model_Observer
{

            public function SaveDate(Varien_Event_Observer $observer)
            {
                       $product = $observer->getEvent()->getProduct();
                       Mage::log($product->getId(), null, 'mylogfile.log');
                       $product->setNewsFromDate('10/26/2016') //product set as new from 
                       ->setNewsToDate('10/30/2016');  // make date dynamic
            }

}
12
  • Can you walk me through on how to use that?
    – cubaton3
    Oct 22 '16 at 23:25
  • Do you have magento 1 or magebto 2? I can see your comment you don't have directory after app/code. It should be M2. Please confirm.
    – Kul
    Oct 23 '16 at 10:03
  • I have Magento 1.9.2.4
    – cubaton3
    Oct 23 '16 at 18:48
  • sorry there is some mis-understanding. you need to create directory in app/code/local/PACKAGE/YOUR MODULE. CODEPOOL means local/community
    – Kul
    Oct 23 '16 at 18:50
  • Wow, I can't find a way to do this, I'm really confused.
    – cubaton3
    Oct 23 '16 at 22:11
0

First you need to declare observer in your config.xml

app/code/[CODEPOOL]/Company/Extension/etc/config.xml

<global>
     <events>
        <catalog_product_save_after>
            <observers>
                <your_module_save_product_changes>
                    <type>singleton</type>
                    <class>your_module/observer</class>
                    <method>setProductdateobserver</method>
                </your_module_save_product_changes>
            </observers>
        </catalog_product_save_after>
    </events>
</global>

Now create observer.php file in this location

app/code/[CODEPOOL]/Company/Extension/Model/Observer.php

class Company_Extension_Model_Observer
{
    public function setProductdateobserver($observer)
    {
       $product = $observer->getEvent()->getProduct();
       // Now right your logic and set new product date
    }
}
5
  • I don't have those directories after /app/code/
    – cubaton3
    Oct 23 '16 at 7:33
  • You can used any already avilable extension Or create new extension for this. Oct 24 '16 at 2:52
  • @cubaton3 Have you check above things? Oct 25 '16 at 3:44
  • I'm not going to pay for an extension, and I don't know how to execute this myself.
    – cubaton3
    Oct 25 '16 at 5:24
  • if you get your solution then mark as answer, so other can also get help :) Oct 25 '16 at 5:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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