Magento Forum

How to check if the product is “new”? 
 
koubas
Jr. Member
 
Total Posts:  20
Joined:  2007-10-31
Czech Republic, Hradec Kralove
 

Hi, I want to add “NEW “ labels on the new products in the catalog, but I have problems with checking of the attribute “news_to_date” (FROM too). I tried simply getting it by $_product->getData(’news_to_date’);, but it didn’t work. Then I tried something like

foreach($_product->getAttributes() as $attribute{
    
if ($attribute->getAttributeCode() == 'news_to_date'var_dump($attribute->getFrontend()->getValue($_product)));;
}

But still nothing. The attribute with code ‘news_to_date’ is in the collection, because var_dump is executed, but returns NULL (the attribute has been set properly in the backend before).

Please help, what is the right way, to check the NEW status of the product?

 
Magento Community Magento Community
Magento Community
Magento Community
 
danieln
Sr. Member
 
Avatar
Total Posts:  131
Joined:  2007-11-08
Mainz, Germany
 

I’ve done something similar and would suggest something like this:
app/code/coreMage/Cataloge/Model/Product.php

public function isNewProduct()
{
    
if ($this->getData('news_to_date') === true{
        
return true;
    
}
    
return false;        
}
You propably want to write this as an extending class in your code/local directory, but you’ll get the point.
Then you could use it later like this:
<? if ($_product->isNewProduct() === true): ?>

But do you want to maintain the new status manually? You could also use the timestamp from the database record of a product to check if it is newer than whatever, 1 week or so…

Best regards,
Daniel

 
Magento Community Magento Community
Magento Community
Magento Community
 
bcelenza
Member
 
Avatar
Total Posts:  44
Joined:  2008-08-27
 

I use this code. It’s not perfect, but it’ll do the trick.

public function isNew($date=null)
    
{
        
if($date==null{
            $date 
$this->getResource()->formatDate(time());
        
}
        
        
if(!$this->getData('news_from_date')) {
            
return false;
        
}
        
        $current_date 
= new DateTime($date); // compare date
        
$from_date = new DateTime($this->getData('news_from_date')); // begin date
        
$to_date = new DateTime($this->getData('news_to_date')); // end date
        
        
$return = ($current_date >= $from_date && $current_date <= $to_date);
        
        return 
$return;
    
}
 
Magento Community Magento Community
Magento Community
Magento Community
 
quanza
Sr. Member
 
Avatar
Total Posts:  97
Joined:  2008-12-02
Tokyo, Japan
 

bcelenza, any chance you could elaborate a bit more on where exactly you put this code?  did you have to create a new extension?  or did you add it to a core file? where? which one?

any details to help those not as familiar with the Magento system would be great.

thanks!

 
Magento Community Magento Community
Magento Community
Magento Community
 
quanza
Sr. Member
 
Avatar
Total Posts:  97
Joined:  2008-12-02
Tokyo, Japan
 

okay, I don’t know WTF is going on with magento, but creating the XML file as noted in this post did the trick:

http://www.magentocommerce.com/boards/viewthread/19215/#t75902

 
Magento Community Magento Community
Magento Community
Magento Community
 
svolchenko
Jr. Member
 
Total Posts:  1
Joined:  2013-10-20
 

How to do it from SOAP request?

I try to do next:

$attributes->attributes = array('name''description''short_description''price''special_price'
        
'special_from_date''special_to_date','url_key''url_path''news_from_date','news_to_date'
        
'additional_attributes');
    try 
{
        $list 
$GLOBALS["proxy"]->catalogProductInfo($GLOBALS["sessionId"]$skuNULL$attributes'sku');
    
catch (Exception $e{
        
return array();
    
}

But $list[news_from_date] and $list[news_to_date] always empty. :(

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top