Home Forums WoodMart support forum Woodmart & WP All Import & Variations Images Gallery

Woodmart & WP All Import & Variations Images Gallery

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #429294

    McQueen
    Participant

    Hello,

    I have been reviewing the forum posts but I have not found a solution to this problem I am having when importing my images and associating them with a specific variation to create a gallery.

    In the documentation for WP All Import, they propose a solution with the following code, but I am unable to get it to work.

    Could you review the following code and see what might be wrong?

    https://www.wpallimport.com/documentation/code-snippets/

    
    function gallery_ids_in_string($post_id, $att_id, $filepath, $is_keep_existing_images = '')
    {
        // The custom field used by gallery.
        $key = '_woodmart_variation_gallery_data';
    
        // The separator to use between each ID.
        $separator = ",";
    
        // Retrieve the current values in the gallery field.
        $gallery = get_post_meta($post_id, $key, true);
    
        // Ensure gallery is valid.
        if (is_string($gallery) || is_empty($gallery) || ($gallery == false)) {
    
            // Split value into array.
            $gallery = explode($separator, $gallery);
    
            // Add image if it's not in the gallery.
            if (!in_array($att_id, $gallery)) {
    
                // Ensure array doesn't start with empty value.
                if ($gallery[0] == '') unset($gallery[0]);
    
                // Add image ID to array.
                $gallery[] = $att_id;
    
                // Save updated gallery field.
                update_post_meta($post_id, $key, implode($separator, $gallery));
    
            }
        }
    }
    
    add_action('pmxi_gallery_image', 'gallery_ids_in_string', 10, 4);

    I look forward to your news. Thank you.

    • This topic was modified 2 years, 7 months ago by McQueen.
    #429396

    Artem Temos
    Keymaster

    Hello,

    Sorry, but we don’t know what this code actually is supposed to do. You need to consult with a plugin developer for this matter. If you need any information about how our theme stores the variations images data just let us know and we will provide you with all the necessary info.

    Kind Regards

    #429442

    McQueen
    Participant

    HI,

    Thank you for your response. I am very interested in understanding how this code works and I would appreciate it if you could take a closer look at the code to see if you can provide any insight. I know that there are many people in the community who are also interested in getting this to work, so any information you can provide would be greatly appreciated. Thank you for offering to provide information about how your theme stores variations images data. If you have any additional information that could be helpful, I would be very interested in hearing about it. Thank you for your assistance.

    This code defines a function called gallery_ids_in_string that takes four parameters: $post_id, $att_id, $filepath, and $is_keep_existing_images.

    Within the function, a variable called $key is declared that stores the value ‘_gallery’, which will be used as a custom field for the gallery. A variable called $separator is also declared that stores the value “,”, which will be used as a separator between each ID.

    Next, the get_post_meta function is used to retrieve the current values of the gallery field for the post with ID $post_id. These values are stored in the variable $gallery.

    Then, it is checked if $gallery is a string, is empty, or is false. If any of these conditions are met, the value of $gallery is split into an array using the $separator separator.

    Next, the image ID, stored in $att_id, is checked to see if it is already in the gallery. If it is not, it is added to the $gallery array. The array is then ensured to not start with an empty value and the image ID is added to the array.

    Finally, the update_post_meta function is used to save the updated gallery field with the value of the $gallery array, using the $separator separator to join each element of the array into a string.

    Finally, add_action is used to add the gallery_ids_in_string function as an action that will be executed when the pmxi_gallery_image hook is activated, and the four necessary parameters for the function are passed to it.

    #429484

    McQueen
    Participant

    HI,

    I have a Solution:

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

    Thanks to: https://gist.github.com/trey8611/b9c0a6f6218c43608dbf818284e9558d

    Explication:
    “wpai_woodmart_gallery_image_imported” is a callback function that is executed when an image is imported to a gallery using the WP All Import plugin. It takes four parameters:

    $post_id: the ID of the post where the image is being inserted.
    $att_id: the ID of the image being inserted.
    $filepath: the file path to the image being inserted.
    $is_keep_existing_images: a flag indicating whether to keep existing images in the gallery (if true, keep them; if false, overwrite them).
    The function first checks if the post where the image is being inserted is a WooCommerce product variation or if the product has a variation assigned. If either of these conditions are met, it gets the variation’s ID and the parent product’s ID. Then, it retrieves the current value of the woodmart_variation_gallery_data field of the parent product and unserializes it.

    Next, it checks if the current variation already has an entry in the variation gallery array. If it does, it adds the image’s ID to the list of image IDs for the variation. If not, it adds a new entry to the array with the variation’s ID and the image’s ID.

    Finally, the function updates the woodmart_variation_gallery_data field of the parent product and the wd_additional_variation_images_data field of the variation with the updated variation gallery array. It also filters out featured images from the variation galleries.

    #429803

    Artem Temos
    Keymaster

    Hello,

    We are glad that you sorted it out and shared your solution here. We hope it will be useful for other customers.

    Kind Regards

Viewing 5 posts - 1 through 5 (of 5 total)