Fix Product SKU not being displayed on frontend when outside Single Product template (https://github.com/woocommerce/woocommerce-blocks/pull/9446)

This commit is contained in:
Alexandre Lara 2023-05-16 15:25:34 -03:00 committed by GitHub
parent f8d46570ae
commit 90573156fe
3 changed files with 36 additions and 0 deletions

View File

@ -0,0 +1,30 @@
/**
* External dependencies
*/
import { useSelect } from '@wordpress/data';
interface UseIsDescendentOfSingleProductBlockProps {
blockClientId: string;
}
export const useIsDescendentOfSingleProductBlock = ( {
blockClientId,
}: UseIsDescendentOfSingleProductBlockProps ) => {
const { isDescendentOfSingleProductBlock } = useSelect(
( select ) => {
const { getBlockParentsByBlockName } =
select( 'core/block-editor' );
const blockParentBlocksIds = getBlockParentsByBlockName(
blockClientId?.replace( 'block-', '' ),
[ 'woocommerce/single-product' ]
);
return {
isDescendentOfSingleProductBlock:
blockParentBlocksIds.length > 0,
};
},
[ blockClientId ]
);
return { isDescendentOfSingleProductBlock };
};

View File

@ -13,6 +13,7 @@ import { useSelect } from '@wordpress/data';
*/ */
import Block from './block'; import Block from './block';
import type { Attributes } from './types'; import type { Attributes } from './types';
import { useIsDescendentOfSingleProductBlock } from '../shared/use-is-descendent-of-single-product-block';
const Edit = ( { const Edit = ( {
attributes, attributes,
@ -28,6 +29,8 @@ const Edit = ( {
...context, ...context,
}; };
const isDescendentOfQueryLoop = Number.isFinite( context.queryId ); const isDescendentOfQueryLoop = Number.isFinite( context.queryId );
const { isDescendentOfSingleProductBlock } =
useIsDescendentOfSingleProductBlock( { blockClientId: blockProps.id } );
const isDescendentOfSingleProductTemplate = useSelect( const isDescendentOfSingleProductTemplate = useSelect(
( select ) => { ( select ) => {
@ -51,11 +54,13 @@ const Edit = ( {
setAttributes( { setAttributes( {
isDescendentOfQueryLoop, isDescendentOfQueryLoop,
isDescendentOfSingleProductTemplate, isDescendentOfSingleProductTemplate,
isDescendentOfSingleProductBlock,
} ), } ),
[ [
setAttributes, setAttributes,
isDescendentOfQueryLoop, isDescendentOfQueryLoop,
isDescendentOfSingleProductTemplate, isDescendentOfSingleProductTemplate,
isDescendentOfSingleProductBlock,
] ]
); );

View File

@ -2,6 +2,7 @@ export interface Attributes {
productId: number; productId: number;
isDescendentOfQueryLoop: boolean; isDescendentOfQueryLoop: boolean;
isDescendentOfSingleProductTemplate: boolean; isDescendentOfSingleProductTemplate: boolean;
isDescendentOfSingleProductBlock: boolean;
showProductSelector: boolean; showProductSelector: boolean;
isDescendantOfAllProducts: boolean; isDescendantOfAllProducts: boolean;
} }