Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

New Icon next to new products added within 30 days


Guest

Recommended Posts

Hello, Firstly I must say THANK YOU! :thumbsup: to all "tutors" in this forum!

 

I am quite new to PHP and I just learnt them from books and from this forum.

 

Want:

 

I want to added a "new" icon next to the product's name if the products is added in the past 30 days.

 

What I've done:

 

As I don't know how to find the date of 30 days before the current day. I just try to select those items added month = current month. This is what I've tried before in products_new.php, the new part I comment by //new products icon

 

 

 

<?php

  if ($products_new_split->number_of_rows > 0) {

    $products_new_query = tep_db_query($products_new_split->sql_query);

    while ($products_new = tep_db_fetch_array($products_new_query)) {

      if ($new_price = tep_get_products_special_price($products_new['products_id'])) {

        $products_price = '<s>' . $currencies->display_price($products_new['products_price'], tep_get_tax_rate($products_new['products_tax_class_id'])) . '</s> <span class="productSpecialPrice">' . $currencies->display_price($new_price, tep_get_tax_rate($products_new['products_tax_class_id'])) . '</span>';

      } else {

        $products_price = $currencies->display_price($products_new['products_price'], tep_get_tax_rate($products_new['products_tax_class_id']));

      }

 

//New Products Icon

$date_added = getdate($products_new['products_date_added']);

        $today = getdate();

        if ($date_added[mon] == $today[mon]) {

$products_name = '<span><b><u>' . $products_new['products_name'] . '</u></b></span></a> <img src="images/icon_newarrival.gif" alt="new">';

  } else {

$products_name = '<span><b><u>' . $products_new['products_name'] . '</u></b></span></a>';

    }

 

//New Products Icon END

?>

        <tr>

    <td width="<?php echo SMALL_IMAGE_WIDTH + 10; ?>" valign="top" class="SMALLSIZE">

    <?php echo '<a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $products_new['products_id']) . '">' . tep_image(DIR_WS_IMAGES . $products_new['products_image'], $products_new['products_name'], SMALL_IMAGE_WIDTH, SMALL_IMAGE_HEIGHT) . '</a>'; ?>

    </td>

        <td valign="top" class="SMALLSIZE"> ......

 

The icons can be shown but however it shows up in all products in my database! I think the correct mon cannot be found by this statement $date_added[mon] but I dont have any idea to solve this.

 

Hope anyone can give me some help! This is my URL for your reference.

 

www.MonchhichiWorld.com

 

thank You so much!

Link to comment
Share on other sites

Got it to work. Here's my code:

 

//New Products Icon
 // convert times to unix timestamp for comparison
   $time_since_product_added = ($today_time - strtotime($products_new['products_date_added']) );

 //  2592000 = 30 days in the unix timestamp format
    if ($time_since_product_added < 2592000)      {
       $newprod_products_name = $products_new['products_name'] . '</a> <img src="images/icon_newarrival.gif" alt="new">';
     } else       {
       $newprod_products_name = $products_new['products_name'] . '</a>';
     }
//New Products Icon END

 

Also, I put the following at the top of the file, just before the call to application_top.php:

  $today_time = time();

 

and then you have to change this:

            <td valign="top" class="main"><?php echo '<a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $products_new['products_id']) . '"><b><u>' . $products_name . '</u></b> <!-- </a> --> <br>' . TEXT_DATE_ADDED . ' ' . tep_date_long($products_new['products_date_added']) . '<br>' . TEXT_MANUFACTURER . ' ' . $products_new['manufacturers_name'] . '<br><br>' . TEXT_PRICE . ' ' . $products_price; ?></td>

 

 

to this:

            <td valign="top" class="main"><?php echo '<a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $products_new['products_id']) . '"><b><u>' . $newprod_products_name . '</u></b> <!-- </a> --> <br>' . TEXT_DATE_ADDED . ' ' . tep_date_long($products_new['products_date_added']) . '<br>' . TEXT_MANUFACTURER . ' ' . $products_new['manufacturers_name'] . '<br><br>' . TEXT_PRICE . ' ' . $products_price; ?></td>

 

Now let's figure out where in index.php we can put this, so that it shows up on the regular product listings as well.

 

-jared

Link to comment
Share on other sites

Sheesh. How many times do I have to remind people that I'm not a programmer! I'm just a hack, for crying out loud! I aspire to have coding skills someday when I grow up. :)

 

I just (can't believe how long it took me) figured out how to do the same thing with the regular product listings too.

 

We need to make changes to 2 files:

  • catalog/index.php
  • catalog/includes/modules/product_listing.php

First, find the following in index.php:

// We are asked to show only a specific category
       $listing_sql = "select " . $select_column_list . " p.products_id, p.manufacturers_id, . . .

 

Note that there are 4 possible $listing_sql queries here, depending on what circumstances lead us to this code area.

 

In each of these queries, add "p.products_date_added, " to the beginning so that it loks like this:

$listing_sql = "select " . $select_column_list . " p.products_date_added, p.products_id, p.manufacturers_id, . . .

 

 

We've just added to the query the date that the product was added to the catalog. Now let's use it.

 

In product_listing.php, find the following:

        switch ($column_list[$col]) {
         case 'PRODUCT_LIST_MODEL':
           $lc_align = '';
           $lc_text = ' ' . $listing['products_model'] . ' ';
           break;
         case 'PRODUCT_LIST_NAME':

 

After it, add the following:

          //New Products Icon
            // convert times to unix timestamp for comparison
    	 $time_since_product_added = ($today_time - strtotime($listing['products_date_added']) );

     //  2592000 = 30 days in the unix timestamp format
       if ($time_since_product_added < 2592000)      {
        	 $newprod_products_name = $listing['products_name'] . '</a> <img src="images/icon_newarrival.gif" alt="new">';
      	 } else       {
        	 $newprod_products_name = $listing['products_name'];
      	 }
   //New Products Icon END

 

and change the next couple of lines from:

             if (isset($HTTP_GET_VARS['manufacturers_id'])) {
             $lc_text = '<a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'manufacturers_id=' . $HTTP_GET_VARS['manufacturers_id'] . '&products_id=' . $listing['products_id']) . '">' . $listing['products_name'] . '</a>';
           } else {
             $lc_text = ' <a href="' . tep_href_link(FILENAME_PRODUCT_INFO, ($cPath ? 'cPath=' . $cPath . '&' : '') . 'products_id=' . $listing['products_id']) . '">' . $listing['products_name'] . '</a> ';
           }

 

to:

            if (isset($HTTP_GET_VARS['manufacturers_id'])) {
             $lc_text = '<a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'manufacturers_id=' . $HTTP_GET_VARS['manufacturers_id'] . '&products_id=' . $listing['products_id']) . '">' . $newprod_products_name;
           } else {
             $lc_text = ' <a href="' . tep_href_link(FILENAME_PRODUCT_INFO, ($cPath ? 'cPath=' . $cPath . '&' : '') . 'products_id=' . $listing['products_id']) . '">' . $newprod_products_name . ' ';
           }

 

Don't forget to add the time function to the top of the file.

    $today_time = time();

 

Really, you can do that at any time, but there isn't any point in figuring out what the time of day is on every loop iteration.

 

Bobby, what affect do you think that this will have on page rendering times?

Link to comment
Share on other sites

I ask about the affect to page rendering because we're doing an additional calculation every loop iteration. It's not a very complicated calculation, I know, but it adds time to the loop nonetheless.

 

FWIW, I just hacked this code from a piece that I already done for something else. I don't even remember why I knew about the unix time thing.

 

I need to do the same type of time calculation in the orders_tracking code, too. It'll simplify the code quite a bit where I try to figure out which day yesterday was. Ok, ok. I digress. I'm sleepy and rambling.

 

Enjoy!

 

-jared

 

BTW, think we should put this in Tips 'n Tricks or release it as a contrib?

Link to comment
Share on other sites

Waitaminute. Isn't the whole point of products_new.php to display only the new products? Why should we have to mark the new products on that page? Shouldn't the page only display products that are new anyway?

 

Let's fix that page, so that we don't need this code there.

 

-jared

Link to comment
Share on other sites

Ok, so I got it to only display the items that are new w/in the last 30 days, but it's just a display thing. The results query still returns all of the products. This means that it'll still say "Displaying 1-20 (of 5000 new products)" if you have 5000 products in your store.

 

We need to fix it before displaying, not while displaying. We can't really change the query, since we don't know the value of products_date_added before the query happens.

 

Someone else want to lend a hand here? Probably best to start a new thread. I'll do that.

 

-jared

Link to comment
Share on other sites

I also added the new icon in the index page. Just need to added the code

 

$today_time = time();

 

in the index page

 

while the other code in includes/module/products_listing.php

Link to comment
Share on other sites

Sheesh.  How many times do I have to remind people that I'm not a programmer!  I'm just a hack, for crying out loud!  I aspire to have coding skills someday when I grow up.  :)

...

Bobby, what affect do you think that this will have on page rendering times?

Two down...post it as a contribution :)

 

This will have virtually no effect on page parse time. Well, maybe in the 6th or 7th decimal place and only if there were more than a few thousand products on the page. Nice, quick mod...

 

A few tips though:

This:

//New Products Icon
// convert times to unix timestamp for comparison
$time_since_product_added = ($today_time - strtotime($listing['products_date_added']) );

//  2592000 = 30 days in the unix timestamp format
if ($time_since_product_added < 2592000)      {
$newprod_products_name = $listing['products_name'] . '</a> <img src="images/icon_newarrival.gif" alt="new">';
} else       {
$newprod_products_name = $listing['products_name'];
}
//New Products Icon END

 

Can be condensed into this:

//New Products Icon
// convert times to unix timestamp for comparison
$time_since_product_added = ($today_time - strtotime($listing['products_date_added']) );

//  2592000 = 30 days in the unix timestamp format
if ($time_since_product_added < 2592000) {
$listing['products_name'] .= ' <img src="images/icon_newarrival.gif" alt="new product">';
}
//New Products Icon END

...or even just this:

//New Products Icon
//  2592000 = 30 days in the unix timestamp format
if ( ($today_time - strtotime($listing['products_date_added'])) < 2592000) {
$listing['products_name'] .= ' <img src="images/icon_newarrival.gif" alt="new product">';
}
//New Products Icon END

That just adds the image to the link so that both the product name AND image are links.

 

Then, this code can stay the same:

if (isset($HTTP_GET_VARS['manufacturers_id'])) {
$lc_text = '<a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'manufacturers_id=' . $HTTP_GET_VARS['manufacturers_id'] . '&products_id=' . $listing['products_id']) . '">' . $listing['products_name'] . '</a>';
} else {
 $lc_text = ' <a href="' . tep_href_link(FILENAME_PRODUCT_INFO, ($cPath ? 'cPath=' . $cPath . '&' : '') . 'products_id=' . $listing['products_id']) . '">' . $listing['products_name'] . '</a> ';
}

Onse less step for installation and cleans up the code a bit...

Link to comment
Share on other sites

  • 2 weeks later...
  • 2 years later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...