Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save trey8611/b9c0a6f6218c43608dbf818284e9558d to your computer and use it in GitHub Desktop.

Select an option

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 );
@dauclair951
Copy link

dauclair951 commented Mar 30, 2022

Thank you for this Gist, it is working great to allow me to import additional variation images into Woodmart using WP All Import. I am having one problem with it, and I am a novice at this so please bear with me as I am trying to understand how this code works.

The first image of each product variation is being imported and displayed twice. Is there a way to exclude the first image from the Woodmart Variation Image Gallery? I tried to modify the code to skip the first instance, but have had no results. I'm afraid I just don't understand what is going on.

I attached some screenshots of what it looks like on my Woocommerce edit screen and the front-end product page. Any insight you can provide would be greatly appreciated, thanks!

Screenshot 2022-03-30 031637
Screenshot 2022-03-30 031631

@dauclair951
Copy link

WP All Import support helped out, adding this line before the call to update_post_meta() fixed my issue:

// filter out featured images from variation galleries.
$gallery[ $variationID ] = implode(',', array_diff( explode(',', $gallery[ $variationID ]), get_post_meta( $variationID, '_thumbnail_id', false) ));

@salihenesalkan
Copy link

I get this error when i trying to import products: Your server terminated the import process
When i delete this codes on my functions file it's successfuly importing. What can i do for that problem? Thanks...
image

@dauclair951
Copy link

It might be a similar issue as I had, WP All Import support shared this with me and it fixed it:

Change the following:

$variationID_first = get_post_meta( $product->get_id(), XmlImportWooCommerceService::FIRST_VARIATION, TRUE );

To this:

$variationID_first = get_post_meta( $product->get_id(), \wpai_woocommerce_add_on\XmlImportWooCommerceService::FIRST_VARIATION, TRUE );

@salihenesalkan
Copy link

It might be a similar issue as I had, WP All Import support shared this with me and it fixed it:

Change the following:

$variationID_first = get_post_meta( $product->get_id(), XmlImportWooCommerceService::FIRST_VARIATION, TRUE );

To this:

$variationID_first = get_post_meta( $product->get_id(), \wpai_woocommerce_add_on\XmlImportWooCommerceService::FIRST_VARIATION, TRUE );

It worked!! Thanks, i really appreciate it. I have one more question if you know. I can't change color in single product page without select the size variation. How can i fix that?

@NerQuiLes
Copy link

NerQuiLes commented Dec 16, 2022

Thanks for this code. Thanks, thanks, thanks !!!!

The complete code is this:

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(), 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 ) ) );

// 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