-
Notifications
You must be signed in to change notification settings - Fork 210
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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]>
- Loading branch information
1 parent
50a0da1
commit beb2828
Showing
7 changed files
with
64 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
client/express-checkout/compatibility/__tests__/wc-product-page.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
3
client/express-checkout/compatibility/wc-order-attribution.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ), | ||
}; | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters