Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix invalid value error for paymentMethodMessaging in product details page #7153

Merged
merged 28 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
c30f9cf
Fix invalid value error for paymentMethodMessaging
mgascam Sep 7, 2023
01c1c6d
Add changelog
mgascam Sep 8, 2023
720f215
Merge branch 'develop' into fix/6819-payment-method-messaging-invalid…
mgascam Sep 11, 2023
3fefa81
Merge branch 'develop' into fix/6819-payment-method-messaging-invalid…
mgascam Sep 11, 2023
23da3b2
PR feedback: prevent updating if missing quantity or currency
mgascam Sep 11, 2023
3ad3c2a
PR feedback: improve comment
mgascam Sep 11, 2023
9bc87d5
Fall back to quantity 1 when selector is not present
mgascam Sep 11, 2023
41910db
Merge branch 'develop' into fix/6819-payment-method-messaging-invalid…
mgascam Sep 11, 2023
6141de8
Revert "Fall back to quantity 1 when selector is not present"
mgascam Sep 11, 2023
919aa9b
Merge branch 'develop' into fix/6819-payment-method-messaging-invalid…
mgascam Sep 12, 2023
15d2d7d
Address PR feedback
mgascam Sep 14, 2023
56b844c
Merge branch 'fix/6819-payment-method-messaging-invalid-value' of git…
mgascam Sep 14, 2023
f78dea8
Merge branch 'develop' into fix/6819-payment-method-messaging-invalid…
mgascam Sep 21, 2023
9550cdc
Merge branch 'fix/6819-payment-method-messaging-invalid-value' of git…
mgascam Sep 21, 2023
7685379
Address PR feedback
mgascam Sep 21, 2023
9032c32
Refactor to avoid code repetition
mgascam Sep 21, 2023
839c914
Handle quantity change when variation is selected
mgascam Sep 21, 2023
8859917
Fix wrong informed amount with variations
mgascam Sep 22, 2023
8cf0fad
Fix messaging not updated when quantity selector does not exist for …
mgascam Sep 22, 2023
9541e68
Merge branch 'develop' into fix/6819-payment-method-messaging-invalid…
mgascam Sep 22, 2023
be2fdfa
Amend comments
mgascam Sep 22, 2023
b6ee174
Rename variable
mgascam Sep 22, 2023
35a6a22
Merge branch 'develop' into fix/6819-payment-method-messaging-invalid…
mgascam Sep 25, 2023
51208ac
Merge branch 'develop' into fix/6819-payment-method-messaging-invalid…
mgascam Sep 25, 2023
d6b1695
Merge branch 'develop' into fix/6819-payment-method-messaging-invalid…
mgascam Sep 27, 2023
1cac1e4
PR feedback
mgascam Sep 27, 2023
47e8fa6
Merge branch 'develop' into fix/6819-payment-method-messaging-invalid…
mgascam Sep 27, 2023
3df9836
Merge branch 'develop' into fix/6819-payment-method-messaging-invalid…
mgascam Sep 28, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions changelog/fix-6819-payment-method-messaging-invalid-value
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fix

Improved product details script with enhanced price calculation, and fallbacks for potential undefined values.
108 changes: 76 additions & 32 deletions client/product-details/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,51 +7,95 @@
import { initializeBnplSiteMessaging } from './bnpl-site-messaging';

