Need help listing "related products" on a Product page

By stsupery - July 24, 2014

Hello,

I'm creating a section on our product pages to display "People that bought this also bought..." followed by a selection of three other products. There's no logic in place determining who actually bought what, but there is a multi-list (checkboxes) where the person setting up a new product can select 3 other active products that will appear in that section. Products are referenced by their "AMS number". We use a commerce system called AMS, so the AMS number is basically a universal number like a SKU or UPC code.

Right now, I'm using the following SQL query to show only active products in the multi-select list (named "related_products") using "Get options from MySQL query (advanced)":

SELECT ams, ams
  FROM `<?php echo $TABLE_PREFIX ?>wines`
  WHERE ams_status = 'active'
  ORDER BY vintage DESC

For each of the (three) related products, I'd like to display the product thumbnail, product title, and the first 100 characters of the product description. The code generator lets you spit out an array of the values and/or labels, but I'm trying to get the rest of the record details, as well as related uploads.

I've been following the instructions in this thread: http://www.interactivetools.com/forum/forum-posts.php?postNum=2202269

But I encounter the following error with the Lookup Related Fields plugin:

lookupRelatedFields: field 'related_products' is not set to 'Get options from database (advanced)'

Can someone help me out?

Thanks,

-Dan

By stsupery - August 1, 2014

Hi Greg,

Thanks for getting me pointed in the right direction. I'm still having a few issues, however.

The filename is product_related.php, and the record number gets determined by the AMS number at the end of the url, as in www.server.com/product_related.php?ams=CS10DH7

Here's the code I'm using to load the records at the top of the page (after calling in the main library).

<?php
  // load record from 'wines'
  list($winesRecords, $winesMetaData) = getRecords(array(
    'tableName'   => 'wines',
    'loadUploads' => true,
    'allowSearch' => true,
    'limit'      => '1',
  ));
  $winesRecord = @$winesRecords[0]; // get first record
  if (!$winesRecord) { dieWith404("Record not found!"); } // show error message if no record found
?>

It's easy for me to pull up a comma-separated list of the AMS number or database record number with the following:

Related Products (values): <?php echo join(', ', $winesRecord['related_products:values']); ?><br/>
Related Products (labels): <?php echo join(', ', $winesRecord['related_products:labels']); ?>

What I'd like to do instead is explode out the array so I can use the other fields and uploads for each of the (three) selected related products.  If I use the following on the page:

<?php 
  //Get all of the category values from the category:values array, and implode them into a string
  $searchNums = implode(', ', $winesRecord['related_products:values']);

  //Return all matching wine categories with num values that are in the category:values array.
  $relatedWines = mysql_select('wines', "'num' IN($searchNums)");

  showme($relatedWines);
  exit;
?>

Then I get the following error message:

MySQL Error: Unknown column 'CS10DE7' in 'where clause' - in mysql_functions.php on line 224 by mysql_select_query()

(CS10DE7 is the AMS# of one of the related wines)

One other thing to note - if possible, I'd like to filter the products (wines) by their ams_status (active or inactive) before they get displayed to the user making that selection, so they don't accidentally select an older product that is no longer for sale (and also to reduce the number of possible choices from hundreds to tens).

Any ideas? Am I going about this all wrong? Thanks again for your help!!!

My best,

-Dan

By gregThomas - August 4, 2014 - edited: August 4, 2014

Hi Dan,

There are a couple of issues here, I've updated your code below:

  //Get all of the category values from the category:values array, and implode them into a string
  $searchNums = implode(', ', $winesRecord['related_products:values']);

  foreach($winesRecord['related_products:values'] as $key => $value){
    if($key == 0){
      $searchNums = "'$value'";
    }else{
      $searchNums .= ", '$value'";
    }
  }

  //Return all matching wine categories with num values that are in the category:values array.
  $relatedWines = mysql_select('wines', "`num` IN($searchNums)");

  showme($relatedWines);
  exit;

The first issue is that I'd assumed that the ams values were numeric, and didn't contain alpha characters. If you're using alpha characters in a value for MySQL, then they must have apostrophes around them. So I've had to use this foreach loop to add them around each value.

The second issue is that you didn't have the right type of apostrophe around the num field. If you're defining a field in a MySQL query, you should use the ones on the top left hand key on your keyboard which is below the ESC key.

I think there is something still wrong with your related wines mysql_select, because you're searching for the values in the num field, but this is used as the primary key for the CMSB database. So it can't contain the AMS key.

Hopefully this helps.

Cheers,

Greg

Greg Thomas







PHP Programmer - interactivetools.com