Get product by ID if there's more products returned from useStoreProducts (https://github.com/woocommerce/woocommerce-blocks/pull/9079)

* Get product by ID if there's more products returned from useStoreProducts
This commit is contained in:
Karol Manijak 2023-04-19 09:52:16 +02:00 committed by GitHub
parent 4fbf32cef6
commit 21269963e8
1 changed files with 7 additions and 1 deletions

View File

@ -7,6 +7,9 @@ import {
useProductDataContext,
} from '@woocommerce/shared-context';
const getProductById = ( products, id ) =>
products.find( ( product ) => product.id === id );
/**
* Loads the product from the API and adds to the context provider.
*
@ -22,7 +25,10 @@ const OriginalComponentWithContext = ( props ) => {
} );
const productFromAPI = {
product: id > 0 && products.length > 0 ? products[ 0 ] : null,
product:
id > 0 && products.length > 0
? getProductById( products, id )
: null,
isLoading: productsLoading,
};