You are here

facebookEcommerceProducts

Used by the following Containers: 
Variable Type: 
Custom Javascript
Gets the productList object from santianoEcommerce and transforms it for Facebook Ecommerce events
	function() {
// grabs the productList from the santiano.io Ecommerce Events and prepares it for Mark Z.
var eventCommand = {{eventCommand}};
var productList = {{productList}};
var productListLength = {{productListLength}};
var facebookEcommerceProducts = [];
var i;

if (eventCommand == 'productDetail' ||
    eventCommand == 'cartProductAdded' ||
    eventCommand == 'checkoutStep' || 
    eventCommand == 'orderConfirmation') {
    
	for ( i = 0; i < productListLength; i++ ) {
      
		// 5 level category magic
		var fbCategoryMagic = [];
		if (productList[i].productCategory1 != undefined) {
			fbCategoryMagic.push(productList[i].productCategory1);
		}
		if (productList[i].productCategory2 != undefined) {
			fbCategoryMagic.push(productList[i].productCategory2);
		}
		if (productList[i].productCategory3 != undefined) {
			fbCategoryMagic.push(productList[i].productCategory3);
		}
		if (productList[i].productCategory4 != undefined) {
			fbCategoryMagic.push(productList[i].productCategory4);
		}
		if (productList[i].productCategory5 != undefined) {
			fbCategoryMagic.push(productList[i].productCategory5);
		}
		// Common Facebook Product Object formating :)
		var facebookIndividualProduct = {
			'content_name': productList[i].productName,
			'id': productList[i].productId,
			'item_price': productList[i].productUnitPrice,
			'content_category': fbCategoryMagic,
		};

		if ( eventCommand == 'cartProductAdded' ||
                     eventCommand == 'checkoutStep' || 
		     eventCommand == 'orderConfirmation') {
		// Specific cases with these eventCommand that require a quantity
		facebookIndividualProduct.quantity = productList[i].productQuantity;
		}

		// My facebook individual Product is ready, I push to the FB product List
		facebookEcommerceProducts.push(facebookIndividualProduct);

	} // End of For Loop

	return facebookEcommerceProducts;

} else {

	// No  products should be sent to Facebook otherwise
	return [];
} // end of If statement

} // end of function