3

I need to have a label "new" on products which were added recently. I have this done on extension/module/latest.tpl, but I'd like also to have this label working on another pages too, like product/product.tpl, product/category and so on...

How can I do this without buying any extensions?

  • OpenCart 2.3
  • Template: default

UPDATE

/controller/product/category.php

    /*start added part*/

                if(strtotime($result['date_added']) > (time() - (60*60*24*10) )){      
                    //(note that 10 means ten days, to consider a product as a new product, you can change it based on your need.)
                    $is_new = true;
                    } else {
                    $is_new = false;
                }
            /*end added part*/


            $data['products'][] = array(
                'is_new'      => $is_new,               //added code
                'product_id'  => $result['product_id'],
                'thumb'       => $image,
                'name'        => $result['name'],
                'description' => utf8_substr(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8')), 0, $this->config->get($this->config->get('config_theme') . '_product_description_length')) . '..',
                'price'       => $price,
                'special'     => $special,
                'tax'         => $tax,
                'minimum'     => $result['minimum'] > 0 ? $result['minimum'] : 1,
                'rating'      => $result['rating'],
                'href'        => $this->url->link('product/product', 'path=' . $this->request->get['path'] . '&product_id=' . $result['product_id'] . $url)
            );
        }

And this is what i did in product/category.tpl

 <?php foreach ($products as $product) { ?>
    <div class="product-layout product-list col-xs-12">
      <div class="product-thumb">
        <div class="image"><a href="<?php echo $product['href']; ?>"><img src="<?php echo $product['thumb']; ?>" alt="<?php echo $product['name']; ?>" title="<?php echo $product['name']; ?>" class="img-responsive" /></a></div>
        <div>
          <div class="caption">
            <h4><a href="<?php echo $product['href']; ?>"><?php echo $product['name']; ?></a></h4>

            <?php if($product['is_new']){ ?><span>NEW</span><?php } ?>

            <p><?php echo $product['description']; ?></p>
            <?php if ($product['price']) { ?>
Jasmine J
  • 43
  • 7

1 Answers1

4

You can compare product's date_added with current time, you need to modify two file for category : Fisrt file: catalog/controller/product/category.php

find:

$data['products'][] = array(

add before it:

if(strtotime($result['date_added']) > (time() - (60*60*24*10) )){
    $is_new = true;
} else {
    $is_new = false;
}

(note that 10 means ten days, to consider a product as a new product, you can change it based on your need.)

add after it:

'is_new'      => $is_new,

Now this section must be like this:

if(strtotime($result['date_added']) > (time() - (60*60*24*10) )){
    $is_new = true;
} else {
    $is_new = false;
}

$data['products'][] = array(
    'is_new'      => $is_new,
    'product_id'  => $result['product_id'],
    'thumb'       => $image,
    'name'        => $result['name'],
    'description' => utf8_substr(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8')), 0, $this->config->get($this->config->get('config_theme') . '_product_description_length')) . '..',
    'price'       => $price,
    'special'     => $special,
    'tax'         => $tax,
    'minimum'     => $result['minimum'] > 0 ? $result['minimum'] : 1,
    'rating'      => $result['rating'],
    'href'        => $this->url->link('product/product', 'path=' . $this->request->get['path'] . '&product_id=' . $result['product_id'] . $url),
);

Second file: catalog/view/theme/default/template/product/category.tpl

add this code inside foreach loop of products, for example before: <p><?php echo $product['description']; ?></p> :

<?php if($product['is_new']){ ?><span class="new-product">NEW</span><?php } ?>

I created a xml script for vqmod:

<?xml version="1.0" encoding="UTF-8"?>
<modification>
    <id>Products New Label</id>
    <version>2.x</version>
    <vqmver required="true">2.6.0</vqmver>
    <author>sabeti05@gmail.com</author>

    <file name="catalog/controller/product/category.php">
        <operation error="skip">
            <search position="replace"><![CDATA[$data['products'][] = array(]]></search>
            <add><![CDATA[
                if(strtotime($result['date_added']) > (time() - (60*60*24*10) )){
                    $is_new = true;
                } else {
                    $is_new = false;
                }
                $data['products'][] = array(
                    'is_new'      => $is_new,
            ]]></add>
        </operation>
    </file>

    <file name="catalog/view/theme/*/template/product/category.tpl">
        <operation error="skip">
            <search position="before"><![CDATA[<p><?php echo $product['description']; ?></p>]]></search>
            <add><![CDATA[
                <?php if($product['is_new']){ ?><span class="new-product">NEW</span><?php } ?>
            ]]></add>
        </operation>
    </file>

</modification>
DigitCart
  • 2,980
  • 2
  • 18
  • 28
  • hello @Mojtaba Sabeti ,thank you for your response i tried first solution(without using vqmod) ,but didn't get any result, it just doesn't show anything(i use 'clear' opencart2.3 without any another extensions installed) ...i put the code in my question update(sorry if i did it wrong ,i am new to stackoverflow:-)),maybe i did something wrong ?? please help.... – Jasmine J Dec 23 '16 at 15:36
  • i did also try vqmod ,but still no result ...i also tried to use latest module and it shows latly added products ... – Jasmine J Dec 26 '16 at 18:51
  • Hello, The day I wrote above post, I tested my work, it worked correctly, please be patience, I'll re-check it as soon as possible. – DigitCart Dec 27 '16 at 10:51
  • i'd like also ask how can i display new label on those products that are last in product list (i mean in admin panel in catalog/product) ,for instance, display new label on latly added 4 products(last 4 products in product list) (not according date)??? – Jasmine J Dec 30 '16 at 14:22
  • Ooops...i've just tried again your code and now everything works PERFECT... really,i'm deeply sorry for being so unattentive and making this all confusion....Thank you so much for your great solution ,for your help and quick response ,i deeply appreciate it... but what i really need to get done is a little bit different :-) as i described in the previous comment ,i'll be really very glad ,if you help me to solve this problem – Jasmine J Dec 30 '16 at 14:49
  • hi @JasmineJ, you are welcome, if my answer solved your mentioned problem, please accept it as solved and for your new request for admin panel, open a new question. Thank you – DigitCart Dec 30 '16 at 15:22