Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save trey8611/b9c0a6f6218c43608dbf818284e9558d to your computer and use it in GitHub Desktop.
Save trey8611/b9c0a6f6218c43608dbf818284e9558d to your computer and use it in GitHub Desktop.
[WoodMart Additional Variation Images]
function wpai_woodmart_gallery_image_imported( $post_id, $att_id, $filepath, $is_keep_existing_images = '' ) {
	$product = wc_get_product( $post_id );
	if ( ! $product ) return;

	$variationID_first = get_post_meta( $product->get_id(), wpai_woocommerce_add_on\XmlImportWooCommerceService::FIRST_VARIATION, TRUE );
	if ( $product->is_type( 'variation' ) || ! empty( $variationID_first ) ) {
		if ( empty( $variationID_first ) ) {
			$variationID = $product->get_id();
			$parent      = $product->get_parent_id();
		} else {
			$variationID = $variationID_first;
			$parent      = $product->get_id();
		}

		$gallery = get_post_meta( $parent, 'woodmart_variation_gallery_data', true );
		if ( empty( $gallery ) ) {
			$gallery = array();
		} else {
			$gallery = maybe_unserialize( $gallery );
		}

		if ( array_key_exists( $variationID, $gallery ) ) {
			$existing = explode( ',', $gallery[ $variationID ] );
			$existing[] = $att_id;
			$gallery[ $variationID ] = implode( ',', $existing );
		} else {
			$gallery[ $variationID ] = $att_id;
		}
		
		// filter out featured images from variation galleries.
		$gallery[ $variationID ] = implode( ',', array_diff( explode( ',', $gallery[ $variationID ] ), get_post_meta( $variationID, '_thumbnail_id', false ) ) );

		update_post_meta( $variationID, 'wd_additional_variation_images_data', $gallery[ $variationID ] );
		update_post_meta( $parent, 'woodmart_variation_gallery_data', $gallery );
	}
}
add_action( 'pmxi_gallery_image', 'wpai_woodmart_gallery_image_imported', 10, 4 );
@asify3409
Copy link

Hi, I have added this code to function.php and trying to import new variable products. Everything is working fine but variation gallery images are not being imported. Am I supposed to map any field with woodmart_variation_gallery_data in the custom field or it will automatically import images?

@NerQuiLes @salihenesalkan @dauclair951 @trey8611

@walpap
Copy link

walpap commented May 3, 2024

@trey8611 THANKS! You saved my life.

@NerQuiLes you should update the line that @dauclair951 mentioned above to make it work.

@asify3409 it works without adding any custom field. Just paste the code in your functions.php

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment