Skip to content

Commit

Permalink
Merge pull request #293 from duracelltomi/fix/add_to_cart_price_not_f…
Browse files Browse the repository at this point in the history
…loat

Fix/add to cart price not float
  • Loading branch information
duracelltomi authored Sep 19, 2023
2 parents 9e49fd5 + ac2574c commit eccc0b3
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
5 changes: 4 additions & 1 deletion admin/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -1006,16 +1006,19 @@ function gtm4wp_show_admin_page() {

/**
* Hook function for admin_menu. Adds the plugin options page into the Settings menu of the WordPress admin.
* The capability can be changed with the gtm4wp_admin_page_capability filter.
*
* @see https://developer.wordpress.org/reference/hooks/admin_menu/
*
* @return void
*/
function gtm4wp_add_admin_page() {
$capability = apply_filters( 'gtm4wp_admin_page_capability', 'manage_options' );

add_options_page(
esc_html__( 'Google Tag Manager for WordPress settings', 'duracelltomi-google-tag-manager' ),
esc_html__( 'Google Tag Manager', 'duracelltomi-google-tag-manager' ),
'manage_options',
$capability,
GTM4WP_ADMINSLUG,
'gtm4wp_show_admin_page'
);
Expand Down
2 changes: 1 addition & 1 deletion integration/woocommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ function gtm4wp_process_product( $product, $additional_product_attributes, $attr
'name' => $product->get_title(),
'sku' => $product_sku ? $product_sku : $product_id,
'category' => $product_cat,
'price' => round( (float) wc_get_price_to_display( $product ), 2 ),
'price' => round( (float) wc_get_price_to_display( $product ), 2 ), // Unfortunately this does not force a .00 postfix for integers.
'stocklevel' => $product->get_stock_quantity(),
);

Expand Down
31 changes: 26 additions & 5 deletions js/gtm4wp-woocommerce-enhanced.js
Original file line number Diff line number Diff line change
Expand Up @@ -518,9 +518,10 @@ function gtm4wp_process_woocommerce_pages() {
});

// fire ga4 version
const variant_value = gtm4wp_last_selected_product_variation.price * gtm4wp_last_selected_product_variation.quantity;
gtm4wp_push_ecommerce( 'add_to_cart', [ gtm4wp_map_eec_to_ga4( gtm4wp_last_selected_product_variation ) ], {
'currency': gtm4wp_currency,
'value': gtm4wp_last_selected_product_variation.price * gtm4wp_last_selected_product_variation.quantity
'value': variant_value.toFixed(2)
});
}
} else if ( product_is_grouped ) {
Expand All @@ -541,10 +542,15 @@ function gtm4wp_process_woocommerce_pages() {
return true;
}

let group_product_price = parseFloat(dom_productdata.getAttribute( 'data-gtm4wp_product_price' ));
if ( isNaN(group_product_price) ) {
group_product_price = 0;
}

const product_data = {
'id': gtm4wp_use_sku_instead ? dom_productdata.getAttribute( 'data-gtm4wp_product_sku' ) : dom_productdata.getAttribute( 'data-gtm4wp_product_id' ),
'name': dom_productdata.getAttribute( 'data-gtm4wp_product_name' ),
'price': dom_productdata.getAttribute( 'data-gtm4wp_product_price' ),
'price': group_product_price.toFixed(2),
'category': dom_productdata.getAttribute( 'data-gtm4wp_product_cat' ),
'quantity': product_qty,
'stocklevel': dom_productdata.getAttribute( 'data-gtm4wp_product_stocklevel' ),
Expand Down Expand Up @@ -574,14 +580,23 @@ function gtm4wp_process_woocommerce_pages() {
// fire ga4 version
gtm4wp_push_ecommerce( 'add_to_cart', ga4_products, {
'currency': gtm4wp_currency,
'value': sum_value
'value': sum_value.toFixed(2)
});
} else {
const product_id_el = gtm4wp_use_sku_instead ? product_form.querySelector( '[name=gtm4wp_sku]' ) : product_form.querySelector( '[name=gtm4wp_id]' );

let product_price = 0;
if ( product_form.querySelector( '[name=gtm4wp_price]' ) ) {
product_price = parseFloat(product_form.querySelector( '[name=gtm4wp_price]' ).value);
if ( isNaN(product_price) ) {
product_price = 0;
}
}

const product_data = {
'id': product_id_el && product_id_el.value,
'name': product_form.querySelector( '[name=gtm4wp_name]' ) && product_form.querySelector( '[name=gtm4wp_name]' ).value,
'price': product_form.querySelector( '[name=gtm4wp_price]' ) && product_form.querySelector( '[name=gtm4wp_price]' ).value,
'price': product_price.toFixed(2),
'category': product_form.querySelector( '[name=gtm4wp_category]' ) && product_form.querySelector( '[name=gtm4wp_category]' ).value,
'quantity': product_form.querySelector( '[name=quantity]' ) && product_form.querySelector( '[name=quantity]' ).value,
'stocklevel': product_form.querySelector( '[name=gtm4wp_stocklevel]' ) && product_form.querySelector( '[name=gtm4wp_stocklevel]' ).value,
Expand Down Expand Up @@ -836,7 +851,13 @@ function gtm4wp_process_woocommerce_pages() {
if ( gtm4wp_use_sku_instead && product_variation.sku && ('' !== product_variation.sku) ) {
current_product_detail_data.id = product_variation.sku;
}
current_product_detail_data.price = product_variation.display_price;
const variant_price = parsefloat( product_variation.display_price );
if ( isNaN( variant_price ) ) {
current_product_detail_data.price = 0;
} else {
current_product_detail_data.price = variant_price.toFixed(2);
}


let product_variation_attribute_values = [];
for( let attrib_key in product_variation.attributes ) {
Expand Down
2 changes: 2 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,8 @@ Instead of the above events, you can use the new GA4 ecommerce events (add_to_ca
In the Universal Analytics GTM tag now you have an option to use the GA4 ecommerce data in the data layer.
If you are on GA360 and still collecting ecommerce data, you need to update your GTM setup to use that option firing on the GA4 ecommerce events.

* Updated: use the gtm4wp_admin_page_capability filter to modify the capability needed to use the admin page (thanks [emreerkan](https://github.com/emreerkan))
* Fixed: price did not include the .00 postfix for integers in the add_to_cart event
* Added: if needed, you can turn on clearing the ecommerce object before a new GA4 event is being pushed.

= 1.18.1 =
Expand Down

0 comments on commit eccc0b3

Please sign in to comment.