Skip to content

Commit

Permalink
Added a more robust method of adding data attributes to the HTML
Browse files Browse the repository at this point in the history
  • Loading branch information
TorbenLundsgaard committed Sep 20, 2023
1 parent f355ffd commit 68fee8a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
27 changes: 17 additions & 10 deletions src/Integration/WooCommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 68fee8a

Please sign in to comment.