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 gregThomas - July 29, 2014

Hey Dan,

I think something like this should work:

  //Load the first wine record from my section
  list($wineRecord, $wineMetaData) = getRecords(array(
    'tableName'   => 'wine',
    'where'       => "ams_status = 'active'",
    'loadUploads' => true,
    'allowSearch' => false,
    'limit'       => '1',
    'orderBy'     => 'vintage DESC',
  ));
  $wineRecord = $wineRecord[0];

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

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

  showme($relatedWines);
  exit;

This is just example code, so you'll need to make a few changes to get it running (namely adding the field that stores multi check items).

So first use the getRecords function to pull the main wine record and add it to the $wineRecord variable.

getRecords will create an array of the related checkbox list items. It will be the field name with ":values" on the end, we can use this to create a comma seperated string of the AMS numer codes. 

Finally, you can use the mysql_select function to return the related wines. The first variable in the function is the table name, and the second is the where cause. 

The mysql IN function is used to search a single field for multiple values.

showme is a CMSB function that will show you the contents of a variable or array. 

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com

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