From ee271d98c8a81e29bc3227db317070eef8352a43 Mon Sep 17 00:00:00 2001 From: Marion Muszynski Date: Tue, 7 Mar 2017 13:25:46 +0100 Subject: [PATCH] continuation of updating of market data with addition of remove from cart to GA and addition of name of sale when product is sent to GA --- modules/blockcart/blockcart-json.tpl | 5 +- modules/blockcartex/ajax-cart.js | 2 + modules/expresscart/ajax.php | 14 ++- modules/expresscart/product_footer.tpl | 3 + modules/ganalytics/ganalytics.php | 16 +++- modules/ganalytics/header.tpl | 1 + override/classes/Cart.php | 11 +++ themes/site/header.tpl | 30 +++++++ themes/site/js/cart-summary.js | 73 ++++++++++++++++ .../modules/blockcartex/blockcart-json.tpl | 1 + themes/site/shopping-cart-product-line.tpl | 4 +- themes/site/shopping-cart.tpl | 6 +- themes/site_mobile/header.tpl | 30 +++++++ themes/site_mobile/js/cart-summary.js | 86 +++++++++++++++++-- .../modules/blockcartex/blockcart-json.tpl | 1 + .../shopping-cart-product-line.tpl | 12 +-- 16 files changed, 272 insertions(+), 23 deletions(-) diff --git a/modules/blockcart/blockcart-json.tpl b/modules/blockcart/blockcart-json.tpl index a9bac4e7..1e9da317 100755 --- a/modules/blockcart/blockcart-json.tpl +++ b/modules/blockcart/blockcart-json.tpl @@ -1,5 +1,5 @@ {* -* 2007-2011 PrestaShop +* 2007-2011 PrestaShop * * NOTICE OF LICENSE * @@ -34,9 +34,10 @@ "id": {$product.id_product}, "link": "{$link->getProductLink($product.id_product, $product.link_rewrite, $product.category)|addslashes|replace:'\\\'':'\''}", "quantity": {$product.cart_quantity}, - "id_image": "{$link->getImageLink($product.link_rewrite, $product.id_image, 'mini_carre')|addslashes}", + "id_image": "{$link->getImageLink($product.link_rewrite, $product.id_image, 'mini_carre')|addslashes}", "priceByLine": "{if $priceDisplay == $smarty.const.PS_TAX_EXC}{displayWtPrice|html_entity_decode:2:'UTF-8' p=$product.total}{else}{displayWtPrice|html_entity_decode:2:'UTF-8' p=$product.total_wt}{/if}", "name": "{$product.name|html_entity_decode:2:'UTF-8'|escape|truncate:40:'...':true}", + "category_name": "{$product.category_name|html_entity_decode:2:'UTF-8'|escape|truncate:16:'...':true}", "unit_price": "{displayWtPrice|html_entity_decode:2:'UTF-8' p=$product.price_wt}", "price": "{if $priceDisplay == $smarty.const.PS_TAX_EXC}{displayWtPrice|html_entity_decode:2:'UTF-8' p=$product.total}{else}{displayWtPrice|html_entity_decode:2:'UTF-8' p=$product.total_wt}{/if}", "idCombination": {if isset($product.attributes_small)}{$productAttributeId}{else}0{/if}, diff --git a/modules/blockcartex/ajax-cart.js b/modules/blockcartex/ajax-cart.js index d7bec2af..5375f20e 100755 --- a/modules/blockcartex/ajax-cart.js +++ b/modules/blockcartex/ajax-cart.js @@ -632,6 +632,8 @@ function confirmation(idProduct, idCombination, quantity, json) { "id" : idProduct, "variant" : idCombination, "name" : ""+json.products[i].name+"", + "brand": ""+json.products[i].category_name+"", + "category": ""+json.products[i].category_name+"", "price" : prod_price.trim(), "qty" : quantity }; diff --git a/modules/expresscart/ajax.php b/modules/expresscart/ajax.php index 8a8a2da2..6812802a 100755 --- a/modules/expresscart/ajax.php +++ b/modules/expresscart/ajax.php @@ -146,18 +146,30 @@ if($id_product = (int) Tools::getValue('id_product')) { $combinations[$id_product_attribute]['list'] = $attributeList; } + $smarty->assign(array( 'groups' => $groups, 'combinaisons' => $combinations, /* Kept for compatibility purpose only */ 'combinations' => $combinations, 'colors' => (sizeof($colors) && $product->id_color_default)? $colors: FALSE, - 'combinationImages' => $combinationImages, + 'combinationImages' => $combinationImages )); } + $category_name = $category_name = Db::getInstance()->getRow(' + SELECT cl.`name` + FROM `'._DB_PREFIX_.'product_ps_cache` ps + LEFT JOIN `'._DB_PREFIX_.'privatesale` s ON (s.`id_sale` = ps.`id_sale`) + LEFT JOIN `'._DB_PREFIX_.'category` c ON (c.`id_category` = s.`id_category`) + LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON (cl.`id_category` = c.`id_category`) + WHERE ps.`id_product` = '.(int)$id_product.' + AND cl.`id_lang` = '.(int)$cookie->id_lang + ); + $smarty->assign(array( 'cartex' => Module::isInstalled('blockcartex'), 'product' => $product, + 'category_name' => $category_name['name'], 'quantity_discounts' => formatQuantityDiscounts(SpecificPrice::getQuantityDiscounts((int) $product->id, (int) Shop::getCurrentShop(), (int) $cookie->id_currency, $id_country, $id_group), $product->getPrice(Product::$_taxCalculationMethod == PS_TAX_INC, FALSE), (float) $tax), 'ecotax_tax_inc' => $ecotaxTaxAmount, 'tax_rate' => $tax, diff --git a/modules/expresscart/product_footer.tpl b/modules/expresscart/product_footer.tpl index 8de2a6a6..990f13f7 100644 --- a/modules/expresscart/product_footer.tpl +++ b/modules/expresscart/product_footer.tpl @@ -488,6 +488,7 @@ +

{$product->name}

@@ -630,6 +631,8 @@ "id" : idProduct, "variant" : idAttribute, "name" : ""+$('.product_footer_title').text()+"", + "brand" : ""+$('#category_name').val()+"", + "category" : ""+$('#category_name').val()+"", "price" : prod_price.trim(), "qty" : quantity }; diff --git a/modules/ganalytics/ganalytics.php b/modules/ganalytics/ganalytics.php index a4e9ae19..3a8292d2 100755 --- a/modules/ganalytics/ganalytics.php +++ b/modules/ganalytics/ganalytics.php @@ -218,10 +218,20 @@ class GAnalytics extends Module $products = $order->getProducts(); foreach ($products AS $product) { + // $category = Db::getInstance()->getRow(' + // SELECT name FROM `'._DB_PREFIX_.'category_lang` , '._DB_PREFIX_.'product + // WHERE `id_product` = '.intval($product['product_id']).' AND `id_category_default` = `id_category` + // AND `id_lang` = '.intval($parameters['PS_LANG_DEFAULT'])); + $category = Db::getInstance()->getRow(' - SELECT name FROM `'._DB_PREFIX_.'category_lang` , '._DB_PREFIX_.'product - WHERE `id_product` = '.intval($product['product_id']).' AND `id_category_default` = `id_category` - AND `id_lang` = '.intval($parameters['PS_LANG_DEFAULT'])); + SELECT cl.`name` + FROM `'._DB_PREFIX_.'product_ps_cache` ps + LEFT JOIN `'._DB_PREFIX_.'privatesale` s ON (s.`id_sale` = ps.`id_sale`) + LEFT JOIN `'._DB_PREFIX_.'category` c ON (c.`id_category` = s.`id_category`) + LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON (cl.`id_category` = c.`id_category`) + WHERE ps.`id_product` = '.intval($product['product_id']).' + AND cl.`id_lang` = '.intval($parameters['PS_LANG_DEFAULT']) + ); $items[] = array( 'OrderId' => intval($order->id), // order ID - required diff --git a/modules/ganalytics/header.tpl b/modules/ganalytics/header.tpl index cd3dfe34..a0922141 100755 --- a/modules/ganalytics/header.tpl +++ b/modules/ganalytics/header.tpl @@ -32,6 +32,7 @@ 'id': '{$item.SKU}', // Product ID (string). 'name': '{$item.Product}', // Product name (string). 'category': '{$item.Category}', // Product category (string). + 'brand': '{$item.Category}', // Product brand (string). 'price': '{$item.Price}', // Product price (currency). 'quantity': {$item.Quantity} // Product quantity (number). }); diff --git a/override/classes/Cart.php b/override/classes/Cart.php index ec099fab..a699907c 100755 --- a/override/classes/Cart.php +++ b/override/classes/Cart.php @@ -125,6 +125,17 @@ class Cart extends CartCore { if (array_key_exists($row['id_product_attribute'].'-'.$this->id_lang, self::$_attributesLists)) $row = array_merge($row, self::$_attributesLists[$row['id_product_attribute'].'-'.$this->id_lang]); + $category_name = Db::getInstance()->getRow(' + SELECT cl.`name` + FROM `'._DB_PREFIX_.'product_ps_cache` ps + LEFT JOIN `'._DB_PREFIX_.'privatesale` s ON (s.`id_sale` = ps.`id_sale`) + LEFT JOIN `'._DB_PREFIX_.'category` c ON (c.`id_category` = s.`id_category`) + LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON (cl.`id_category` = c.`id_category`) + WHERE ps.`id_product` = '.(int)$row['id_product'].' + AND cl.`id_lang` = '.(int)$this->id_lang + ); + $row['category_name'] = $category_name['name']; + $this->_products[] = $row; } diff --git a/themes/site/header.tpl b/themes/site/header.tpl index 94131e06..1ee03659 100755 --- a/themes/site/header.tpl +++ b/themes/site/header.tpl @@ -199,6 +199,8 @@ ga('ec:addProduct', { 'id': product.id, 'name': product.name, + 'brand': product.category, + 'category': product.category, 'variant' : product.variant, 'price': product.price, 'quantity': product.qty @@ -206,6 +208,19 @@ ga('ec:setAction', 'add'); ga('send', 'event', 'UX', 'click', 'add to cart'); } + function removeFromCartToGA(product) { + ga('ec:addProduct', { + 'id': product.id, + 'name': product.name, + 'brand': product.category, + 'category': product.category, + 'variant' : product.variant, + 'price': product.price, + 'quantity': product.qty + }); + ga('ec:setAction', 'remove'); + ga('send', 'event', 'UX', 'click', 'remove to cart'); + } {/literal} {elseif $cookie->id_lang == 3} @@ -319,6 +334,8 @@ ga('ec:addProduct', { 'id': product.id, 'name': product.name, + 'brand': product.category, + 'category': product.category, 'variant' : product.variant, 'price': product.price, 'quantity': product.qty @@ -326,6 +343,19 @@ ga('ec:setAction', 'add'); ga('send', 'event', 'UX', 'click', 'add to cart'); } + function removeFromCartToGA(product) { + ga('ec:addProduct', { + 'id': product.id, + 'name': product.name, + 'brand': product.category, + 'category': product.category, + 'variant' : product.variant, + 'price': product.price, + 'quantity': product.qty + }); + ga('ec:setAction', 'remove'); + ga('send', 'event', 'UX', 'click', 'remove to cart'); + } {/literal} {/if} diff --git a/themes/site/js/cart-summary.js b/themes/site/js/cart-summary.js index 6a1a5490..0cb806a2 100755 --- a/themes/site/js/cart-summary.js +++ b/themes/site/js/cart-summary.js @@ -68,6 +68,27 @@ function deletProductFromSummary(id) productAttributeId = parseInt(ids[1]); if (typeof(ids[2]) != 'undefined') customizationId = parseInt(ids[2]); + + // send to GA + var product_name = $('#product_'+id+' .cart_description a.cart_product_name').text(); + var qty = $('#product_'+id+' .cart_quantity input[name="quantity_'+id+'_hidden"]').val(); + var category_name = $('#product_'+id).attr('category_name'); + if(category_name == null || category_name == "" || category_name == undefined) { + category_name = ""; + } + var prod_price = $('#product_price_'+id).text(); + prod_price = prod_price.replace(",","."); + prod_price = prod_price.replace("€",""); + var productToGA = { + "id" : productId, + "variant" : productAttributeId, + "name" : ""+product_name.trim()+"", + "brand": ""+category_name.trim()+"", + "category": ""+category_name.trim()+"", + "price" : prod_price.trim(), + "qty" : qty + }; + $.ajax({ type: 'GET', url: baseDir + 'cart.php', @@ -125,6 +146,10 @@ function deletProductFromSummary(id) if (jsonData.carriers != null) updateCarrierList(jsonData); + if (window.ga && ga.create){ + removeFromCartToGA(productToGA); + } + } }, error: function(XMLHttpRequest, textStatus, errorThrown) {alert("TECHNICAL ERROR: unable to save update quantity \n\nDetails:\nError thrown: " + XMLHttpRequest + "\n" + 'Text status: ' + textStatus);} @@ -156,6 +181,26 @@ function upQuantity(id, qty) productAttributeId = parseInt(ids[1]); if (typeof(ids[2]) != 'undefined') customizationId = parseInt(ids[2]); + + // send to GA + var product_name = $('#product_'+id+' .cart_description a.cart_product_name').text(); + var category_name = $('#product_'+id).attr('category_name'); + if(category_name == null || category_name == "" || category_name == undefined) { + category_name = ""; + } + var prod_price = $('#product_price_'+id).text(); + prod_price = prod_price.replace(",","."); + prod_price = prod_price.replace("€",""); + var productToGA = { + "id" : productId, + "variant" : productAttributeId, + "name" : ""+product_name.trim()+"", + "brand": ""+category_name.trim()+"", + "category": ""+category_name.trim()+"", + "price" : prod_price.trim(), + "qty" : qty + }; + $.ajax({ type: 'GET', url: baseDir + 'cart.php', @@ -183,6 +228,11 @@ function upQuantity(id, qty) updateHookShoppingCartExtra(jsonData.HOOK_SHOPPING_CART_EXTRA); if (jsonData.carriers != null) updateCarrierList(jsonData); + + // send add product to cart information to GA + if (window.ga && ga.create){ + addToCartToGA(productToGA); + } } }, error: function(XMLHttpRequest, textStatus, errorThrown) {alert("TECHNICAL ERROR: unable to save update quantity \n\nDetails:\nError thrown: " + XMLHttpRequest + "\n" + 'Text status: ' + textStatus);} @@ -212,6 +262,25 @@ function downQuantity(id, qty) productAttributeId = parseInt(ids[1]); if (typeof(ids[2]) != 'undefined') customizationId = parseInt(ids[2]); + + // send to GA + var product_name = $('#product_'+id+' .cart_description a.cart_product_name').text(); + var category_name = $('#product_'+id).attr('category_name'); + if(category_name == null || category_name == "" || category_name == undefined) { + category_name = ""; + } + var prod_price = $('#product_price_'+id).text(); + prod_price = prod_price.replace(",","."); + prod_price = prod_price.replace("€",""); + var productToGA = { + "id" : productId, + "variant" : productAttributeId, + "name" : ""+product_name.trim()+"", + "brand": ""+category_name.trim()+"", + "category": ""+category_name.trim()+"", + "price" : prod_price.trim(), + "qty" : qty + }; $.ajax({ type: 'GET', url: baseDir + 'cart.php', @@ -239,6 +308,10 @@ function downQuantity(id, qty) updateHookShoppingCartExtra(jsonData.HOOK_SHOPPING_CART_EXTRA); if (jsonData.carriers != null) updateCarrierList(jsonData); + // send add product to cart information to GA + if (window.ga && ga.create){ + removeFromCartToGA(productToGA); + } } }, error: function(XMLHttpRequest, textStatus, errorThrown) {alert("TECHNICAL ERROR: unable to save update quantity \n\nDetails:\nError thrown: " + XMLHttpRequest + "\n" + 'Text status: ' + textStatus);} diff --git a/themes/site/modules/blockcartex/blockcart-json.tpl b/themes/site/modules/blockcartex/blockcart-json.tpl index 7e1f5b88..ed159c90 100755 --- a/themes/site/modules/blockcartex/blockcart-json.tpl +++ b/themes/site/modules/blockcartex/blockcart-json.tpl @@ -36,6 +36,7 @@ "quantity": {$product.cart_quantity}, "priceByLine": "{if $priceDisplay == $smarty.const.PS_TAX_EXC}{displayWtPrice|html_entity_decode:2:'UTF-8' p=$product.total}{else}{displayWtPrice|html_entity_decode:2:'UTF-8' p=$product.total_wt}{/if}", "name": "{$product.name|html_entity_decode:2:'UTF-8'|escape|truncate:16:'...':true}", + "category_name": "{$product.category_name|html_entity_decode:2:'UTF-8'|escape|truncate:16:'...':true}", "price": "{if $priceDisplay == $smarty.const.PS_TAX_EXC}{displayWtPrice|html_entity_decode:2:'UTF-8' p=$product.total}{else}{displayWtPrice|html_entity_decode:2:'UTF-8' p=$product.total_wt}{/if}", "idCombination": {if isset($product.attributes_small)}{$productAttributeId}{else}0{/if}, {if isset($product.attributes_small)} diff --git a/themes/site/shopping-cart-product-line.tpl b/themes/site/shopping-cart-product-line.tpl index eb32d814..9dfa1e17 100755 --- a/themes/site/shopping-cart-product-line.tpl +++ b/themes/site/shopping-cart-product-line.tpl @@ -24,12 +24,12 @@ * International Registered Trademark & Property of PrestaShop SA *} - + {$product.name|escape:'htmlall':'UTF-8'} -
{$product.name|escape:'htmlall':'UTF-8'}
+
{$product.name|escape:'htmlall':'UTF-8'}
{if isset($product.attributes) && $product.attributes}{$product.attributes|escape:'htmlall':'UTF-8'}{/if} {*{if $product.reference}{$product.reference|escape:'htmlall':'UTF-8'}{else}--{/if}*} diff --git a/themes/site/shopping-cart.tpl b/themes/site/shopping-cart.tpl index 68f16d9c..c42861b4 100755 --- a/themes/site/shopping-cart.tpl +++ b/themes/site/shopping-cart.tpl @@ -250,7 +250,7 @@ {* Then the customized datas ones*} {if isset($customizedDatas.$productId.$productAttributeId)} {foreach from=$customizedDatas.$productId.$productAttributeId key='id_customization' item='customization'} - + {foreach from=$customization.datas key='type' item='datas'} {if $type == $CUSTOMIZE_FILE} @@ -349,7 +349,7 @@ {/if} -{if $economy > 0} +{if $economy > 0}

{l s='savings'} {displayPrice price=$economy} ({displayPrice price=$total_products_wt} {l s='instead of'} {displayPrice price=$strike_price})

{/if}

{l s='Shipping calculated its step 3'}

@@ -424,7 +424,7 @@ {/if}*} -