Skip to content

Commit 467f544

Browse files
wjrosadiegocurbelo
authored andcommitted
Using filters to collect cart endpoint data using StoreAPI (#3840)
* Using filters to collect cart endpoint data using StoreAPI * Fix product page ID retrieval * Adding specific unit tests * Changelog and readme entries * Reverting part of the hooks since it is required by the legacy endpoint --------- Co-authored-by: Diego Curbelo <[email protected]>
1 parent 50a0da1 commit 467f544

File tree

7 files changed

+64
-21
lines changed

7 files changed

+64
-21
lines changed

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
*** Changelog ***
22

33
= 9.2.0 - xxxx-xx-xx =
4+
* Dev - Replaces part of the StoreAPI call code for the cart endpoints to use the newly introduced filter.
45
* Dev - Add new E2E tests for Link express checkout.
56
* Add - Add Amazon Pay to block cart and block checkout.
67
* Fix - Remove intentional delay when displaying tax-related notice for express checkout, causing click event to time out.

client/blocks/express-checkout/hooks.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
normalizeLineItems,
1616
} from 'wcstripe/express-checkout/utils';
1717
import 'wcstripe/express-checkout/compatibility/wc-order-attribution';
18+
import 'wcstripe/express-checkout/compatibility/wc-product-page';
1819

1920
export const useExpressCheckout = ( {
2021
api,

client/entrypoints/express-checkout/index.js

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import {
1111
getExpressCheckoutButtonAppearance,
1212
getExpressCheckoutButtonStyleSettings,
1313
getExpressCheckoutData,
14-
isManualPaymentMethodCreation,
1514
getPaymentMethodTypesForExpressMethod,
15+
isManualPaymentMethodCreation,
1616
normalizeLineItems,
1717
} from 'wcstripe/express-checkout/utils';
1818
import {
@@ -28,6 +28,7 @@ import {
2828
import { getStripeServerData } from 'wcstripe/stripe-utils';
2929
import { getAddToCartVariationParams } from 'wcstripe/utils';
3030
import 'wcstripe/express-checkout/compatibility/wc-order-attribution';
31+
import 'wcstripe/express-checkout/compatibility/wc-product-page';
3132
import './styles.scss';
3233
import {
3334
EXPRESS_PAYMENT_METHOD_SETTING_AMAZON_PAY,
@@ -527,27 +528,12 @@ jQuery( function ( $ ) {
527528
addToCart: () => {
528529
let productId = $( '.single_add_to_cart_button' ).val();
529530

530-
// Check if product is a variable product.
531-
if ( $( '.single_variation_wrap' ).length ) {
532-
productId = $( '.single_variation_wrap' )
533-
.find( 'input[name="product_id"]' )
534-
.val();
535-
}
536-
537-
if ( $( '.wc-bookings-booking-form' ).length ) {
538-
productId = $( '.wc-booking-product-id' ).val();
539-
}
540-
541531
const data = {
542532
qty: $( quantityInputSelector ).val(),
543533
};
544534

545-
if ( useLegacyCartEndpoints ) {
546-
data.product_id = productId;
547-
data.attributes = wcStripeECE.getAttributes().data;
548-
} else {
549-
data.id = productId;
550-
data.variation = [];
535+
if ( $( '.wc-bookings-booking-form' ).length ) {
536+
productId = $( '.wc-booking-product-id' ).val();
551537
}
552538

553539
// Add extension data to the POST body
@@ -570,10 +556,18 @@ jQuery( function ( $ ) {
570556
}
571557
} );
572558

559+
// Legacy support for variations.
573560
if ( useLegacyCartEndpoints ) {
561+
data.product_id = productId;
562+
data.attributes = wcStripeECE.getAttributes().data;
563+
574564
return api.expressCheckoutAddToCartLegacy( data );
575565
}
576566

567+
// BlocksAPI partial support (lacking support for variations).
568+
data.id = productId;
569+
data.variation = [];
570+
577571
return api.expressCheckoutAddToCart( data );
578572
},
579573

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { applyFilters } from '@wordpress/hooks';
2+
import { render } from '@testing-library/react';
3+
import 'wcstripe/express-checkout/compatibility/wc-product-page';
4+
5+
describe( 'ECE product page compatibility', () => {
6+
describe( 'filters out data when adding item to the cart', () => {
7+
it( 'single variation form is present', () => {
8+
function App() {
9+
return (
10+
<div className="single_variation_wrap">
11+
<input name="product_id" defaultValue="123" />
12+
</div>
13+
);
14+
}
15+
render( <App /> );
16+
17+
const cartAddItemData = applyFilters(
18+
'wcstripe.express-checkout.cart-add-item',
19+
{}
20+
);
21+
22+
expect( cartAddItemData ).toStrictEqual( { id: 123 } );
23+
} );
24+
} );
25+
} );

client/express-checkout/compatibility/wc-order-attribution.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
/**
2-
* External dependencies
3-
*/
41
import { addFilter } from '@wordpress/hooks';
52
import { extractOrderAttributionData } from 'wcstripe/blocks/utils';
63

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import jQuery from 'jquery';
2+
import { addFilter } from '@wordpress/hooks';
3+
4+
/**
5+
* Sets the product ID when using the BlocksAPI and single variation form is present.
6+
*/
7+
addFilter(
8+
'wcstripe.express-checkout.cart-add-item',
9+
'automattic/wcstripe/express-checkout',
10+
( productData ) => {
11+
const $variationInformation = jQuery( '.single_variation_wrap' );
12+
if ( ! $variationInformation.length ) {
13+
return productData;
14+
}
15+
16+
const productId = $variationInformation
17+
.find( 'input[name="product_id"]' )
18+
.val();
19+
return {
20+
...productData,
21+
id: parseInt( productId, 10 ),
22+
};
23+
}
24+
);

readme.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ If you get stuck, you can ask for help in the [Plugin Forum](https://wordpress.o
111111
== Changelog ==
112112

113113
= 9.2.0 - xxxx-xx-xx =
114+
* Dev - Replaces part of the StoreAPI call code for the cart endpoints to use the newly introduced filter.
114115
* Dev - Add new E2E tests for Link express checkout.
115116
* Add - Add Amazon Pay to block cart and block checkout.
116117
* Fix - Remove intentional delay when displaying tax-related notice for express checkout, causing click event to time out.

0 commit comments

Comments
 (0)