MMv4 KB
 
Checkbox that adds additional products to the basket
  Last Edited - 02/9/2017 4:32pm PST
  Category Path - Developer's Guide > Code Samples
 
quote
I want to create a checkbox on my "Mac N' Cheese" product page, which upon selection, simultaneously adds another product named "Empty Bowl". While the checkbox is selected, submitting the page should add both "Mac N' Cheese" AND "Empty Bowl". 

It's possible to create a checkbox on a product's details page that adds additional products, using a mixture of JavaScript and Modular Merchant's quick links.

To do so, there are a few steps involved:

prerequisites
This article assumes the reader has familiarity with the following:

- HTML & JavaScript.
- Modular Merchant Quick Links.
- Template Package Sandbox.

use at your own risk
The following solution and its code sample are not supported by the Modular Merchant support team. Implementation of this code, or any revisions to said code, may potentially be performed as a paid programming project, but will not be troubleshot by the support team.

 
Creating and assigning a new product template

First, a new template should be created & assigned to the Mac N' Cheese product. The purpose of this is so that only the product page for Mac N' Cheese has the checkbox, rather than every product in the store.
  1. Navigate to [Design > Template Package Sandbox].
  2. Select a Template Package to Edit.
  3. If product.tpl is not present under Template Package Files, click Populate Templates.
    1. Select the checkbox for product.tpl.
    2. Populate Selected Templates.
  4. Under Template Package Files, select product.tpl.
  5. Scroll down to File Options, select "Save as a copy..." and give the template a unique name. (Example: product_macncheese)
  6. Save a Copy.
After creating the new template, it may be assigned to the Mac N' Cheese product:
  1. Navigate to [Products > Search Products].
  2. Open the product in the editor.
  3. For option #14, Template, select the new template.
  4. Save Changes.
If you would rather wait to apply the new template until after the code change has been made, that is perfectly acceptable.
 
Adding the checkbox and JavaScript

Once the new template has been created/assigned, we'll need to adjust it to add our checkbox and a bit of JavaScript.

1.) Navigate to [Design > Template Package Sandbox]
2.) Select the new template. (Example: product_macncheese.tpl)
3.) Viewing the editor in Plain Text mode, browse to where you would like to add the checkbox. (Example: Directly above the Add to Cart button.)
4.) Add the following code:

code
<!-- HTML Code -->
<label><input id="extra_prod" name="extra_prod" type="checkbox" value="yes" />&nbsp&nbsp Don't prefer to devour your mac like an animal? Need a bowl? Check the box to add one! [Adds $2.00]</label> 

<!-- JavaScript Code -->
<script type="text/javascript">
$('#extra_prod').on('change', function(){
if ($(this).is(':checked')) {
$('form').attr('action', 'https://www.mystore.com/quick_return.php?id1=123&qty1=1');
} else {
$('form').attr('action', '{THIS-PAGE}');
}
});
</script>

The lines in blue are HTML comments that can be removed, while the text in red should be changed to their correct values.

What's the code doing?
The HTML code is to create the checkbox and its selectable text, while the JavaScript code serves to modify the form action of the page when the checkbox is selected. The red link within the JavaScript code should be changed to the quick return link of your additional product (ex: Empty Bowl). To find that product's quick return link, navigate to [Products > Search Products], and click View Links for your product, or use the instructions from this article.

5.) Save Changes.
 
End result

The end result of adding this custom code is as follows:
  • If the checkbox is selected, the form action is changed to a quick return link that adds our Empty Bowl. Since we're submitting the form for our Mac N' Cheese product to the Empty Bowl quick link, both products are added.
     
  • If the checkbox is not selected, the form action remains the same as it was before. You get all the mac, but no bowl.
     

How the box and text may appear if added directly above the Add to Cart button.
 
We hope this example helps add another tasty bite of goodness to your storefront. 
Powered by ModularKB