gtm4wp_woocommerce_before_shop_loop_item

The Google Tag Manager for WordPress gtm4wp woocommerce before shop loop item function.

Description

gtm4wp_woocommerce_before_shop_loop_item(); 

Usage

  1. if ( !function_exists( 'gtm4wp_woocommerce_before_shop_loop_item' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'google-tag-manager-for-wordpress/integration/woocommerce.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = gtm4wp_woocommerce_before_shop_loop_item(); 
  7.  

Defined (1)

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

/integration/woocommerce.php  
  1. function gtm4wp_woocommerce_before_shop_loop_item() { 
  2. global $product, $woocommerce_loop, $wp_query, $gtm4wp_options; 
  3.  
  4. if ( !isset( $product ) ) { 
  5. return; 
  6.  
  7. $product_id = $product->get_id(); 
  8. $product_cat = ""; 
  9. if ( is_product_category() ) { 
  10. global $wp_query; 
  11. $cat_obj = $wp_query->get_queried_object(); 
  12. $product_cat = $cat_obj->name; 
  13. } else { 
  14. $_product_cats = get_the_terms($product->id, 'product_cat'); 
  15. if ( ( is_array($_product_cats) ) && ( count( $_product_cats ) > 0 ) ) { 
  16. $last_product_cat = array_pop( $_product_cats ); 
  17. $product_cat = $last_product_cat->name; 
  18.  
  19. if ( is_search() ) { 
  20. $list_name = __( "Search Results", "duracelltomi-google-tag-manager" ); 
  21. } else if ( isset( $woocommerce_loop[ "listtype" ] ) && ( $woocommerce_loop[ "listtype" ] != '' ) ) { 
  22. $list_name = $woocommerce_loop[ "listtype" ]; 
  23. } else { 
  24. $list_name = __( "General Product List", "duracelltomi-google-tag-manager" ); 
  25.  
  26. $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; 
  27. $posts_per_page = get_query_var('posts_per_page'); 
  28. if ( $posts_per_page < 1 ) { 
  29. $posts_per_page = 1; 
  30.  
  31. $remarketing_id = $product_id; 
  32. $product_sku = $product->get_sku(); 
  33. if ( $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCUSESKU ] && ( "" != $product_sku ) ) { 
  34. $remarketing_id = $product_sku; 
  35.  
  36. $_temp_productdata = array( 
  37. "id" => $remarketing_id,  
  38. "name" => $product->post->post_title,  
  39. "price" => $product->get_price(),  
  40. "category" => $product_cat,  
  41. "productlink" => apply_filters( 'the_permalink', get_permalink(), 0),  
  42. "listname" => $list_name,  
  43. "listposition" => $woocommerce_loop[ "loop" ] + ( $posts_per_page * ($paged-1) ) 
  44. ); 
  45. $eec_product_array = apply_filters( GTM4WP_WPFILTER_EEC_PRODUCT_ARRAY, $_temp_productdata, "productlist" ); 
  46.  
  47. printf('<span class="gtm4wp_productdata" style="display:none; visibility:hidden;" 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"></span>',  
  48. esc_attr( $eec_product_array[ "id" ] ),  
  49. esc_attr( $eec_product_array[ "name" ] ),  
  50. esc_attr( $eec_product_array[ "price" ] ),  
  51. esc_attr( $eec_product_array[ "category" ] ),  
  52. esc_url( $eec_product_array[ "productlink" ] ),  
  53. esc_attr( $eec_product_array[ "listposition" ] ),  
  54. esc_attr( $eec_product_array[ "listname" ] ) 
  55. );