gtm4wp_woocommerce_add_prod_data

The Google Tag Manager for WordPress gtm4wp woocommerce add prod data function.

Description

gtm4wp_woocommerce_add_prod_data( $add_to_cart_link ); 

Parameters (1)

0. $add_to_cart_link
The add to cart link.

Usage

  1. if ( !function_exists( 'gtm4wp_woocommerce_add_prod_data' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'google-tag-manager-for-wordpress/integration/woocommerce.php'; 
  3.  
  4. // The add to cart link. 
  5. $add_to_cart_link = null; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = gtm4wp_woocommerce_add_prod_data($add_to_cart_link); 
  9.  

Defined (1)

The function is defined in the following location(s).

/integration/woocommerce.php  
  1. function gtm4wp_woocommerce_add_prod_data( $add_to_cart_link ) { 
  2. global $product, $woocommerce_loop, $wp_query, $gtm4wp_options; 
  3.  
  4. $product_id = $product->get_id(); 
  5. $_product_cats = get_the_terms($product->id, 'product_cat'); 
  6. if ( ( is_array($_product_cats) ) && ( count( $_product_cats ) > 0 ) ) { 
  7. $product_cat = array_pop( $_product_cats ); 
  8. $product_cat = $product_cat->name; 
  9. } else { 
  10. $product_cat = ""; 
  11.  
  12. if ( is_search() ) { 
  13. $list_name = __( "Search Results", "duracelltomi-google-tag-manager" ); 
  14. } else if ( isset( $woocommerce_loop[ "listtype" ] ) && ( $woocommerce_loop[ "listtype" ] != '' ) ) { 
  15. $list_name = $woocommerce_loop[ "listtype" ]; 
  16. } else { 
  17. $list_name = __( "General Product List", "duracelltomi-google-tag-manager" ); 
  18.  
  19. $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; 
  20. $posts_per_page = get_query_var('posts_per_page'); 
  21. if ( $posts_per_page < 1 ) { 
  22. $posts_per_page = 1; 
  23.  
  24. $remarketing_id = $product_id; 
  25. $product_sku = $product->get_sku(); 
  26. if ( $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCUSESKU ] && ( "" != $product_sku ) ) { 
  27. $remarketing_id = $product_sku; 
  28.  
  29. $_temp_productdata = array( 
  30. "id" => $remarketing_id,  
  31. "name" => $product->post->post_title,  
  32. "category" => $product_cat,  
  33. "price" => $product->get_price(),  
  34. "productlink" => apply_filters( 'the_permalink', get_permalink(), 0),  
  35. "listposition" => $woocommerce_loop[ "loop" ] + ( $posts_per_page * ($paged-1) ),  
  36. "listname" => $list_name 
  37. ); 
  38.  
  39. if ( "variation" == $product->product_type ) { 
  40. $_temp_productdata[ "variant" ] = implode(", ", $product->get_variation_attributes()); 
  41. } else { 
  42. $_temp_productdata[ "variant" ] = ""; 
  43.  
  44. $eec_product_array = apply_filters( GTM4WP_WPFILTER_EEC_PRODUCT_ARRAY, $_temp_productdata, "addtocartproductlist" ); 
  45.  
  46. $cartlink_with_data = sprintf('data-gtm4wp_product_id="%s" data-gtm4wp_product_name="%s" data-gtm4wp_product_price="%s" data-gtm4wp_product_cat="%s" data-gtm4wp_product_url="%s" data-gtm4wp_product_listposition="%s" data-gtm4wp_productlist_name="%s" data-gtm4wp_product_variant="%s" href="',  
  47. esc_attr( $eec_product_array[ "id" ] ),  
  48. esc_attr( $eec_product_array[ "name" ] ),  
  49. esc_attr( $eec_product_array[ "price" ] ),  
  50. esc_attr( $eec_product_array[ "category" ] ),  
  51. esc_url( $eec_product_array[ "productlink" ] ),  
  52. esc_attr( $eec_product_array[ "listposition" ] ),  
  53. esc_attr( $eec_product_array[ "listname" ] ),  
  54. esc_attr( $eec_product_array[ "variant" ] ) 
  55. ); 
  56.  
  57. return str_replace( 'href="', $cartlink_with_data, $add_to_cart_link );