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

Commit

Permalink
Product Gallery block: Add support for variation image updates (#11459)
Browse files Browse the repository at this point in the history
* Add `data-wc-init` directive to Interactivity API

* Add support for variation image updates on the Product Gallery block

* Watch correct form based on the product id

* Fix php cs error

* Fix php cs error

* Prevent adding wc-init to non-variable products
  • Loading branch information
thealexandrelara committed Oct 27, 2023
1 parent 68d8847 commit 483975d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
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' &&
currentImageAttribute &&
store.context.woocommerce.visibleImagesIds.includes(
currentImageAttribute
)
) {
store.context.woocommerce.selectedImage =
currentImageAttribute;
}
}
} );

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

return () => {
observer.disconnect();
};
},
keyboardAccess: ( store: Store ) => {
const { context, actions } = store;
let allowNavigation = true;
Expand Down
7 changes: 7 additions & 0 deletions src/BlockTypes/ProductGallery.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ 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 );
Expand All @@ -140,10 +141,16 @@ 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,
),
)
)
);

if ( $product->is_type( 'variable' ) ) {
$p->set_attribute( 'data-wc-init--watch-changes-on-add-to-cart-form', 'effects.woocommerce.watchForChangesOnAddToCartForm' );
}

$p->add_class( $classname );
$p->add_class( $classname_single_image );
$html = $p->get_updated_html();
Expand Down

0 comments on commit 483975d

Please sign in to comment.