YorkieMagento YorkieMagento - 2 months ago 10
PHP Question

Add additional labels (e.g. new, in stock, sale) in Magento theme

I have a Magento store, using a template I purchased, I'd like to add additional product label prompts. For example, at the moment we have 'Sale' and 'New' as standard. But I'd like to add 'in stock' or anything else that might provide a call to action.

Below is the 'labels.php' file which controls the labels (as far as I can tell). I wondered if I can add more lables to this perhaps after setting up a product attribute (e.g. in_stock YES/NO) to gain the additional labels.

<?php

class Infortis_Ultimo_Helper_Labels extends Mage_Core_Helper_Abstract
{
/**
* Get product labels (HTML)
*
* @return string
*/
public function getLabels($product)
{
$html = '';

$isNew = false;
if (Mage::getStoreConfig('ultimo/product_labels/new'))
{
$isNew = $this->isNew($product);
}

$isSale = false;
if (Mage::getStoreConfig('ultimo/product_labels/sale'))
{
$isSale = $this->isOnSale($product);
}

if ($isNew == true)
{
$html .= '<span class="sticker-wrapper top-left"><span class="sticker new">' . $this->__('New') . '</span></span>';
}

if ($isSale == true)
{
$html .= '<span class="sticker-wrapper top-right"><span class="sticker sale">' . $this->__('Sale') . '</span></span>';
}

return $html;
}

/**
* Check if "new" label is enabled and if product is marked as "new"
*
* @return bool
*/
public function isNew($product)
{
return $this->_nowIsBetween($product->getData('news_from_date'), $product->getData('news_to_date'));
}

/**
* Check if "sale" label is enabled and if product has special price
*
* @return bool
*/
public function isOnSale($product)
{
$specialPrice = number_format($product->getFinalPrice(), 2);
$regularPrice = number_format($product->getPrice(), 2);

if ($specialPrice != $regularPrice)
return $this->_nowIsBetween($product->getData('special_from_date'), $product->getData('special_to_date'));
else
return false;
}

protected function _nowIsBetween($fromDate, $toDate)
{
if ($fromDate)
{
$fromDate = strtotime($fromDate);
$toDate = strtotime($toDate);
$now = strtotime(Mage::app()->getLocale()->date()->setTime('00:00:00')->toString(Varien_Date::DATETIME_INTERNAL_FORMAT));

if ($toDate)
{
if ($fromDate <= $now && $now <= $toDate)
return true;
}
else
{
if ($fromDate <= $now)
return true;
}
}

return false;
}
}


Any help would be much appreciated.

Thanks very much

Answer

I managed to do it crudely, by adding a new product date attribute to Magento 'in stock' and copying the code for the new, editing for the change in name. It would be good to know if I can do this as a straight yes/no as well, I presume it's relatively simple to change the query from a date to a straight yes/no however I don't know the syntax.

     <?php



     class Infortis_Ultimo_Helper_Labels extends Mage_Core_Helper_Abstract

     {

    /**

 * Get product labels (HTML)

 *

 * @return string

 */

public function getLabels($product)

{

    $html = '';



    $isNew = false;

    if (Mage::getStoreConfig('ultimo/product_labels/new'))

    {   

        $isNew = $this->isNew($product);

    }



    $isSale = false;

    if (Mage::getStoreConfig('ultimo/product_labels/sale'))

    {

        $isSale = $this->isOnSale($product);

    }

    $isInstock = false;

    if (Mage::getStoreConfig('ultimo/product_labels/new'))

    {

        $isInstock = $this->isInstock($product);

    }


    if ($isNew == true)

    {

        $html .= '<span class="sticker-wrapper top-left"><span class="sticker new">' . $this->__('New') . '</span></span>';

    }



    if ($isSale == true)

    {

        $html .= '<span class="sticker-wrapper top-right"><span class="sticker sale">' . $this->__('Sale') . '</span></span>';

    }

    if ($isInstock == true)

    {

        $html .= '<span class="sticker-wrapper top-right"><span class="sticker sale">' . $this->__('In Stock') . '</span></span>';

    }

    return $html;

}



/**

 * Check if "new" label is enabled and if product is marked as "new"

 *

 * @return  bool

 */

public function isNew($product)

{

    return $this->_nowIsBetween($product->getData('news_from_date'), $product->getData('news_to_date'));

}

public function isInstock($product)

{

    return $this->_nowIsBetween($product->getData('in_stock_from'), $product->getData('in_stock_to'));

}


/**

 * Check if "sale" label is enabled and if product has special price

 *

 * @return  bool

 */

public function isOnSale($product)

{

    $specialPrice = number_format($product->getFinalPrice(), 2);

    $regularPrice = number_format($product->getPrice(), 2);



    if ($specialPrice != $regularPrice)

        return $this->_nowIsBetween($product->getData('special_from_date'), $product->getData('special_to_date'));

    else

        return false;

}



protected function _nowIsBetween($fromDate, $toDate)

{

    if ($fromDate)

    {

        $fromDate = strtotime($fromDate);

        $toDate = strtotime($toDate);

        $now = strtotime(Mage::app()->getLocale()->date()->setTime('00:00:00')->toString(Varien_Date::DATETIME_INTERNAL_FORMAT));



        if ($toDate)

        {

            if ($fromDate <= $now && $now <= $toDate)

                return true;

        }

        else

        {

            if ($fromDate <= $now)

                return true;

        }

    }



    return false;

}

}