From 81aab10c4ef1526f81dcac59edb53c4800fe32e3 Mon Sep 17 00:00:00 2001 From: MaxBUhe Date: Tue, 7 Nov 2023 10:59:39 +0100 Subject: [PATCH] [Changed] process-list of add_to_cart; [Added] BasketComponent extensions to controll the trigger-behaviour of add_to_cart, ArticleDetailsController extension for add_to_Cart i view_item add_to_cart no longer is a block extension, changed to an indiv tpl-file --- .../blocks/details_productmain_tobasket.tpl | 3 + .../views/blocks/page_list_listbody.tpl | 3 + Application/views/ga4/add_to_cart.tpl | 81 ++++++++++--------- Application/views/ga4/add_to_cart_listtpl.tpl | 57 ------------- IntelliSenseHelper.php | 2 + .../Controller/ArticleDetailsController.php | 15 ++++ metadata.php | 13 ++- 7 files changed, 77 insertions(+), 97 deletions(-) create mode 100644 Application/views/blocks/details_productmain_tobasket.tpl create mode 100644 Application/views/blocks/page_list_listbody.tpl delete mode 100644 Application/views/ga4/add_to_cart_listtpl.tpl create mode 100644 Modules/Application/Controller/ArticleDetailsController.php diff --git a/Application/views/blocks/details_productmain_tobasket.tpl b/Application/views/blocks/details_productmain_tobasket.tpl new file mode 100644 index 0000000..6f3c741 --- /dev/null +++ b/Application/views/blocks/details_productmain_tobasket.tpl @@ -0,0 +1,3 @@ +[{$smarty.block.parent}] + +[{include file="addtocart.tpl" htmlIdAmountOfArticles='#amountToBasket'}] \ No newline at end of file diff --git a/Application/views/blocks/page_list_listbody.tpl b/Application/views/blocks/page_list_listbody.tpl new file mode 100644 index 0000000..6f3c741 --- /dev/null +++ b/Application/views/blocks/page_list_listbody.tpl @@ -0,0 +1,3 @@ +[{$smarty.block.parent}] + +[{include file="addtocart.tpl" htmlIdAmountOfArticles='#amountToBasket'}] \ No newline at end of file diff --git a/Application/views/ga4/add_to_cart.tpl b/Application/views/ga4/add_to_cart.tpl index 8a2b68e..8dfc2e5 100644 --- a/Application/views/ga4/add_to_cart.tpl +++ b/Application/views/ga4/add_to_cart.tpl @@ -1,36 +1,45 @@ -[{$smarty.block.parent}] -[{* variable $gtmProduct is passed from parent tempalte *}] -[{assign var="d3PriceObject" value=$gtmProduct->getPrice()}] -[{assign var="gtmCurrency" value=$oView->getActCurrency()}] -[{assign var="gtmManufacturer" value=$gtmProduct->getManufacturer()}] -[{assign var="gtmCategory" value=$gtmProduct->getCategory()}] +[{if $d3CmpBasket->getAddToBasketDecision() && $d3CmpBasket->d3GtmRequestedArticleLoadedByAnid() !== null}] + [{assign var="oGtmProduct" value=$d3CmpBasket->d3GtmRequestedArticleLoadedByAnid()}] + [{assign var="oGtmAmountArticlesAdded" value=$d3CmpBasket->getD3GtmAddToCartAmountArticles()}] + [{*$smarty.block.parent*}] + [{* variable $oGtmProduct is passed from parent tempalte *}] + [{assign var="d3PriceObject" value=$oGtmProduct->getPrice()}] + [{assign var="gtmCurrency" value=$oView->getActCurrency()}] + [{assign var="gtmManufacturer" value=$oGtmProduct->getManufacturer()}] + [{assign var="gtmCategory" value=$oGtmProduct->getCategory()}] -[{block name="d3_ga4_add_to_cart_block"}] - [{capture assign=d3_ga4_add_to_cart}] - [{strip}] - $("#toBasket").click(function(event) { - dataLayer.push({"event": null, "eventLabel": null, "ecommerce": null}); /* Clear the previous ecommerce object. */ + [{block name="d3_ga4_add_to_cart_list_block"}] + [{capture name="d3_ga4_add_to_cart_listtpl"}] + [{strip}] + dataLayer.push({"event": null, "eventLabel": null, "ecommerce": null}); /* Clear the previous ecommerce object. */ - [{*** Debug cases ***}] - [{*event.preventDefault();*}] + [{*** Debug cases ***}] + [{*event.preventDefault();*}] - let iArtQuantity = $("#amountToBasket").val(); + let iArtQuantity = $("[{$htmlIdAmountOfArticles}]").val(); + let iArtQuantityAdded = [{$oGtmAmountArticlesAdded}]; - dataLayer.push({ - 'isAddToBasket': true, - 'event':'add_to_cart', - 'eventLabel': 'add_to_cart', - 'ecommerce': { - 'currency': "[{$currency->name}]", - 'value': iArtQuantity*[{$d3PriceObject->getPrice()}], - 'items': [ - { - 'item_id': '[{$gtmProduct->getFieldData('oxartnum')}]', - 'item_name': '[{$gtmProduct->getFieldData('oxtitle')}]', + if(!iArtQuantity && (iArtQuantityAdded === 1)){ + iArtQuantity = 1; + }else{ + iArtQuantity = iArtQuantityAdded; + } + + dataLayer.push({ + 'isAddToBasket': true, + 'event':'add_to_cart', + 'eventLabel': 'add_to_cart', + 'ecommerce': { + 'currency': "[{$currency->name}]", + 'value': iArtQuantity*[{$d3PriceObject->getPrice()}], + 'items': [ + { + 'item_id': '[{$oGtmProduct->getFieldData('oxartnum')}]', + 'item_name': '[{$oGtmProduct->getFieldData('oxtitle')}]', 'price': [{$d3PriceObject->getPrice()}], 'item_brand': '[{if $gtmManufacturer}][{$gtmManufacturer->oxmanufacturers__oxtitle->value}][{/if}]', - 'item_variant': '[{if $gtmProduct->getFieldData('oxvarselect')}][{$gtmProduct->getFieldData('oxvarselect')}][{/if}]', + 'item_variant': '[{if $oGtmProduct->getFieldData('oxvarselect')}][{$oGtmProduct->getFieldData('oxvarselect')}][{/if}]', [{if $gtmCategory}] 'item_category': '[{$gtmCategory->getSplitCategoryArray(0)}]', 'item_category_2':'[{$gtmCategory->getSplitCategoryArray(1)}]', @@ -39,14 +48,14 @@ 'item_list_name':'[{$gtmCategory->getSplitCategoryArray()}]', [{/if}] 'quantity': iArtQuantity - } - ] - }[{if $oViewConf->isDebugModeOn()}], - 'debug_mode': 'true' - [{/if}] - }); + } + ] + }[{if $oViewConf->isDebugModeOn()}], + 'debug_mode': 'true' + [{/if}] }); - [{/strip}] - [{/capture}] - [{oxscript add=$d3_ga4_add_to_cart}] -[{/block}] \ No newline at end of file + [{/strip}] + [{/capture}] + [{oxscript add=$smarty.capture.d3_ga4_add_to_cart_listtpl}] + [{/block}] +[{/if}] \ No newline at end of file diff --git a/Application/views/ga4/add_to_cart_listtpl.tpl b/Application/views/ga4/add_to_cart_listtpl.tpl deleted file mode 100644 index 0210a86..0000000 --- a/Application/views/ga4/add_to_cart_listtpl.tpl +++ /dev/null @@ -1,57 +0,0 @@ -[{$smarty.block.parent}] - -[{if $oView->getAddToBasketDecision() && $oView->d3GtmRequestedArticleLoadedByAnid() !== null}] - [{assign var="oGtmProduct" value=$oView->d3GtmRequestedArticleLoadedByAnid()}] - [{*$smarty.block.parent*}] - [{* variable $oGtmProduct is passed from parent tempalte *}] - [{assign var="d3PriceObject" value=$oGtmProduct->getPrice()}] - [{assign var="gtmCurrency" value=$oView->getActCurrency()}] - [{assign var="gtmManufacturer" value=$oGtmProduct->getManufacturer()}] - [{assign var="gtmCategory" value=$oGtmProduct->getCategory()}] - - [{block name="d3_ga4_add_to_cart_list_block"}] - [{capture name="d3_ga4_add_to_cart_listtpl"}] - [{strip}] - dataLayer.push({"event": null, "eventLabel": null, "ecommerce": null}); /* Clear the previous ecommerce object. */ - - [{*** Debug cases ***}] - [{*event.preventDefault();*}] - - let iArtQuantity = $("#amountToBasket").val(); - if(!iArtQuantity){ - iArtQuantity = 1; - } - - dataLayer.push({ - 'isAddToBasket': true, - 'event':'add_to_cart', - 'eventLabel': 'add_to_cart', - 'ecommerce': { - 'currency': "[{$currency->name}]", - 'value': iArtQuantity*[{$d3PriceObject->getPrice()}], - 'items': [ - { - 'item_id': '[{$oGtmProduct->getFieldData('oxartnum')}]', - 'item_name': '[{$oGtmProduct->getFieldData('oxtitle')}]', - 'price': [{$d3PriceObject->getPrice()}], - 'item_brand': '[{if $gtmManufacturer}][{$gtmManufacturer->oxmanufacturers__oxtitle->value}][{/if}]', - 'item_variant': '[{if $oGtmProduct->getFieldData('oxvarselect')}][{$oGtmProduct->getFieldData('oxvarselect')}][{/if}]', - [{if $gtmCategory}] - 'item_category': '[{$gtmCategory->getSplitCategoryArray(0)}]', - 'item_category_2':'[{$gtmCategory->getSplitCategoryArray(1)}]', - 'item_category_3':'[{$gtmCategory->getSplitCategoryArray(2)}]', - 'item_category_4':'[{$gtmCategory->getSplitCategoryArray(3)}]', - 'item_list_name':'[{$gtmCategory->getSplitCategoryArray()}]', - [{/if}] - 'quantity': iArtQuantity - } - ] - }[{if $oViewConf->isDebugModeOn()}], - 'debug_mode': 'true' - [{/if}] - }); - [{/strip}] - [{/capture}] - [{oxscript add=$smarty.capture.d3_ga4_add_to_cart_listtpl}] - [{/block}] -[{/if}] \ No newline at end of file diff --git a/IntelliSenseHelper.php b/IntelliSenseHelper.php index cdc661c..a7192ca 100644 --- a/IntelliSenseHelper.php +++ b/IntelliSenseHelper.php @@ -30,6 +30,8 @@ namespace D3\GoogleAnalytics4\Modules\Application\Controller{ class ThankYouController_parent extends \OxidEsales\Eshop\Application\Controller\ThankYouController {} class ArticleListController_AddToCartHelpMethods_parent extends ArticleListController {} + + class ArticleDetailsController_parent extends \OxidEsales\Eshop\Application\Controller\ArticleDetailsController {} } namespace D3\GoogleAnalytics4\Modules\Application\Component{ diff --git a/Modules/Application/Controller/ArticleDetailsController.php b/Modules/Application/Controller/ArticleDetailsController.php new file mode 100644 index 0000000..a2aee20 --- /dev/null +++ b/Modules/Application/Controller/ArticleDetailsController.php @@ -0,0 +1,15 @@ +addTplParam('d3CmpBasket', $this->getComponent('oxcmp_basket')); + + return $return; + } +} \ No newline at end of file diff --git a/metadata.php b/metadata.php index 0072bb0..f42d279 100755 --- a/metadata.php +++ b/metadata.php @@ -1,6 +1,7 @@ Manufacturer::class, OEThankYouController::class => ThankYouController::class, OEArticleListController::class => ArticleListController_AddToCartHelpMethods::class, - OEBasketComponent::class => d3GtmBasketComponentExtension::class + OEBasketComponent::class => d3GtmBasketComponentExtension::class, + OEArticleDetailsController::class => ArticleDetailsController::class + ], + 'templates' => [ + 'addtocart.tpl' => 'd3/googleanalytics4/Application/views/ga4/add_to_cart.tpl' ], - 'templates' => [], 'blocks' => [ // tag manager js [ @@ -98,13 +103,13 @@ $aModule = [ [ 'template' => 'page/details/inc/productmain.tpl', 'block' => 'details_productmain_tobasket', - 'file' => '/Application/views/ga4/add_to_cart.tpl', + 'file' => '/Application/views/blocks/details_productmain_tobasket.tpl', 'position' => 150 ], [ 'template' => 'page/list/list.tpl', 'block' => 'page_list_listbody', - 'file' => '/Application/views/ga4/add_to_cart_listtpl.tpl', + 'file' => '/Application/views/blocks/page_list_listbody.tpl', 'position' => 150 ], // remove_from_cart