[Product Block Editor]: improve getting suggested products from linked product section (#44047)
* introduce getSuggestedProductsFor helper * use suggested products to get choices * update helper to the data store * changelog
This commit is contained in:
parent
65d71fa1e2
commit
aaf9981e90
|
@ -0,0 +1,4 @@
|
|||
Significance: patch
|
||||
Type: update
|
||||
|
||||
[Product Block Editor]: improve getting suggested products from linked product section
|
|
@ -41,7 +41,7 @@ import {
|
|||
LinkedProductListBlockAttributes,
|
||||
LinkedProductListBlockEmptyState,
|
||||
} from './types';
|
||||
import getRelatedProducts from '../../../utils/get-related-products';
|
||||
import { getSuggestedProductsFor } from '../../../utils/get-related-products';
|
||||
import { SectionActions } from '../../../components/block-slot-fill';
|
||||
|
||||
export function EmptyStateImage( {
|
||||
|
@ -182,8 +182,8 @@ export function LinkedProductListBlockEdit( {
|
|||
|
||||
setIsChoosingProducts( true );
|
||||
|
||||
const relatedProducts = ( await getRelatedProducts( productId, {
|
||||
fallbackToRandomProducts: true,
|
||||
const linkedProducts = ( await getSuggestedProductsFor( {
|
||||
postId: productId,
|
||||
} ) ) as Product[];
|
||||
|
||||
dispatch( {
|
||||
|
@ -195,12 +195,12 @@ export function LinkedProductListBlockEdit( {
|
|||
|
||||
setIsChoosingProducts( false );
|
||||
|
||||
if ( ! relatedProducts ) {
|
||||
if ( ! linkedProducts ) {
|
||||
return;
|
||||
}
|
||||
|
||||
const newLinkedProducts = selectSearchedProductDispatcher(
|
||||
relatedProducts,
|
||||
linkedProducts,
|
||||
[]
|
||||
);
|
||||
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
* External dependencies
|
||||
*/
|
||||
import { select, resolveSelect } from '@wordpress/data';
|
||||
import { PRODUCTS_STORE_NAME } from '@woocommerce/data';
|
||||
import type { Product } from '@woocommerce/data';
|
||||
|
||||
type getRelatedProductsOptions = {
|
||||
|
@ -67,3 +68,35 @@ export default async function getRelatedProducts(
|
|||
}
|
||||
) ) as Product[];
|
||||
}
|
||||
|
||||
type getSuggestedProductsForOptions = {
|
||||
postId: number;
|
||||
postType?: 'product' | 'post' | 'page';
|
||||
};
|
||||
|
||||
/**
|
||||
* Get suggested products for a given post ID.
|
||||
*
|
||||
* @param { getSuggestedProductsForOptions } options - Options.
|
||||
* @return { Promise<Product[] | undefined> } Suggested products.
|
||||
*/
|
||||
export async function getSuggestedProductsFor( {
|
||||
postId,
|
||||
postType = 'product',
|
||||
}: getSuggestedProductsForOptions ): Promise< Product[] | undefined > {
|
||||
// @ts-expect-error There are no types for this.
|
||||
const { getEditedEntityRecord } = select( 'core' );
|
||||
|
||||
const data: Product = getEditedEntityRecord( 'postType', postType, postId );
|
||||
|
||||
const options = {
|
||||
categories: data?.categories
|
||||
? data.categories.map( ( cat ) => cat.id )
|
||||
: [],
|
||||
tags: data?.tags ? data.tags.map( ( tag ) => tag.id ) : [],
|
||||
};
|
||||
|
||||
return await resolveSelect( PRODUCTS_STORE_NAME ).getSuggestedProducts(
|
||||
options
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue