OPTIONS IN A BUY NOW FORM POPULATED FROM CHECKED VALUES IN PRODUCT RECORD - Jul 23rd, 2013


I needed to set up a form field on a detail page’s PayPal “Buy Now” form that would allow a buyer to choose the
color of an item from only those colors available for that particular item.

The colors available were supposed to be limited those that were checked in a multi-value check box list field in that
item's record in the store_inventory section. (Possible values for the multi-value check box list field were pulled from
another database using the "record number" for the value and a" title" field value for the label.)

I had done something similar many times, allowing a visitor choose from a complete list of possible choices, but I
couldn’t seem to figure out how to limit their options to only those colors that were checked as being available for a
particular item (record).

That is, until Greg Thomas from Interactive Tools came to the rescue. He said:

You can use a getRecords function to retrieve the details for the product. The values and labels selected for that
product are included in the getRecords array, and you can select the values and labels like this:

<?php // load record from 'store_inventory'
list($colors, $store_inventoryMetaData) = getRecords(array(
'tableName' => 'store_inventory',
'where' => whereRecordNumberInUrl(1),
'loadUploads' => true,
'allowSearch' => false,
'limit' => '1',
));
?>
<?php $available_colors = $colors['0'];

//Create a drop down list by combining the values and labels into one array
$dropList = array_combine($available_colors['colors:values'], $available_colors['colors:labels']);

?>
<select name="os1">
<option value="">Please Choose a Color</option>
<!-- cycle through drop list to create options -->
<?php foreach($dropList as $value => $label): ?>
<option value = " <?php echo $label; ?>" <?php selectedIf($value, @$_REQUEST['colors']);?>> <?php echo $label;
?></option>
<?php endforeach; ?>
</select>

Based on Greg’s suggestion, in this example, there’s a multi value check box list field in my store_inventory
section called ‘colors’.

Since the ‘colors’ list field is being populated with all it’s possible values from another database, it uses the
record number from the other database as the value and the title from that database for the label.

In the code above I'm getting the data for a store_inventory record using the getRecords function.

Then the code creates a variable called $dropList that stores an array of the available_colors values and labels in this
record using the array_combine function.

Finally the code cycles through this list using a foreach loop to create a select input.

PAYPAL NOTES:
In order to allow a buyer to choose values for optional criteria like style, size, color, etc., you need 2 fields in
your buy now button.

on0 and os0, where on0 is the label for the criteria, I.E.: <input type="hidden" name="on1" value="Color">, and os0 is
the value for that criteria, IE: <input type="hidden" name="os1" value="Blue"> (or a list of options to choose from as
in this recipe)


The <?php selectedIf($value, @$_REQUEST['colors']);?> includes the color chosen in the submission to PayPal.

You can learn more about integrating PayPal HTML variables at:
https://developer.paypal.com/webapps/developer/docs/classic/paypal-payments-standard/integration-guide/Appx_websitestandard_htmlvariables/



The materials on this web site have been created for use with CMS Builder content management software. CMS Builder software is published and licensed for use by InteractiveTools.com. Please contact Interactive Tools for information on the downloading of the software or the purchasing of licenses.


Terms of Service