• Resolved saens

    (@saens)


    Hi,
    I’ve added a new add to cart button for mobile users. It uses Woocommerce add-to-cart URL. How can I have these clicks trigger gtm4wp.addProductToCartEEC as well?

    <form id="in_winkelmand_knop" method="post" enctype='multipart/form-data' action="/winkelmand/?add-to-cart=<?php
    						global $product;
    						echo $product->get_id();
    						?>"> 
    						<button class="winkelmand_knop_float" type="submit">In mijn mandje</button>
    				</form>
Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Thomas Geiger

    (@duracelltomi)

    Hi,

    This is currently not easy to achieve since GTM4WP needs many additional codes in the form.

    First, you need to call the do_action() function with the woocommerce_after_add_to_cart_button action name. Before that, please make sure that the $product variable is initiated with the proper WC_Product instance.

    This should be added inside the form so that GTM4WP can add some more hidden input fields to the form.

    You should also make sure that the form node and the add to cart button includes the same HTML classes as the single add to cart button on product detail pages.

    I can not guarantee that this will work but without this, it will not work for sure.

    Thread Starter saens

    (@saens)

    I agree, this is rather complex for a non-webdev such as myself.
    Two questions:

    1) is adding this functionality on your roadmap for this plugin?
    2) maybe you could create a guide or how-to? Maybe custom tags in tagmanager can fix this somehow? It’s not that it has to be a gtm4wp.addProductToCartEEC-event, it can be a different addToCart-event , as long as it’s registered properly.

    Thanks for your work!

    Plugin Author Thomas Geiger

    (@duracelltomi)

    Hi,

    1) not on the short term roadmap, it could be implemented on the longer term
    2) the main problem is that data needed for enhanced ecommerce tracking is not present in the source code of the website by default. The code that I proposed above tries to bring all necessary data to the “surface” so that you can use it somehow. Just by using custom HTML tags in your GTM container, it is either very hard or impossible to have the same reporting

    Thread Starter saens

    (@saens)

    Hi, thanks a lot for indulging me.

    And to keep it simple, just send an addToCart-event with only the absolute basic data? The main issue I’m trying to solve is to let Google Analytics/Ads know what a click on an ad did. It’s less relevant for me to know what particular product/price/SKU/etc.

    Either way, I’m very happy with your plugin ??

    Thread Starter saens

    (@saens)

    FYI, I managed to get it to work. Custom tag+trigger and custom code in the button I created.

    Still, an integrated solution is most likely best ??

    Hi @saens, I got the same issue and couldn’t solve it .

    Would you please share a brief example of your solution?

    I appreciate it. Thank you!

    Thread Starter saens

    (@saens)

    Sure!

    This code is placed in footer.php. It only runs on a certain product category and only shows on mobile devices. (the latter is done with CSS).
    Im not a web-dev, so there’s most likely a better way, but this works for now. Also, this works only on simple products. On variable products you’d need more info, like the variation_id. I’m still working on that.
    Furthermore, it’s most likely best to put the script in a different file, instead of this source. So it’s quite hacky.

    In tagmanager I created a new tag+trigger. Tag should use data-layer, and the trigger (custom event type) triggers on clicks in the button below.

    <?php if  (function_exists( 'is_woocommerce' )) : ?>
    							<?php if (is_product() && strpos(get_the_title(), 'cape')) : ?>
    									<div class="winkelmand_knop_float_container">
    										<form id="in_winkelmand_knop" method="post" enctype='multipart/form-data' action="<?php global $product; echo $product->add_to_cart_url();?>">
    											
    										<script>
    										function functie() {
    											var name = document.getElementById("productName").value;
    											var id = document.getElementById("productId").value;
    											var sku = document.getElementById("productSKU").value;
    											var price = document.getElementById("productPrice").value;
    
    											dataLayer.push({
    											  'event': 'addToCart',
    											  'ecommerce': {
    												'currencyCode': 'EUR',
    												'add': {
    												  'products': [{
    													'name': name,
    													'id': id,
    													'price': price,
    													'sku': sku,
    													'brand': 'our brand',
    													'category': 'Apparel',
    													'quantity': 1
    												   }]
    												}
    											  }
    											});
    										}
    										</script>								
    										<input id="productName" value="<?php global $product; echo $product->get_name();?>" type="hidden" />
    										<input id="productId" value="<?php global $product; echo $product->get_id();?>" type="hidden" />
    										<input id="productSKU" value="<?php global $product; echo $product->get_sku();?>" type="hidden" />
    										<input id="productPrice" value="<?php global $product; echo $product->get_price();?>" type="hidden" />
    										<button class="winkelmand_knop_float" type="submit" OnClick="functie()">In mijn mandje</button>
    										</form>
    									</div>
    							<?php endif; ?>
    						<?php endif; ?>
Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Use custom woocommerce add-to-cart URL’ is closed to new replies.