diff --git a/readme.txt b/readme.txt index 3f556af..a456e5f 100644 --- a/readme.txt +++ b/readme.txt @@ -90,6 +90,7 @@ Yes! Pagespeed is one of our main focus points, and we strive to make the plugin = 1.X = Enhancements: +* Added a more robust method of adding data attributes to the HTML. Bugfixes: diff --git a/src/Integration/WooCommerce.php b/src/Integration/WooCommerce.php index ffb39da..7a2fc17 100644 --- a/src/Integration/WooCommerce.php +++ b/src/Integration/WooCommerce.php @@ -957,18 +957,25 @@ public function cart_item_remove_link( string $woocommerce_cart_item_remove_link 'remove_from_cart' ); - $find_href = ' href="'; - $replace_width_product_data = sprintf( ' data-gtmkit_product_data="%s" href="', esc_attr( wp_json_encode( $item_data ) ) ); + if ( class_exists( 'WP_HTML_Tag_Processor' ) ) { + $link_html = new \WP_HTML_Tag_Processor( $woocommerce_cart_item_remove_link ); + $link_html->next_tag(); + $link_html->set_attribute( 'data-gtmkit_product_data', esc_attr( wp_json_encode( $item_data ) ) ); + $woocommerce_cart_item_remove_link = $link_html->get_updated_html(); + } else { + $find_href = ' href="'; + $replace_width_product_data = sprintf( ' data-gtmkit_product_data="%s" href="', esc_attr( wp_json_encode( $item_data ) ) ); - $substring_pos = strpos( $woocommerce_cart_item_remove_link, $find_href ); + $substring_pos = strpos( $woocommerce_cart_item_remove_link, $find_href ); - if ( $substring_pos !== false ) { - $woocommerce_cart_item_remove_link = substr_replace( - $woocommerce_cart_item_remove_link, - $replace_width_product_data, - $substring_pos, - strlen( $find_href ) - ); + if ( $substring_pos !== false ) { + $woocommerce_cart_item_remove_link = substr_replace( + $woocommerce_cart_item_remove_link, + $replace_width_product_data, + $substring_pos, + strlen( $find_href ) + ); + } } return $woocommerce_cart_item_remove_link;