Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

Help Needed merging 2 Contributions


Marc_J

Recommended Posts

I'm trying to merge two good contributions, without much success!

 

The contributions are: -

 

Include all Subcategories for 'New Products in ...'

 

and

 

Random New Products

 

Both contributions consist simply of one replacement file for includes/modules/new_products.php

 

The "Include SubCats" new_products.php : -

 

<?php
/*
 $Id: new_products.php,v 1.35 2003/09/08 13:25:44 project3000 Exp $
 
***************************************************************************
Contribution: Include all Subcategories for 'New Products', version 1.0
Mod by Nils Petersson, 2004/05/02 
***************************************************************************

 osCommerce, Open Source E-Commerce Solutions
 http://www.oscommerce.com

 Copyright (c) 2003 osCommerce

 Released under the GNU General Public License
*/

?>

<!-- new_products //-->
<?php

if ( (!isset($new_products_category_id)) || ($new_products_category_id == '0') ) {  // in index.php
  $new_products_query = tep_db_query("select p.products_id, p.products_image, p.products_tax_class_id, if(s.status, s.specials_new_products_price, p.products_price) as products_price from " . TABLE_PRODUCTS . " p left join " . TABLE_SPECIALS . " s on p.products_id = s.products_id where products_status = '1' order by p.products_date_added desc limit " . MAX_DISPLAY_NEW_PRODUCTS);

} else {   // in a category

$cats[] = $new_products_category_id; // current catID as starting value
// put cat-IDs of all cats nested in current branch into $cats array, go through all subbranches
for($i=0;$i<count($cats);$i++) {
 $categorie_query = tep_db_query("select categories_id from " . TABLE_CATEGORIES . " where parent_id = '" . (int)$cats[$i] . "'"); 
 while ($categorie = tep_db_fetch_array($categorie_query)) {
	 $cats[] = $categorie['categories_id'];
 }
 $cats=array_unique($cats); // sort out doubles
}

$num = (int) MAX_DISPLAY_NEW_PRODUCTS;
$new_products_query = tep_db_query("select distinct p.products_id, p.products_image, p.products_tax_class_id, if(s.status, s.specials_new_products_price, p.products_price) as products_price from " . TABLE_PRODUCTS . " p left join " . TABLE_SPECIALS . " s on p.products_id = s.products_id, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c, " . TABLE_CATEGORIES . " c where p.products_id = p2c.products_id and p2c.categories_id = c.categories_id and c.parent_id in (".implode(', ', $cats).") and p.products_status = '1' order by p.products_date_added desc limit " . $num);
}

