---- dataentry snippet ---- type : snippet # do not change this line category : Misc cart and checkout # mention the category like 'shipping', 'checkout' etc name : Product Bundles/Addons # the name of the snippet description : # description about snippet versions_tags : 0.7.0, 0.7.1, 0.7.2, 1.0, 1.1 # compatible versions reference_url : http://forum.foxycart.com/comments.php?DiscussionID=1572&page=1\#Comment_25071 # External link tags_tags : Advance, Snippets, cart and checkout # tags, separated by commas. date_dt : 2011-05-27 # the date in YYYY-MM-DD format ---- [[v:2.0:products#product_grouping_bundling_ensembles|Product bundling]] is now native in FoxyCart version 2.0+. This snippet should only be used on versions 1.1 and lower. ====== Product Bundles/Addons ======= {{ :snippets:misc_cart_and_checkout:productbundles2.jpg?direct&400|}}Packaged, bundled or addon products can be a great way to create special product offers or include required fees or costs with a purchase. This is especially useful when a customer is adding a subscription to a service that requires a once off setup fee to also be purchased at the same time. Or if a free product is being given to customers, but only if certain products are purchased at the same time. By utilising the following script, these scenarios are completely possible, allowing you to bundle any number of parent products to any number of dependent products with or without a matching quantity. ==== Step 1: Set up your products ==== For any products that you plan to use with this script, ensure that they all have unique product codes associated with them. For bundled products, this script leans towards them being added at the same time, through the same add-to-cart link or form. For adding multiple products in one [[http://wiki.foxycart.com/static/redirect/add_to_cart#adding_multiple_distinct_products_at_once|click here for more information]]. ==== Step 2: Add Javascript ==== Paste the following code right before the closing '''' tag of your cart template. ==== Step 3: Customise the javascript ==== Now that you have the script in place, you need to let it know what products it needs to look out for. Look for the ''productBundles'' in the script, and edit/add to the object. Adding another bundle is quite simple, simply follow this structure: {"parent":["parentCode1","parentCode2"], "dependent":["dependantCode1","dependantCode2"], "quantity-match":true} ; ''parent'' : **Description:** The codes for your parent products. : **Type:** An array of strings, comma seperated : **Example:** ''parent:["code1","code2","code3"]'' : **Notes:** Can take any number of codes. ; ''dependent'' : **Description:** The codes for your dependent products. : **Type:** An array of strings, comma seperated : **Example:** ''dependent:["code1","code2","code3"]'' : **Notes:** Can take any number of codes. ; ''quantity-match'' : **Description:** If true, the script requires that all dependant products in this set match the combined total of their matched parents and dependant product quantity inputs are set to read only. : **Type:** Boolean : **Example:** ''quantity-match:false'' : **Notes:** Only accepts ''true'' or ''false'' **Note:** If you are defining multiple bundles, they must be comma separated (each one must end in a comma apart from the last) ==== How it works ==== {{ :snippets:misc_cart_and_checkout:productbundleerror.jpg?direct&400|}}The script works by completing the following set of actions on cart load: - Loops through the productBundles object - Loops through the parent products array for the current productBundles object - Loops through fc_json.products looking for the parent product - Overwrites the delete function and the change and keyup functions if ''quantity-match'' is set to true - Sets a variable noting if the product was found - Loops through the dependent products array for the current productBundles object - Loops through fc_json.products looking for each dependent product - Sets the quantity input to read only and adds a fc_readonly class if the bundle is set to ''quantity-match'' - Hides the delete button - Adds a fc_dependent class to the parent tr - Adds a span to the dependent products name stating which parent product(s) it came from - If ''quantity-match'' is set to true, compares its quantity to the parent(s) quantity - If different, updates the quantity (to match the combined total of its parent(s), shows an error and prevents checkout - Sets a variable noting if the variable was not found - If a parent or dependent product was not found, shows an error message, makes the parent product read only with a class fc_readonly and prevents checkout ===== Related Forum Discussions ===== * http://forum.foxycart.com/comments.php?DiscussionID=1572&page=1#Comment_25071