In this step of the tutorial, you implement the function
to add a product into a shopping cart for a recipe.
About this task
You will create a JavaScript function to call the AjaxAddOrderItem
service. This function will parse the product ID from the recipe display
page, and then pass the product ID to the service. This is very similar
to "add to shopping cart" for bundles. You can read the AddBundle2ShopCartAjax
function in the CategoryDisplay.js file as reference.
Procedure
- Create RecipeDisplay.js.
-
In the Project Explorer view, expand
- Right-click the javascript folder
and select
- In the Folder name field, enter Recipe
- Right-click the Recipe folder
and select Import. Expand .
- Click Next , then click Browse and
navigate to TutorialSource\RecipeTool\Stores\WebContent\Madisons\ShoppingArea\javascript\Recipe directory,
where TutorialSource is the location where you
unzipped the Tutorial sample source code. Select the RecipeDisplay.js file.
- Modify RecipeDisplay.jsp to use RecipeDisplay.js
- Include RecipeDisplay.js.
In the
RecipeDisplay.jsp file, add the
following code into the head section.
<script type="text/javascript"
src="<c:out value="${jspStoreImgDir}javascript/Recipe/RecipeDisplay.js"/>"></script>
- Add the initialization code.
In the
RecipeDisplay.jsp file,
add the following code into the head section.
<script type="text/javascript">
dojo.addOnLoad(function() {
MessageHelper.setMessage("NO_SELECTED_PRODUCTS", "<fmt:message key="NO_SELECTED_PRODUCTS" bundle="${storeText}"/>");
MessageHelper.setMessage("SHOPCART_ADDED", "<fmt:message key="ProjectAddtoCartSucessInfo" bundle="${storeText}"/>");
recipeDisplayJS.setCommonParameters('${WCParam.langId}','${WCParam.storeId}','${WCParam.catalogId}');
});
</script>
The above code will initialize MessageHelper
and
the JavaScript function will use the messages by MessageHelper
; recipeDisplayJS.setCommonParameters()
sets
the initialization of RecipeDisplay.js file.
- Set
href
value to point to the JavaScript
function.In the
RecipeDisplay.jsp file,
search for
id="WC_recipe_addtocart_1" and set
the href attribute.
href='javascript: recipeDisplayJS.AddRecipeIngredients2ShopCartAjax("materialForm")'
Note: Setting the href attribute to the above value will
invoke the AddRecipeIngredients2ShopCartAjax function when you click Add
the selected products to Cart.
- Search for <div id="body588">.
Add <%@ include file="../../include/MessageDisplay.jspf"%> underneath.
- Verify this task.
- Browse a recipe
- Click Add the selected products to Cart.
Confirm the product is added to the shopping cart.
- Do not select any checkbox and click Add
the selected products to Cart again. Confirm the alert
message displays.