if (tep_db_num_rows($new_products_query) > 0) {
$info_box_contents = array();
$info_box_contents[] = array('text' => sprintf(TABLE_HEADING_NEW_PRODUCTS, strftime('%B')));
new contentBoxHeading($info_box_contents);

$row = 0;
$col = 0;
while ($new_products = tep_db_fetch_array($new_products_query)) {
  $new_products['products_name'] = tep_get_products_name($new_products['products_id']);
  $info_box_contents[$row][$col] = array('align' => 'center',
                    'params' => 'class="smallText" width="33%" valign="top"',
                    'text' => '<a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $new_products['products_id']) . '">' . tep_image(DIR_WS_IMAGES . $new_products['products_image'], $new_products['products_name'], SMALL_IMAGE_WIDTH, SMALL_IMAGE_HEIGHT) . '</a><br><a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $new_products['products_id']) . '">' . $new_products['products_name'] . '</a><br>' . $currencies->display_price($new_products['products_price'], tep_get_tax_rate($new_products['products_tax_class_id'])));

  $col ++;
  if ($col > 2) {
   $col = 0;
   $row ++;
  }
}
new contentBox($info_box_contents);
}

?>
<!-- new_products_eof //-->

 

 

 

And the "Random New Products" new_products.php : -

 

<?php
/*
 $Id: new_products.php,v 1.35 2004/02/05 15:50:00 adf Exp $
 Randomize New Products
 $Id: new_products.php,v 1.34 2003/06/09 22:49:58 hpdl Exp $

 osCommerce, Open Source E-Commerce Solutions
 http://www.oscommerce.com

 Copyright (c) 2003 osCommerce

 Released under the GNU General Public License
*/
?>
<!-- new_products //-->
<?php
 $info_box_contents = array();
 $info_box_contents[] = array('text' => sprintf(TABLE_HEADING_NEW_PRODUCTS, strftime('%B')));

 new contentBoxHeading($info_box_contents);

 if ( (!isset($new_products_category_id)) || ($new_products_category_id == '0') ) {
// start random new products
   $new_products_query = tep_db_query("select p.products_id, p.products_image, p.products_tax_class_id, if(s.status, s.specials_new_products_price, p.products_price) as products_price from " . TABLE_PRODUCTS . " p left join " . TABLE_SPECIALS . " s on p.products_id = s.products_id where products_status = '1' order by p.products_date_added desc limit " . MAX_RANDOM_SELECT_NEW);
 } else {
   $new_products_query = tep_db_query("select distinct p.products_id, p.products_image, p.products_tax_class_id, if(s.status, s.specials_new_products_price, p.products_price) as products_price from " . TABLE_PRODUCTS . " p left join " . TABLE_SPECIALS . " s on p.products_id = s.products_id, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c, " . TABLE_CATEGORIES . " c where p.products_id = p2c.products_id and p2c.categories_id = c.categories_id and c.parent_id = '" . (int)$new_products_category_id . "' and p.products_status = '1' order by p.products_date_added desc limit " . MAX_RANDOM_SELECT_NEW);
// end random new products    
 }
// start random new products
 // To configure:
 //
 // Under Admin - Configuration - Maximum Values
 // Set the following:
 //  'Selection of Random New Products' 
 //   (MAX_RANDOM_SELECT_NEW) - Sets how many of the most recent new product entries are queried
 //  'New Products Module'
 //   (MAX_DISPLAY_NEW_PRODUCTS) - Sets how many random new products are displayed on screen
 //
 $row = 0;
 $col = 0;
 $pCount = 0;
 $info_box_contents = array();
 $found_products = array();
 $num_rows = tep_db_num_rows($new_products_query);
 if (MAX_DISPLAY_NEW_PRODUCTS > MAX_RANDOM_SELECT_NEW) {
   // don't allow more new products than will be queried
   $max_new_products = MAX_RANDOM_SELECT_NEW;
 } else {
   $max_new_products = MAX_DISPLAY_NEW_PRODUCTS;
 }
 if ($num_rows < $max_new_products) {
	 $max_new_products = $num_rows;
 }
//  echo "num_rows: " . $num_rows . " max_new_products " . $max_new_products . "<br>";
 if ($num_rows > 0) {        
   while ($pCount < $max_new_products) {    
     // choose a random row 
     $random_product = '';
     $random_row = tep_rand(0, ($num_rows - 1));
     tep_db_data_seek($new_products_query, $random_row);
     $random_product = tep_db_fetch_array($new_products_query);
     // see if found already, if not use else skip
     $found = 0;
     for ($ii=0; $ii < $pCount; $ii++) {
       if ($found_products[$ii] == $random_product['products_id']) {
         $found = 1;
         break;
       }
     }
     if ($found == 0) {
       // keep track of found id's
       $found_products[$pCount] += $random_product['products_id'];
       $pCount ++; 
       // add to display array
       $random_product['products_name'] = tep_get_products_name($random_product['products_id']);
       $info_box_contents[$row][$col] = array('align' => 'center',
                                          'params' => 'class="smallText" width="33%" valign="top"',
                                          'text' => '<a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $random_product['products_id']) . '">' . tep_image(DIR_WS_IMAGES . $random_product['products_image'], $random_product['products_name'], SMALL_IMAGE_WIDTH, SMALL_IMAGE_HEIGHT) . '</a><br><a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $random_product['products_id']) . '">' . $random_product['products_name'] . '</a><br>' . $currencies->display_price($random_product['products_price'], tep_get_tax_rate($random_product['products_tax_class_id'])));
       $col ++;
       if ($col > 2) {
         $col = 0;
         $row ++;
       }
     } // found
   } // while pCount
 } // num_rows  
// end random new products

 new contentBox($info_box_contents);
?>
<!-- new_products_eof //-->

 

If anyone has already done this, or wants to have a go, it'd be a fantastic help!

Link to comment
Share on other sites

Get a file compare tool and then you should be able to do it yourself.

Link to comment
Share on other sites

Merged the files in Beyond Compare (below) and the code runs ok but whether it's doing what it should, you tell me!!!

 

<?php
/*
$Id: new_products.php,v 1.35 2004/02/05 15:50:00 adf Exp $
Randomize New Products
$Id: new_products.php,v 1.34 2003/06/09 22:49:58 hpdl Exp $

osCommerce, Open Source E-Commerce Solutions
http://www.oscommerce.com

Copyright (c) 2003 osCommerce

Released under the GNU General Public License
*/
?>
<!-- new_products //-->
<?php

if ( (!isset($new_products_category_id)) || ($new_products_category_id == '0') ) {
// start random new products
  $new_products_query = tep_db_query("select p.products_id, p.products_image, p.products_tax_class_id, if(s.status, s.specials_new_products_price, p.products_price) as products_price from " . TABLE_PRODUCTS . " p left join " . TABLE_SPECIALS . " s on p.products_id = s.products_id where products_status = '1' order by p.products_date_added desc limit " . MAX_RANDOM_SELECT_NEW);
} else {   // in a category

$cats[] = $new_products_category_id; // current catID as starting value
// put cat-IDs of all cats nested in current branch into $cats array, go through all subbranches
for($i=0;$i<count($cats);$i++) {
$categorie_query = tep_db_query("select categories_id from " . TABLE_CATEGORIES . " where parent_id = '" . (int)$cats[$i] . "'"); 
while ($categorie = tep_db_fetch_array($categorie_query)) {
 $cats[] = $categorie['categories_id'];
}
$cats=array_unique($cats); // sort out doubles
}

$num = (int) MAX_DISPLAY_NEW_PRODUCTS;
$new_products_query = tep_db_query("select distinct p.products_id, p.products_image, p.products_tax_class_id, if(s.status, s.specials_new_products_price, p.products_price) as products_price from " . TABLE_PRODUCTS . " p left join " . TABLE_SPECIALS . " s on p.products_id = s.products_id, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c, " . TABLE_CATEGORIES . " c where p.products_id = p2c.products_id and p2c.categories_id = c.categories_id and c.parent_id in (".implode(', ', $cats).") and p.products_status = '1' order by p.products_date_added desc limit " . MAX_RANDOM_SELECT_NEW);
// end random new products    
}

if (tep_db_num_rows($new_products_query) > 0) {
$info_box_contents = array();
$info_box_contents[] = array('text' => sprintf(TABLE_HEADING_NEW_PRODUCTS, strftime('%B')));
new contentBoxHeading($info_box_contents);
$row = 0;
$col = 0;
$pCount = 0;
$info_box_contents = array();
$found_products = array();
$num_rows = tep_db_num_rows($new_products_query);
if (MAX_DISPLAY_NEW_PRODUCTS > MAX_RANDOM_SELECT_NEW) {
  // don't allow more new products than will be queried
  $max_new_products = MAX_RANDOM_SELECT_NEW;
} else {
  $max_new_products = MAX_DISPLAY_NEW_PRODUCTS;
}
if ($num_rows < $max_new_products) {
 $max_new_products = $num_rows;
}
//  echo "num_rows: " . $num_rows . " max_new_products " . $max_new_products . "<br>";
if ($num_rows > 0) {        
  while ($pCount < $max_new_products) {    
    // choose a random row 
    $random_product = '';
    $random_row = tep_rand(0, ($num_rows - 1));
    tep_db_data_seek($new_products_query, $random_row);
    $random_product = tep_db_fetch_array($new_products_query);
    // see if found already, if not use else skip
    $found = 0;
    for ($ii=0; $ii < $pCount; $ii++) {
      if ($found_products[$ii] == $random_product['products_id']) {
        $found = 1;
        break;
      }
    }
    if ($found == 0) {
      // keep track of found id's
      $found_products[$pCount] += $random_product['products_id'];
      $pCount ++; 
      // add to display array
      $random_product['products_name'] = tep_get_products_name($random_product['products_id']);
      $info_box_contents[$row][$col] = array('align' => 'center',
                                         'params' => 'class="smallText" width="33%" valign="top"',
                                         'text' => '<a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $random_product['products_id']) . '">' . tep_image(DIR_WS_IMAGES . $random_product['products_image'], $random_product['products_name'], SMALL_IMAGE_WIDTH, SMALL_IMAGE_HEIGHT) . '</a><br><a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $random_product['products_id']) . '">' . $random_product['products_name'] . '</a><br>' . $currencies->display_price($random_product['products_price'], tep_get_tax_rate($random_product['products_tax_class_id'])));
      $col ++;
      if ($col > 2) {
        $col = 0;
        $row ++;
      }
    } // found
  } // while pCount
} // num_rows  
// end random new products

new contentBox($info_box_contents);
}
?>
<!-- new_products_eof //-->

Light, in the absence of eyes, illuminates nothing.

Link to comment
Share on other sites

Merged the files in Beyond Compare (below) and the code runs ok but whether it's doing what it should, you tell me!!!

 

It seems to work! Thank-you very much!

 

Was that done using an automatic "merge files" in Beyond Compare? I haven't used it enough to know my way around it - wasn't aware there was such a utility, I was trying to manually merge them, just using Beyond Compare to see the differences.

 

If I have time, I'll take a close look at it, tidy it up (comments) and up it as a contrib - with full credits to the two original authors and yourself, of course!

 

Thanks again :)

Link to comment
Share on other sites

  • 3 weeks later...

would this do the following:

 

is there a contribution that i can have displaying 10 products on my home page which are randomised by category everytime i refresh the page.

 

like on this website (refresh page and different products appear!):

 

http://www.eclipsecomputers.com

 

if there is'nt is there anything to show my products on my home page as i want to get rid of the what's new box.

 

thanks

Link to comment
Share on other sites

Take the "Include SubCats" new_products.php and change this:

order by p.products_date_added desc

to this:

order by RAND()

Looks like there are 2 places to change this.

 

This will randomise the product display of the "include subcats" new_products.php - note that I have not looked at the rest of the code to see if it is OK or not.

 

nice one burt, think its done what i wanted! will let yopu know how i get on!

Link to comment
Share on other sites

  • 4 weeks later...

Further help required!

 

Cross-Ref : See This Topic

 

I noticed that new_products modules (both the default, and this modified one) don't display "specials" prices correctly, i.e. in red, with a score-through for the regular price. It simply displays the correct "specials" price, with no indication that this is a special price.

 

If anyone has any suggestions, it'd be much appreciated, although in the other thread, please, as enigma1 is kindly helping over there...

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...