ModularMerchant
Knowledge Base
Shopping Cart Software Online Manual
Including additional variables in "Add to Cart" links
Checkbox that adds additional products to the basket Override the default results per page
Location Home > Developer's Guide > Code Samples

Including additional variables in "Add to Cart" links

this article last updated: December 5, 2012
Due to inevitable changes that will be made to the shopping cart software over time, the cart's variable and database structure may change from time to time without advanced notice. We will attempt to keep the information in this article up-to-date, but Modular Merchant makes no guarantee as to the accuracy of the information provided in this article. Use this information at your own risk.

retaining additional variables
In addition to the usual variables for the product ID and quantity that we include in our QuickCode Tag, we want to include several more and have them available for use on our checkout pages. Is there a way to pass these additional variables, and have them retained?

For demonstration purposes, let's say that the goal is to create an "Add to Cart" link that adds 1 unit of product SID 311 to the cart and include two additional variables, referrer_id and checksum. The store should retain the values for referrer_id and checksum, so that they're available for reference by code on the checkout page.

The first question is, "can this be accomplished by using a standard QuickCheckout "Add to Cart" link like this?"
code
http://www.my-store.com/quick_checkout.php?id1=311&qty1=1&referrer_id=007&checksum=ABC


The answer to that question is "no, that approach would not work."

So, the second question is, "okay, smart guy, then how would it be accomplished?"

Here's how:

This would be accomplished by adding a page before the call to quick_checkout.php, which we'll call new_page.php. This new_page.php would first save the additional variables in the desired location (such in the session, cookies, database, etc.) and then forward the user on to the QuickCheckout page.

For example, the URL above would be changed to something like this:
code
http://www.my-store.com/new_page.php?id1=311&qty1=1&referrer_id=007&checksum=ABC


The new_page.php file may contain code similar to:
code
<?php
//Start the PHP session.
session_start();

//Save the incoming variables in session memory.
$_SESSION["referrer_id"] = $_REQUEST["referrer_id"];
$_SESSION["checksum"] = $_REQUEST["checksum"];

//Wrap things up and redirect to the checkout area...
session_write_close();
header("location: http://www.my-store.com/quick_checkout.php?id1=311&qty1=1");
exit();
?>


The new_page.php file could also be written to save the incoming variables in cookies, in a database, or whatever other imaginative things you come up with.

tip
To ensure that the session and/or cookie variables are accessible by the store's checkout page, new_page.php would need to be on the same website as the quick_checkout.php page.

By putting both on the same website, the two pages will share to the same session and cookie variables, allowing one page to access data saved by the other.

The additional variables would then be accessible for use or display on the checkout page. The storefront's checkout template could then display these variables by using code like this:
code
<?php
echo("Your referrer_id is: ".$_SESSION["referrer_id"]);
echo(
"Your checksum is: ".$_SESSION["checksum"]);
?>


That's one suggestion as to how additional incoming variables could be retained and used on other storefront pages. I hope this helps point you in the right direction!

— Last Edited - 12/5/2012 2:16pm PST
Rank and add a comment to this article
Only logged in users may leave comments and rank articles. Log in to have your say!
  Knowledge Base Home

  QuickCode™ Tag Glossary

  Modular Merchant.com Homepage

Not Logged in.
Either Log in or create a User Account.

Other articles in this category...

Can I add error messages to a storefront page?

Checkbox that adds additional products to the basket

Including additional variables in "Add to Cart" links

Override the default results per page

Using PHP includes in Templates