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

Product Gallery block: Add support for variation image updates #11459

Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 36 additions & 0 deletions assets/js/blocks/product-gallery/frontend.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ interface Context {
imageId: string;
visibleImagesIds: string[];
isDialogOpen: boolean;
productId: string;
};
}

Expand Down Expand Up @@ -63,6 +64,41 @@ interactivityApiStore( {
state: {},
effects: {
woocommerce: {
watchForChangesOnAddToCartForm: ( store: Store ) => {
const variableProductCartForm = document.querySelector(
`form[data-product_id="${ store.context.woocommerce.productId }"]`
);

if ( ! variableProductCartForm ) {
return;
}

const observer = new MutationObserver( function ( mutations ) {
for ( const mutation of mutations ) {
const mutationTarget = mutation.target as HTMLElement;
const currentImageAttribute =
mutationTarget.getAttribute( 'current-image' );
if (
mutation.type === 'attributes' &&
thealexandrelara marked this conversation as resolved.
Show resolved Hide resolved
currentImageAttribute &&
store.context.woocommerce.visibleImagesIds.includes(
currentImageAttribute
)
) {
store.context.woocommerce.selectedImage =
currentImageAttribute;
}
}
} );

observer.observe( variableProductCartForm, {
attributes: true,
} );

return () => {
observer.disconnect();
thealexandrelara marked this conversation as resolved.
Show resolved Hide resolved
};
},
keyboardAccess: ( store: Store ) => {
const { context, actions } = store;
let allowNavigation = true;
Expand Down
10 changes: 10 additions & 0 deletions assets/js/interactivity/directives.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ export default () => {
}
);

// data-wc-init--[name]
directive( 'init', ( { directives: { init }, context, evaluate } ) => {
const contextValue = useContext( context );
Object.values( init ).forEach( ( path ) => {
useEffect( () => {
return evaluate( path, { context: contextValue } );
}, [] );
} );
} );

// data-wc-on--[event]
directive( 'on', ( { directives: { on }, element, evaluate, context } ) => {
const contextValue = useContext( context );
Expand Down
3 changes: 3 additions & 0 deletions src/BlockTypes/ProductGallery.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,14 @@ protected function render( $attributes, $content, $block ) {
$dialog = ( true === $attributes['fullScreenOnClick'] && isset( $attributes['mode'] ) && 'full' !== $attributes['mode'] ) ? $this->render_dialog() : '';
$post_id = $block->context['postId'] ?? '';
$product = wc_get_product( $post_id );
$product_id = strval( $product->get_id() );

$html = $this->inject_dialog( $content, $dialog );
$p = new \WP_HTML_Tag_Processor( $html );

if ( $p->next_tag() ) {
$p->set_attribute( 'data-wc-interactive', true );
$p->set_attribute( 'data-wc-init--watch-changes-on-add-to-cart-form', 'effects.woocommerce.watchForChangesOnAddToCartForm' );
thealexandrelara marked this conversation as resolved.
Show resolved Hide resolved
$p->set_attribute(
'data-wc-context',
wp_json_encode(
Expand All @@ -140,6 +142,7 @@ protected function render( $attributes, $content, $block ) {
'selectedImage' => $product->get_image_id(),
'visibleImagesIds' => ProductGalleryUtils::get_product_gallery_image_ids( $product, $number_of_thumbnails, true ),
'isDialogOpen' => false,
'productId' => $product_id,
),
)
)
Expand Down