jQuery( function ( $ ) {
/**
* Check for the existence of the `wcpayStripeSiteMessaging` variable on the window object.
* This variable holds the configuration for Stripe site messaging and contains the following keys:
* - productId: The ID of the product.
* - productVariations: Variations of the product.
* - country: The country of the customer. Defaults to the store's country.
* - publishableKey: The key used for Stripe's API calls.
* - paymentMethods: Enabled BNPL payment methods.
*
* If this variable is not set, the script will exit early to prevent further execution.
*/
if ( ! window.wcpayStripeSiteMessaging ) {
return;
}

const { productVariations, productId } = window.wcpayStripeSiteMessaging;
const bnplPaymentMessageElement = initializeBnplSiteMessaging();
const { productVariations } = window.wcpayStripeSiteMessaging;
let { productId } = window.wcpayStripeSiteMessaging;

// Utility function to safely parse integers and handle NaN
const safeParseInt = ( value ) => {
mgascam marked this conversation as resolved.
Show resolved Hide resolved
const result = parseInt( value, 10 );
return isNaN( result ) ? 0 : result;
};

const updateBnplPaymentMessage = ( amount, currency ) => {
if ( amount > 0 && currency ) {
bnplPaymentMessageElement.update( { amount, currency } );
}
};

const quantityInput = $( '.quantity input[type=number]' );

const resetBnplPaymentMessage = () => {
const quantity = $( '.quantity input[type=number]' ).val();
productId = 'base_product';
bnplPaymentMessageElement.update( {
amount:
parseInt( productVariations.base_product.amount, 10 ) *
quantity,
currency: productVariations.base_product.currency,
} );
if ( ! quantityInput.length || ! productVariations.base_product )
mgascam marked this conversation as resolved.
Show resolved Hide resolved
return;

const quantity = safeParseInt( quantityInput.val() );
const baseProductAmount = safeParseInt(
productVariations.base_product.amount
);

const amount = baseProductAmount * quantity;
const currency = productVariations.base_product.currency;
mgascam marked this conversation as resolved.
Show resolved Hide resolved

updateBnplPaymentMessage( amount, currency );
};

$( '.quantity input[type=number]' ).on( 'change', function ( event ) {
const newQuantity = event.target.value;
const price = productVariations[ productId ].amount;
bnplPaymentMessageElement.update( {
amount: parseInt( price, 10 ) * newQuantity,
currency: productVariations[ productId ].currency,
} );
// Update BNPL message based on the quantity change
quantityInput.on( 'change', ( event ) => {
const newQuantity = safeParseInt( event.target.value );
const price = safeParseInt( productVariations[ productId ].amount );
mgascam marked this conversation as resolved.
Show resolved Hide resolved

const amount = price * newQuantity;
const currency = productVariations[ productId ]?.currency;

updateBnplPaymentMessage( amount, currency );
} );

// Handle BNPL messaging for variable products.
if ( Object.keys( productVariations ).length > 1 ) {
$( '.single_variation_wrap' ).on( 'show_variation', function (
event,
variation
) {
const quantity = $( '.quantity input[type=number]' ).val();
const variationPrice =
productVariations[ variation.variation_id ].amount;
productId = variation.variation_id;
bnplPaymentMessageElement.update( {
amount: parseInt( variationPrice, 10 ) * quantity,
currency: productVariations[ variation.variation_id ].currency,
} );
} );
// Update BNPL message based on product variation
$( '.single_variation_wrap' ).on(
'show_variation',
( event, variation ) => {
if (
! quantityInput.length ||
! productVariations[ variation.variation_id ]
)
mgascam marked this conversation as resolved.
Show resolved Hide resolved
return;

const quantity = safeParseInt( quantityInput.val() );
const variationPrice = safeParseInt(
productVariations[ variation.variation_id ].amount
mgascam marked this conversation as resolved.
Show resolved Hide resolved
);

const amount = variationPrice * quantity;
const currency =
productVariations[ variation.variation_id ]?.currency;

updateBnplPaymentMessage( amount, currency );
}
);

// If variation is changed back to default, reset BNPL messaging.
$( '.variations' ).on( 'change', function ( event ) {
// Reset BNPL message if variation is changed back to default
$( '.variations' ).on( 'change', ( event ) => {
if ( event.target.value === '' ) resetBnplPaymentMessage();
} );

// Reset BNPL message on variations reset
$( '.reset_variations' ).on( 'click', resetBnplPaymentMessage );
}
} );