Skip to content

JS Methods

Trevor Fayas edited this page Apr 6, 2022 · 5 revisions

As with any Javascript library, you can extend and overwrite elements within it. If you desire to implement your own handling of certain aspects of the JavaScript libraries, you can replace the functions like this:

// Imports
const customAddToCart = addToCart || InitializeEcommerceService("AddToCart");
const customCheckout = checkout || InitializeEcommerceService("Checkout");
const customShoppingCart = shoppingCart || InitializeEcommerceService("ShoppingCart");
const customEcommerceClass = ecommerceClass|| InitializeEcommerceService("EcommerceClass");

// Override with a custom plea
customShoppingCart.removeItem = function(event) {

	        // Custom plea
			if(window.confirm("You don't really want to remove this item, do you?  I mean, it's a really cool item!")) {
			
	            return ecommerceClass.ajax("/ShoppingCart/RemoveItem", {
	                method: "POST",
	                body: event.detail.ID.toString(),
	                headers: ecommerceClass.getPostHeaders()
	            }).then((response) => {
	                return response.json();
	            }).then((json) => {
	                if (json.alert) {
	                    document.body.dispatchEvent(ecommerceClass.showAlertEvent(json.alert));
	                }
	                return json.html;
	            }).catch((error) => {
	                document.body.dispatchEvent(ecommerceClass.showAlertEvent(error.message));
	            });
            } else {
	            return null;
            }

    }

Keep in mind it is probably better to leverage TypeScript than render the normal javascript like this. In the repository there are the typescript definition files for each object and function so you can mimic them.

Clone this wiki locally