Changeset 3374198
- Timestamp:
- 10/07/2025 08:56:25 AM (5 months ago)
- Location:
- gtm-server-side
- Files:
-
- 10 edited
- 1 copied
-
tags/2.1.38 (copied) (copied from gtm-server-side/trunk)
-
tags/2.1.38/README.txt (modified) (2 diffs)
-
tags/2.1.38/assets/js/javascript.js (modified) (10 diffs)
-
tags/2.1.38/gtm-server-side.php (modified) (1 diff)
-
tags/2.1.38/includes/class-gtm-server-side-admin-settings.php (modified) (1 diff)
-
tags/2.1.38/includes/class-gtm-server-side-event-addtocart.php (modified) (6 diffs)
-
trunk/README.txt (modified) (2 diffs)
-
trunk/assets/js/javascript.js (modified) (10 diffs)
-
trunk/gtm-server-side.php (modified) (1 diff)
-
trunk/includes/class-gtm-server-side-admin-settings.php (modified) (1 diff)
-
trunk/includes/class-gtm-server-side-event-addtocart.php (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
gtm-server-side/tags/2.1.38/README.txt
r3366307 r3374198 4 4 Requires at least: 5.2.0 5 5 Tested up to: 6.8.0 6 Stable tag: 2.1.3 76 Stable tag: 2.1.38 7 7 License: GPLv2 or later 8 8 License URI: http://www.gnu.org/licenses/gpl-2.0.html … … 68 68 == Changelog == 69 69 70 = 2.1.38 = 71 * Added "select_item" event 72 70 73 = 2.1.37 = 71 74 * Fix: submit button -
gtm-server-side/tags/2.1.38/assets/js/javascript.js
r3174835 r3374198 36 36 } 37 37 38 pluginGtmServerSide.pushAddToCart( 39 pluginGtmServerSide.removePrefixes( el.dataset ) 40 ); 38 let gtmData = pluginGtmServerSide.getGtmItemData( el.dataset ); 39 let customData = pluginGtmServerSide.getCustomItemData( el.dataset ); 40 41 pluginGtmServerSide.pushAddToCart( gtmData ); 42 pluginGtmServerSide.pushSelectItem( gtmData, customData ); 41 43 } 42 44 ); … … 55 57 } 56 58 57 pluginGtmServerSide.pushAddToCart( 58 pluginGtmServerSide.removePrefixes( $el.data() ) 59 ); 59 let gtmData = pluginGtmServerSide.getGtmItemData( $el.data() ); 60 let customData = pluginGtmServerSide.getCustomItemData( $el.data() ); 61 62 pluginGtmServerSide.pushAddToCart( gtmData ); 63 pluginGtmServerSide.pushSelectItem( gtmData, customData ); 60 64 } 61 65 ); … … 99 103 100 104 pluginGtmServerSide.removeFromCart( 101 pluginGtmServerSide. removePrefixes( $thisbutton.data() )105 pluginGtmServerSide.getGtmItemData( $thisbutton.data() ) 102 106 ); 103 107 } … … 118 122 119 123 pluginGtmServerSide.removeFromCart( 120 pluginGtmServerSide. removePrefixes( $el.data() )124 pluginGtmServerSide.getGtmItemData( $el.data() ) 121 125 ); 122 126 } … … 130 134 $elForm.find( '[name^=gtm_]' ) 131 135 ); 132 item = this. removePrefixes( item );136 item = this.getGtmItemData( item ); 133 137 134 138 var $elQty = $elForm.find( '[name=quantity]' ); … … 144 148 $elForm.find( '[name^=gtm_]' ) 145 149 ); 146 item = this. removePrefixes( item );150 item = this.getGtmItemData( item ); 147 151 148 152 var $elQty = $elForm.find( '[name=quantity]' ); … … 243 247 244 248 if ( originalValue < currentValue ) { 245 var item = $this. removePrefixes( elDataset.dataset );249 var item = $this.getGtmItemData( elDataset.dataset ); 246 250 item['quantity'] = currentValue - originalValue; 247 251 … … 254 258 255 259 /** 256 * Re move field prefixes.260 * Return gtm custom data. 257 261 * 258 262 * @param object items List items. 259 263 * @returns object 260 264 */ 261 removePrefixes: function ( items ) { 262 var item = {}; 265 getGtmItemData: function ( items ) { 266 return this._getItemData( items, 'gtm_' ); 267 }, 268 269 /** 270 * Return gtm custom data. 271 * 272 * @param object items List items. 273 * @returns object 274 */ 275 getCustomItemData: function ( items ) { 276 return this._getItemData( items, 'custom_gtm_' ); 277 }, 278 279 /** 280 * Return item data. 281 * 282 * @param object items List items. 283 * @returns object 284 */ 285 _getItemData: function ( items, prefix ) { 286 var result = {}; 263 287 for ( var key in items ) { 264 if ( 0 !== key.indexOf( 'gtm_') ) {288 if ( 0 !== key.indexOf( prefix ) ) { 265 289 continue; 266 290 } 267 291 268 var itemKey = key.replace( 'gtm_', '' )269 item[ itemKey ] = items[key];270 } 271 return item;292 var itemKey = key.replace( prefix, '' ) 293 result[ itemKey ] = items[ key ]; 294 } 295 return result; 272 296 }, 273 297 … … 318 342 */ 319 343 pushAddToCart: function ( item ) { 344 this._pushToDataLayer( item, 'add_to_cart', 'product' ) 345 }, 346 347 /** 348 * Push add_to_cart to dataLayer. 349 * 350 * @param object item List items. 351 * @param object custom Custom data. 352 */ 353 pushSelectItem: function ( item, custom ) { 354 if ( custom?.pagetype ) { // phpcs:ignore WordPress.WhiteSpace.OperatorSpacing.NoSpaceBefore, WordPress.WhiteSpace.OperatorSpacing.NoSpaceAfter 355 this._pushToDataLayer( 356 item, 357 'select_item', 358 custom.pagetype, 359 { 360 collection_id: custom?.collection_id, // phpcs:ignore WordPress.WhiteSpace.OperatorSpacing.NoSpaceBefore, WordPress.WhiteSpace.OperatorSpacing.NoSpaceAfter 361 item_list_name: custom?.item_list_name, // phpcs:ignore WordPress.WhiteSpace.OperatorSpacing.NoSpaceBefore, WordPress.WhiteSpace.OperatorSpacing.NoSpaceAfter 362 } 363 ) 364 } 365 }, 366 367 /** 368 * Push to dataLayer. 369 * 370 * @param object originalItem List items. 371 * @param string event Event name. 372 * @param string pagetype Page type name. 373 * @param object customEcommerce Ecommerce data. 374 */ 375 _pushToDataLayer: function ( originalItem, event, pagetype, customEcommerce = {} ) { 376 item = Object.assign( {}, originalItem ); 320 377 if ( item.item_id ) { 321 378 item = [ item ]; … … 333 390 } 334 391 392 let eventDataEcommerce = { 393 'currency': varGtmServerSide.currency, 394 'value': value.toFixed( 2 ), 395 'items': items, 396 } 397 335 398 var eventData = { 336 'event': this.getDataLayerEventName( 'add_to_cart' ), 337 'ecomm_pagetype': 'product', 338 'ecommerce': { 339 'currency': varGtmServerSide.currency, 340 'value': value.toFixed( 2 ), 341 'items': items, 342 }, 399 'event': this.getDataLayerEventName( event ), 400 'ecomm_pagetype': pagetype, 401 'ecommerce': Object.assign( {}, eventDataEcommerce, customEcommerce ), 343 402 }; 344 403 if ( varGtmServerSide.user_data ) { -
gtm-server-side/tags/2.1.38/gtm-server-side.php
r3366307 r3374198 8 8 * 9 9 * @wordpress-plugin 10 * Plugin Name: GTM Server Side10 * Plugin Name: Stape Conversion Tracking 11 11 * Plugin URI: https://wordpress.org/plugins/gtm-server-side/ 12 12 * Description: Enhance conversion tracking by implementing server-side tagging using server Google Tag Manager container. Effortlessly configure data layer events in web GTM, send webhooks, set up custom loader, and extend cookie lifetime. 13 * Version: 2.1.3 713 * Version: 2.1.38 14 14 * Author: Stape 15 15 * Author URI: https://stape.io -
gtm-server-side/tags/2.1.38/includes/class-gtm-server-side-admin-settings.php
r3359782 r3374198 61 61 public function admin_menu() { 62 62 add_options_page( 63 __( ' GTM Server Side', 'gtm-server-side' ),64 __( ' GTM Server Side', 'gtm-server-side' ),63 __( 'Stape Conversion Tracking', 'gtm-server-side' ), 64 __( 'Stape Conversion Tracking', 'gtm-server-side' ), 65 65 'manage_options', 66 66 GTM_SERVER_SIDE_ADMIN_SLUG, -
gtm-server-side/tags/2.1.38/includes/class-gtm-server-side-event-addtocart.php
r3359782 r3374198 36 36 add_action( 'woocommerce_after_add_to_cart_button', array( $this, 'woocommerce_after_add_to_cart_button' ) ); 37 37 add_filter( 'woocommerce_grouped_product_list_column_quantity', array( $this, 'woocommerce_grouped_product_list_column_quantity' ), 10, 2 ); 38 39 add_filter( 'gtm_server_side_before_html_data_attributes', array( $this, 'format_data_attributes' ) ); 40 add_filter( 'gtm_server_side_after_html_data_attributes', array( $this, 'attach_data_to_event_select_item' ), 20, 3 ); 38 41 } 39 42 … … 52 55 53 56 $data = $this->get_item( $item['data'] ); 54 $data = $this->get_formatted_data_attributes( $data);57 $data = apply_filters( 'gtm_server_side_before_html_data_attributes', $data, 'remove_from_cart', 'woocommerce_cart_item_remove_link' ); 55 58 $data['quantity'] = isset( $item['quantity'] ) ? intval( $item['quantity'] ) : 1; 56 $attrs = $this->convert_product_data_to_html_attrs( $data ); 59 $data = $this->convert_product_data_to_html_attrs( $data ); 60 $attrs = apply_filters( 'gtm_server_side_after_html_data_attributes', $data, 'remove_from_cart', 'woocommerce_cart_item_remove_link' ); 57 61 $link = str_replace( '<a ', '<a ' . join( ' ', $attrs ), $link ); 58 62 … … 78 82 79 83 $data = $this->get_item( $product ); 80 $data = $this->get_formatted_data_attributes( $data);84 $data = apply_filters( 'gtm_server_side_before_html_data_attributes', $data, 'add_to_cart', 'woocommerce_loop_add_to_cart_args' ); 81 85 $data['quantity'] = isset( $args['quantity'] ) ? intval( $args['quantity'] ) : 1; 82 86 $data['index'] = isset( $woocommerce_loop['loop'] ) ? intval( $woocommerce_loop['loop'] ) : 1; 83 $attrs = $this->convert_product_data_key( $data ); 87 $data = $this->convert_product_data_key( $data ); 88 $attrs = apply_filters( 'gtm_server_side_after_html_data_attributes', $data, 'add_to_cart', 'woocommerce_loop_add_to_cart_args' ); 84 89 85 90 if ( ! isset( $args['attributes'] ) || ! is_array( $args['attributes'] ) ) { … … 109 114 110 115 $data = $this->get_item( $product ); 111 $data = $this->get_formatted_data_attributes( $data ); 112 $attrs = $this->convert_product_data_to_html_attrs( $data ); 116 $data = apply_filters( 'gtm_server_side_before_html_data_attributes', $data, 'add_to_cart', 'woocommerce_blocks_product_grid_item_html' ); 117 $data = $this->convert_product_data_to_html_attrs( $data ); 118 $attrs = apply_filters( 'gtm_server_side_after_html_data_attributes', $data, 'add_to_cart', 'woocommerce_blocks_product_grid_item_html' ); 113 119 $html = str_replace( '<li ', '<li ' . join( ' ', $attrs ), $html ); 114 120 … … 160 166 161 167 /** 168 * Formatted data attributes. 169 * 170 * @param array $data Attrs. 171 * @return array 172 */ 173 public function format_data_attributes( $data ) { 174 if ( isset( $data['imageUrl'] ) ) { 175 $data['image-url'] = $data['imageUrl']; 176 unset( $data['imageUrl'] ); 177 } 178 179 return $data; 180 } 181 182 /** 183 * Attach data to event select item. 184 * 185 * @param array $data Attrs. 186 * @param string $type Type. 187 * @param string $caller_hook Caller hook. 188 * @return array 189 */ 190 public function attach_data_to_event_select_item( $data, $type, $caller_hook ) { 191 if ( 'add_to_cart' !== $type ) { 192 return $data; 193 } 194 195 $first_key = false; 196 if ( is_array( $data ) ) { 197 $first_key = array_key_first( $data ); 198 } 199 200 if ( false === $first_key ) { 201 return $data; 202 } 203 204 $pagetype = null; 205 $collection_id = null; 206 $item_list_name = null; 207 208 if ( is_search() ) { 209 $pagetype = 'search'; 210 } elseif ( is_shop() ) { 211 $pagetype = 'shop'; 212 } elseif ( is_product_category() ) { 213 $pagetype = 'category'; 214 } elseif ( is_product_tag() ) { 215 $pagetype = 'tag'; 216 } elseif ( 'woocommerce_blocks_product_grid_item_html' === $caller_hook ) { 217 $pagetype = 'grid'; 218 } 219 220 if ( is_product_category() || is_product_tag() ) { 221 $term = get_queried_object(); 222 if ( $term instanceof WP_Term ) { 223 $collection_id = $term->term_id; 224 $item_list_name = $term->name; 225 } 226 } 227 228 if ( is_int( $first_key ) ) { 229 $data[] = 'data-custom_gtm_pagetype="' . esc_attr( $pagetype ) . '"'; 230 $data[] = 'data-custom_gtm_collection_id="' . esc_attr( $collection_id ) . '"'; 231 $data[] = 'data-custom_gtm_item_list_name="' . esc_attr( $item_list_name ) . '"'; 232 } else { 233 $data['data-custom_gtm_pagetype'] = esc_attr( $pagetype ); 234 $data['data-custom_gtm_collection_id'] = esc_attr( $collection_id ); 235 $data['data-custom_gtm_item_list_name'] = esc_attr( $item_list_name ); 236 } 237 238 return $data; 239 } 240 241 /** 162 242 * Convert product data key. 163 243 * … … 203 283 return $array; 204 284 } 205 206 /**207 * Return formatted data attributes.208 *209 * @param array $attrs Attrs.210 * @return array211 */212 private function get_formatted_data_attributes( $attrs ) {213 214 if ( isset( $attrs['imageUrl'] ) ) {215 $attrs['image-url'] = $attrs['imageUrl'];216 unset( $attrs['imageUrl'] );217 }218 219 return $attrs;220 }221 285 } -
gtm-server-side/trunk/README.txt
r3366307 r3374198 4 4 Requires at least: 5.2.0 5 5 Tested up to: 6.8.0 6 Stable tag: 2.1.3 76 Stable tag: 2.1.38 7 7 License: GPLv2 or later 8 8 License URI: http://www.gnu.org/licenses/gpl-2.0.html … … 68 68 == Changelog == 69 69 70 = 2.1.38 = 71 * Added "select_item" event 72 70 73 = 2.1.37 = 71 74 * Fix: submit button -
gtm-server-side/trunk/assets/js/javascript.js
r3174835 r3374198 36 36 } 37 37 38 pluginGtmServerSide.pushAddToCart( 39 pluginGtmServerSide.removePrefixes( el.dataset ) 40 ); 38 let gtmData = pluginGtmServerSide.getGtmItemData( el.dataset ); 39 let customData = pluginGtmServerSide.getCustomItemData( el.dataset ); 40 41 pluginGtmServerSide.pushAddToCart( gtmData ); 42 pluginGtmServerSide.pushSelectItem( gtmData, customData ); 41 43 } 42 44 ); … … 55 57 } 56 58 57 pluginGtmServerSide.pushAddToCart( 58 pluginGtmServerSide.removePrefixes( $el.data() ) 59 ); 59 let gtmData = pluginGtmServerSide.getGtmItemData( $el.data() ); 60 let customData = pluginGtmServerSide.getCustomItemData( $el.data() ); 61 62 pluginGtmServerSide.pushAddToCart( gtmData ); 63 pluginGtmServerSide.pushSelectItem( gtmData, customData ); 60 64 } 61 65 ); … … 99 103 100 104 pluginGtmServerSide.removeFromCart( 101 pluginGtmServerSide. removePrefixes( $thisbutton.data() )105 pluginGtmServerSide.getGtmItemData( $thisbutton.data() ) 102 106 ); 103 107 } … … 118 122 119 123 pluginGtmServerSide.removeFromCart( 120 pluginGtmServerSide. removePrefixes( $el.data() )124 pluginGtmServerSide.getGtmItemData( $el.data() ) 121 125 ); 122 126 } … … 130 134 $elForm.find( '[name^=gtm_]' ) 131 135 ); 132 item = this. removePrefixes( item );136 item = this.getGtmItemData( item ); 133 137 134 138 var $elQty = $elForm.find( '[name=quantity]' ); … … 144 148 $elForm.find( '[name^=gtm_]' ) 145 149 ); 146 item = this. removePrefixes( item );150 item = this.getGtmItemData( item ); 147 151 148 152 var $elQty = $elForm.find( '[name=quantity]' ); … … 243 247 244 248 if ( originalValue < currentValue ) { 245 var item = $this. removePrefixes( elDataset.dataset );249 var item = $this.getGtmItemData( elDataset.dataset ); 246 250 item['quantity'] = currentValue - originalValue; 247 251 … … 254 258 255 259 /** 256 * Re move field prefixes.260 * Return gtm custom data. 257 261 * 258 262 * @param object items List items. 259 263 * @returns object 260 264 */ 261 removePrefixes: function ( items ) { 262 var item = {}; 265 getGtmItemData: function ( items ) { 266 return this._getItemData( items, 'gtm_' ); 267 }, 268 269 /** 270 * Return gtm custom data. 271 * 272 * @param object items List items. 273 * @returns object 274 */ 275 getCustomItemData: function ( items ) { 276 return this._getItemData( items, 'custom_gtm_' ); 277 }, 278 279 /** 280 * Return item data. 281 * 282 * @param object items List items. 283 * @returns object 284 */ 285 _getItemData: function ( items, prefix ) { 286 var result = {}; 263 287 for ( var key in items ) { 264 if ( 0 !== key.indexOf( 'gtm_') ) {288 if ( 0 !== key.indexOf( prefix ) ) { 265 289 continue; 266 290 } 267 291 268 var itemKey = key.replace( 'gtm_', '' )269 item[ itemKey ] = items[key];270 } 271 return item;292 var itemKey = key.replace( prefix, '' ) 293 result[ itemKey ] = items[ key ]; 294 } 295 return result; 272 296 }, 273 297 … … 318 342 */ 319 343 pushAddToCart: function ( item ) { 344 this._pushToDataLayer( item, 'add_to_cart', 'product' ) 345 }, 346 347 /** 348 * Push add_to_cart to dataLayer. 349 * 350 * @param object item List items. 351 * @param object custom Custom data. 352 */ 353 pushSelectItem: function ( item, custom ) { 354 if ( custom?.pagetype ) { // phpcs:ignore WordPress.WhiteSpace.OperatorSpacing.NoSpaceBefore, WordPress.WhiteSpace.OperatorSpacing.NoSpaceAfter 355 this._pushToDataLayer( 356 item, 357 'select_item', 358 custom.pagetype, 359 { 360 collection_id: custom?.collection_id, // phpcs:ignore WordPress.WhiteSpace.OperatorSpacing.NoSpaceBefore, WordPress.WhiteSpace.OperatorSpacing.NoSpaceAfter 361 item_list_name: custom?.item_list_name, // phpcs:ignore WordPress.WhiteSpace.OperatorSpacing.NoSpaceBefore, WordPress.WhiteSpace.OperatorSpacing.NoSpaceAfter 362 } 363 ) 364 } 365 }, 366 367 /** 368 * Push to dataLayer. 369 * 370 * @param object originalItem List items. 371 * @param string event Event name. 372 * @param string pagetype Page type name. 373 * @param object customEcommerce Ecommerce data. 374 */ 375 _pushToDataLayer: function ( originalItem, event, pagetype, customEcommerce = {} ) { 376 item = Object.assign( {}, originalItem ); 320 377 if ( item.item_id ) { 321 378 item = [ item ]; … … 333 390 } 334 391 392 let eventDataEcommerce = { 393 'currency': varGtmServerSide.currency, 394 'value': value.toFixed( 2 ), 395 'items': items, 396 } 397 335 398 var eventData = { 336 'event': this.getDataLayerEventName( 'add_to_cart' ), 337 'ecomm_pagetype': 'product', 338 'ecommerce': { 339 'currency': varGtmServerSide.currency, 340 'value': value.toFixed( 2 ), 341 'items': items, 342 }, 399 'event': this.getDataLayerEventName( event ), 400 'ecomm_pagetype': pagetype, 401 'ecommerce': Object.assign( {}, eventDataEcommerce, customEcommerce ), 343 402 }; 344 403 if ( varGtmServerSide.user_data ) { -
gtm-server-side/trunk/gtm-server-side.php
r3366307 r3374198 8 8 * 9 9 * @wordpress-plugin 10 * Plugin Name: GTM Server Side10 * Plugin Name: Stape Conversion Tracking 11 11 * Plugin URI: https://wordpress.org/plugins/gtm-server-side/ 12 12 * Description: Enhance conversion tracking by implementing server-side tagging using server Google Tag Manager container. Effortlessly configure data layer events in web GTM, send webhooks, set up custom loader, and extend cookie lifetime. 13 * Version: 2.1.3 713 * Version: 2.1.38 14 14 * Author: Stape 15 15 * Author URI: https://stape.io -
gtm-server-side/trunk/includes/class-gtm-server-side-admin-settings.php
r3359782 r3374198 61 61 public function admin_menu() { 62 62 add_options_page( 63 __( ' GTM Server Side', 'gtm-server-side' ),64 __( ' GTM Server Side', 'gtm-server-side' ),63 __( 'Stape Conversion Tracking', 'gtm-server-side' ), 64 __( 'Stape Conversion Tracking', 'gtm-server-side' ), 65 65 'manage_options', 66 66 GTM_SERVER_SIDE_ADMIN_SLUG, -
gtm-server-side/trunk/includes/class-gtm-server-side-event-addtocart.php
r3359782 r3374198 36 36 add_action( 'woocommerce_after_add_to_cart_button', array( $this, 'woocommerce_after_add_to_cart_button' ) ); 37 37 add_filter( 'woocommerce_grouped_product_list_column_quantity', array( $this, 'woocommerce_grouped_product_list_column_quantity' ), 10, 2 ); 38 39 add_filter( 'gtm_server_side_before_html_data_attributes', array( $this, 'format_data_attributes' ) ); 40 add_filter( 'gtm_server_side_after_html_data_attributes', array( $this, 'attach_data_to_event_select_item' ), 20, 3 ); 38 41 } 39 42 … … 52 55 53 56 $data = $this->get_item( $item['data'] ); 54 $data = $this->get_formatted_data_attributes( $data);57 $data = apply_filters( 'gtm_server_side_before_html_data_attributes', $data, 'remove_from_cart', 'woocommerce_cart_item_remove_link' ); 55 58 $data['quantity'] = isset( $item['quantity'] ) ? intval( $item['quantity'] ) : 1; 56 $attrs = $this->convert_product_data_to_html_attrs( $data ); 59 $data = $this->convert_product_data_to_html_attrs( $data ); 60 $attrs = apply_filters( 'gtm_server_side_after_html_data_attributes', $data, 'remove_from_cart', 'woocommerce_cart_item_remove_link' ); 57 61 $link = str_replace( '<a ', '<a ' . join( ' ', $attrs ), $link ); 58 62 … … 78 82 79 83 $data = $this->get_item( $product ); 80 $data = $this->get_formatted_data_attributes( $data);84 $data = apply_filters( 'gtm_server_side_before_html_data_attributes', $data, 'add_to_cart', 'woocommerce_loop_add_to_cart_args' ); 81 85 $data['quantity'] = isset( $args['quantity'] ) ? intval( $args['quantity'] ) : 1; 82 86 $data['index'] = isset( $woocommerce_loop['loop'] ) ? intval( $woocommerce_loop['loop'] ) : 1; 83 $attrs = $this->convert_product_data_key( $data ); 87 $data = $this->convert_product_data_key( $data ); 88 $attrs = apply_filters( 'gtm_server_side_after_html_data_attributes', $data, 'add_to_cart', 'woocommerce_loop_add_to_cart_args' ); 84 89 85 90 if ( ! isset( $args['attributes'] ) || ! is_array( $args['attributes'] ) ) { … … 109 114 110 115 $data = $this->get_item( $product ); 111 $data = $this->get_formatted_data_attributes( $data ); 112 $attrs = $this->convert_product_data_to_html_attrs( $data ); 116 $data = apply_filters( 'gtm_server_side_before_html_data_attributes', $data, 'add_to_cart', 'woocommerce_blocks_product_grid_item_html' ); 117 $data = $this->convert_product_data_to_html_attrs( $data ); 118 $attrs = apply_filters( 'gtm_server_side_after_html_data_attributes', $data, 'add_to_cart', 'woocommerce_blocks_product_grid_item_html' ); 113 119 $html = str_replace( '<li ', '<li ' . join( ' ', $attrs ), $html ); 114 120 … … 160 166 161 167 /** 168 * Formatted data attributes. 169 * 170 * @param array $data Attrs. 171 * @return array 172 */ 173 public function format_data_attributes( $data ) { 174 if ( isset( $data['imageUrl'] ) ) { 175 $data['image-url'] = $data['imageUrl']; 176 unset( $data['imageUrl'] ); 177 } 178 179 return $data; 180 } 181 182 /** 183 * Attach data to event select item. 184 * 185 * @param array $data Attrs. 186 * @param string $type Type. 187 * @param string $caller_hook Caller hook. 188 * @return array 189 */ 190 public function attach_data_to_event_select_item( $data, $type, $caller_hook ) { 191 if ( 'add_to_cart' !== $type ) { 192 return $data; 193 } 194 195 $first_key = false; 196 if ( is_array( $data ) ) { 197 $first_key = array_key_first( $data ); 198 } 199 200 if ( false === $first_key ) { 201 return $data; 202 } 203 204 $pagetype = null; 205 $collection_id = null; 206 $item_list_name = null; 207 208 if ( is_search() ) { 209 $pagetype = 'search'; 210 } elseif ( is_shop() ) { 211 $pagetype = 'shop'; 212 } elseif ( is_product_category() ) { 213 $pagetype = 'category'; 214 } elseif ( is_product_tag() ) { 215 $pagetype = 'tag'; 216 } elseif ( 'woocommerce_blocks_product_grid_item_html' === $caller_hook ) { 217 $pagetype = 'grid'; 218 } 219 220 if ( is_product_category() || is_product_tag() ) { 221 $term = get_queried_object(); 222 if ( $term instanceof WP_Term ) { 223 $collection_id = $term->term_id; 224 $item_list_name = $term->name; 225 } 226 } 227 228 if ( is_int( $first_key ) ) { 229 $data[] = 'data-custom_gtm_pagetype="' . esc_attr( $pagetype ) . '"'; 230 $data[] = 'data-custom_gtm_collection_id="' . esc_attr( $collection_id ) . '"'; 231 $data[] = 'data-custom_gtm_item_list_name="' . esc_attr( $item_list_name ) . '"'; 232 } else { 233 $data['data-custom_gtm_pagetype'] = esc_attr( $pagetype ); 234 $data['data-custom_gtm_collection_id'] = esc_attr( $collection_id ); 235 $data['data-custom_gtm_item_list_name'] = esc_attr( $item_list_name ); 236 } 237 238 return $data; 239 } 240 241 /** 162 242 * Convert product data key. 163 243 * … … 203 283 return $array; 204 284 } 205 206 /**207 * Return formatted data attributes.208 *209 * @param array $attrs Attrs.210 * @return array211 */212 private function get_formatted_data_attributes( $attrs ) {213 214 if ( isset( $attrs['imageUrl'] ) ) {215 $attrs['image-url'] = $attrs['imageUrl'];216 unset( $attrs['imageUrl'] );217 }218 219 return $attrs;220 }221 285 }
Note: See TracChangeset
for help on using the changeset viewer.