Home Forums WoodMart support forum URGENT – DATA NOT SAVED – ADDING VARIATION IMAGES GALLERY Reply To: URGENT – DATA NOT SAVED – ADDING VARIATION IMAGES GALLERY

#95954

Artem Temos
Keymaster

Try to add the following PHP code snippet to the child theme functions.php file

function woodmart_remove_unnecessary_vg_data( $post_id ) {
		if ( ! woodmart_woocommerce_installed() ) {
			return;
		}

		$product = wc_get_product( $post_id );

		if ( ! $product ) {
			return;
		}

		$available_variations = array();

		foreach ( $product->get_children() as $child_id ) {
			$available_variations[] = $product->get_available_variation( wc_get_product( $child_id ) );
		}

		$available_variations = array_values( array_filter( $available_variations ) );
		
		$variations             = $product->get_type() == 'variable' ? $available_variations : '';
		$variations_ids         = array();
		$variation_gallery_data = get_post_meta( $post_id, 'woodmart_variation_gallery_data', true );

		if ( ! $variations || ! $variation_gallery_data ) {
			return;
		}

		foreach ( $variations as $variation ) {
			$variations_ids[] = $variation['variation_id'];
		}

		foreach ( $variation_gallery_data as $key => $data ) {
			if ( ! in_array( $key, $variations_ids ) && isset( $variation_gallery_data[ $key ] ) ) {
				unset( $variation_gallery_data[ $key ] );
			}
		}

		update_post_meta( $post_id, 'woodmart_variation_gallery_data', $variation_gallery_data );
	}

	add_action( 'save_post', 'woodmart_remove_unnecessary_vg_data' );