GCart
From MODx Wiki
MODx Snippet
- Author
- Jesse R.
- MODx Versions
- >= 0.9.6
- Last Updated
- 12/13/2007
Download Snippet
Support Thread
SVN Repository
The documentation effort is a work in progress. I will try to add all sorts of useful information on how stuff was done on Blue Sky Vineyard If you are unsure of how something was accomplished there, ask in the support thread, and then I will update the Wiki with an example.
This contribution can utilize Google Checkout or PayPal with UPS. However, these are mutually exclusive, so you cannot use both together.
Installation
- Upload the contents of the gCart package to assets/snippets/gCart
- Paste the contents of gCart/snippets/gCart.php into a new snippet named gCart (MAKE SURE YOU ONLY HAVE ONE SET OF <?php ?> tags).
- Paste <?php return time();?> into a snippet named gToken.
- Paste the contents of gCart/plugins/gSession.php into a new plugin named gSession (MAKE SURE YOU HAVE NO <?, <?php, or ?> tags in your plugin). Paste "&gOptions=Maximum Amount of Options;string;2" into the configuration of the plugin. Click "Update parameter display" and change the value to the maximum amount of configurable options that an item in your store could have. On the "System Events" tab, make sure "OnWebpageInit" is checked.
- Create a template and place a call to [!gCart!] using the parameters set above or from the examples below. Simply [!gCart!] should function though.
- Create some TVs (Assigned to the template you created):
- gPrice - Input Type: Number; Default Value: 0.
- gWeight - Input Type: Number; Default Value: 0.
- gDiscount - Input Type: Text; Default Value: BLANK.
- gTaxExempt - Input Type: Radio Options; Input Option Values: No||Yes.
- gFreeShip - Input Type: Radio Options; Input Option Values: No||Yes.
- gToken - Input Type: Text; Default Value: [!gToken!].
- Create a chunk for each file contained in gCart/chunks/
- Create a new document. Set its template to the one you created containing the call to [!gCart!]. Fill out the TVs for this document. In the documents content place one of the "Add*" chunks you created (e.g., AddRegularItem).
You should now be in business. The output might not be pretty, but it should at least allow you to add items to the cart. The files contained in gCart/chunks/ should give you a good idea of how to template your output. After looking at those you could then template your output and pass it to the snippet via the template parameters.
Parameters
gID - This is the Merchant ID. It can be found by logging into your Google Checkout account. Defaults to the Sandbox ID.
gKey - This is the Merchant Key. It can be found by logging into your account and going to Settings->Integration. Defaults to the Sandbox Key.
gTaxRate - Tax Rate. Defaults to 0.065.
TaxShipping - Should shipping be taxed. Defaults to true.
gZipcode - Origination zip code. Defaults to 62902.
gCity - Origination city. Defaults to Carbondale.
gState - Origination state. Defaults to IL.
gShipID - Name of origination location. Defaults to Blue Sky.
gItemTpl - A chunk that contains placeholders for the output of each item in the cart.
gCartTpl - A chunk that contains placeholders for the output of the cart. This wraps around the items.
gAjaxTpl - A chunk that contains a placeholder to wrap your entire cart in for utilizing javascript. For example, this template can be used along with javascript to flash the cart when an item is added.
gEmptyTpl - A chunk that contains html of an empty cart.
Placeholders
Certain placeholders are only available inside certain templates. Here is the breakdown:
gItemTpl
[+gName+] - The name of the item.
[+gSKU+] - The unique item code/SKU/UPC for the item.
[+gAlias+] - The item's/document's alias.
[+gQuantity+] - The quantity of this item that is currently inside the cart.
[+gItemURL+] - The URL to the item/document.
[+gClearItemURL+] - The URL that will clear the item from the cart.
[+gIncrementURL+] - The URL that will increase the item's quantity by one (1).
[+gDecrementURL+] - The URL that will decrease the item's quantity by one (1).
[+gPrice+] - The item's price.
gCartTpl
[+gWrap+] - The output of all the templated items.
[+gClearCartURL+] - The URL that will clear the entire cart.
[+gTotalItems+] - The total quantity of items in the cart.
[+gSubtotal+] - The current total of the items in the cart. This does not include discounts.
[+gDiscount+] - The discount amount.
[+gButton+] - The checkout button.
gAjaxTpl
[+gAjax+] - Used to wrap the cart in html on a POST. TODO: Should also wrap on a GET.
gEmptyTPL
No placeholders. Whatever html is placed in here is output directly when the cart is empty.
Example Calls
[!gCart? &gItemTpl=`gItemTpl` &gCartTpl=`gCartTpl`!]
Sets the templates to chunks with names "gItemTpl" and "gCartTpl." It utilizes the sandbox API ID and Key I created for this snippet.
More calls to come...
Shipping Methods
Open up assets/snippets/gCart/include/gCart.inc.php for editing. Look for the following code:
//Shipping Options
$gShippingOption1 = new gShippingOption('20.00','UPS','REGULAR_PICKUP','Ground','5.63');
$gShippingOption2 = new gShippingOption('40.00','UPS','REGULAR_PICKUP','3 Day Select','5.63');
$gShippingOption3 = new gShippingOption('60.00','UPS','REGULAR_PICKUP','Next Day Air','5.63');
Using "$gShippingOption1" as an example, the following are what each variable represents:
- 20.00 = The amount Google should use if it is unable to get dynamically get a rate from the carrier.
- UPS = The carrier to use. (UPS, FedEx, or USPS).
- REGULAR_PICKUP = An indication of how the carrier will pick up the package. (REGULAR_PICKUP, SPECIAL_PICKUP, DROP_OFF).
- Ground - The shipping option available to the buyer. Available options can be found here Shipping Options
- 5.63 - Handling fee added to the value returned by the carrier.
TODO: Abstract this out into parameters in the snippet call.
Discounting
Open up assets/snippets/gCart/include/gCart.inc.php for editing. Look for the following code:
//Add all your discounts by calling the functions created in the discount file
addTieredDiscount('.1,.15,.2','11,23,47','wine','Bulk Discount');
- .1,.15,.2 - The percentage discount applied to items meeting the criteria.
- 11,23,47 - The quantity required to meet the respective discounts.
- wine - The category that an item must be in to qualify for the discount.
- Bulk Discount - The description of the discount.
TODO: Abstract this out into parameters in the snippet call. Add other discounting methods as requested. Allow fees, discounts, and shipping to be added via a plugin architecture similar to eForm.
Going to Production Mode
Edit gCart/config.php by changing $use_sandbox to TRUE.
Explanation of Blue Sky Vineyards' Setup
This links to the example site: Illinois Wine
Creating Products
Each product is represented by a document. In our case, we sell wine. These wines fall into a variety of categories. So the documents sit in containers named after the categories. This is what it looks like in the manager.

Each individual product has all the document information filled out, including the template variables referenced above like so:

This uses the "Product" template which contains the call to the gCart or pCart snippet that shows up in the sidebar at Blue Sky Vineyards' site. It also uses the gItem chunk included in the package to provide an "Add to Cart" button. We also see the gPrice TV being pulled in for price display.

Here are all the templates as described above. As you can see I pulled them into their own tab for easier management using the managermanager plugin. Download. I have also used some other TVs that I use for category listing output with ditto, such as Product Image and Image Height (which was needed for DirectResize - Highslide effect).
