Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Fix Product SKU not being displayed on frontend when outside Single P…
Browse files Browse the repository at this point in the history
…roduct template
  • Loading branch information
thealexandrelara committed May 11, 2023
1 parent 04cf1b3 commit bd5da9a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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 };
};
5 changes: 5 additions & 0 deletions assets/js/atomic/blocks/product-elements/sku/edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { useSelect } from '@wordpress/data';
*/
import Block from './block';
import type { Attributes } from './types';
import { useIsDescendentOfSingleProductBlock } from '../shared/use-is-descendent-of-single-product-block';

const Edit = ( {
attributes,
Expand All @@ -28,6 +29,8 @@ const Edit = ( {
...context,
};
const isDescendentOfQueryLoop = Number.isFinite( context.queryId );
const { isDescendentOfSingleProductBlock } =
useIsDescendentOfSingleProductBlock( { blockClientId: blockProps.id } );

const isDescendentOfSingleProductTemplate = useSelect(
( select ) => {
Expand All @@ -51,11 +54,13 @@ const Edit = ( {
setAttributes( {
isDescendentOfQueryLoop,
isDescendentOfSingleProductTemplate,
isDescendentOfSingleProductBlock,
} ),
[
setAttributes,
isDescendentOfQueryLoop,
isDescendentOfSingleProductTemplate,
isDescendentOfSingleProductBlock,
]
);

Expand Down
1 change: 1 addition & 0 deletions assets/js/atomic/blocks/product-elements/sku/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export interface Attributes {
productId: number;
isDescendentOfQueryLoop: boolean;
isDescendentOfSingleProductTemplate: boolean;
isDescendentOfSingleProductBlock: boolean;
showProductSelector: boolean;
isDescendantOfAllProducts: boolean;
}

0 comments on commit bd5da9a

Please sign in to comment.