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

Using filters to collect cart endpoint data using StoreAPI #3840

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
*** Changelog ***

= 9.2.0 - xxxx-xx-xx =
* Dev - Replaces part of the StoreAPI call code for the cart endpoints to use the newly introduced filter.
* Dev - Introduces new payment method constants for the express methods: Google Pay, Apple Pay, Link, and Amazon Pay.
* Fix - Prevent an express checkout element's load errors from affecting other express checkout elements.
* Tweak - Process ECE cart requests using the Blocks (Store) API.
Expand Down
1 change: 1 addition & 0 deletions client/blocks/express-checkout/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
normalizeLineItems,
} from 'wcstripe/express-checkout/utils';
import 'wcstripe/express-checkout/compatibility/wc-order-attribution';
import 'wcstripe/express-checkout/compatibility/wc-product-page';

export const useExpressCheckout = ( {
api,
Expand Down
30 changes: 12 additions & 18 deletions client/entrypoints/express-checkout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import {
getExpressCheckoutButtonAppearance,
getExpressCheckoutButtonStyleSettings,
getExpressCheckoutData,
isManualPaymentMethodCreation,
getPaymentMethodTypesForExpressMethod,
isManualPaymentMethodCreation,
normalizeLineItems,
} from 'wcstripe/express-checkout/utils';
import {
Expand All @@ -27,6 +27,7 @@ import {
import { getStripeServerData } from 'wcstripe/stripe-utils';
import { getAddToCartVariationParams } from 'wcstripe/utils';
import 'wcstripe/express-checkout/compatibility/wc-order-attribution';
import 'wcstripe/express-checkout/compatibility/wc-product-page';
import './styles.scss';
import {
EXPRESS_PAYMENT_METHOD_SETTING_AMAZON_PAY,
Expand Down Expand Up @@ -516,27 +517,12 @@ jQuery( function ( $ ) {
addToCart: () => {
let productId = $( '.single_add_to_cart_button' ).val();

// Check if product is a variable product.
if ( $( '.single_variation_wrap' ).length ) {
productId = $( '.single_variation_wrap' )
.find( 'input[name="product_id"]' )
.val();
}

if ( $( '.wc-bookings-booking-form' ).length ) {
productId = $( '.wc-booking-product-id' ).val();
}

const data = {
qty: $( quantityInputSelector ).val(),
};

if ( useLegacyCartEndpoints ) {
data.product_id = productId;
data.attributes = wcStripeECE.getAttributes().data;
} else {
data.id = productId;
data.variation = [];
if ( $( '.wc-bookings-booking-form' ).length ) {
productId = $( '.wc-booking-product-id' ).val();
}

// Add extension data to the POST body
Expand All @@ -559,10 +545,18 @@ jQuery( function ( $ ) {
}
} );

// Legacy support for variations.
if ( useLegacyCartEndpoints ) {
data.product_id = productId;
data.attributes = wcStripeECE.getAttributes().data;

return api.expressCheckoutAddToCartLegacy( data );
}

// BlocksAPI partial support (lacking support for variations).
data.id = productId;
data.variation = [];

return api.expressCheckoutAddToCart( data );
},

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { applyFilters } from '@wordpress/hooks';
import { render } from '@testing-library/react';
import 'wcstripe/express-checkout/compatibility/wc-product-page';

describe( 'ECE product page compatibility', () => {
describe( 'filters out data when adding item to the cart', () => {
it( 'single variation form is present', () => {
function App() {
return (
<div className="single_variation_wrap">
<input name="product_id" defaultValue="123" />
</div>
);
}
render( <App /> );

const cartAddItemData = applyFilters(
'wcstripe.express-checkout.cart-add-item',
{}
);

expect( cartAddItemData ).toStrictEqual( { id: 123 } );
} );
} );
} );
3 changes: 0 additions & 3 deletions client/express-checkout/compatibility/wc-order-attribution.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/**
* External dependencies
*/
import { addFilter } from '@wordpress/hooks';
import { extractOrderAttributionData } from 'wcstripe/blocks/utils';

Expand Down
24 changes: 24 additions & 0 deletions client/express-checkout/compatibility/wc-product-page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import jQuery from 'jquery';
import { addFilter } from '@wordpress/hooks';

/**
* Sets the product ID when using the BlocksAPI and single variation form is present.
*/
addFilter(
'wcstripe.express-checkout.cart-add-item',
'automattic/wcstripe/express-checkout',
( productData ) => {
const $variationInformation = jQuery( '.single_variation_wrap' );
if ( ! $variationInformation.length ) {
return productData;
}

const productId = $variationInformation
.find( 'input[name="product_id"]' )
.val();
return {
...productData,
id: parseInt( productId, 10 ),
};
}
);
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ If you get stuck, you can ask for help in the [Plugin Forum](https://wordpress.o
== Changelog ==

= 9.2.0 - xxxx-xx-xx =
* Dev - Replaces part of the StoreAPI call code for the cart endpoints to use the newly introduced filter.
* Dev - Introduces new payment method constants for the express methods: Google Pay, Apple Pay, Link, and Amazon Pay.
* Fix - Prevent an express checkout element's load errors from affecting other express checkout elements.
* Tweak - Process ECE cart requests using the Blocks (Store) API.
Expand Down
Loading