I’m trying to update the function that handles the upload of the image to variation product ![image variation] https://i.imgur.com/reNn6x7.png
I thought that the code that handles it is :
protected function set_variation_image( $variation, $image ) { $attachment_id = isset( $image['id'] ) ? absint( $image['id'] ) : 0; if ( 0 === $attachment_id && isset( $image['src'] ) ) { $upload = wc_rest_upload_image_from_url( esc_url_raw( $image['src'] ) ); if ( is_wp_error( $upload ) ) { if ( ! apply_filters( 'woocommerce_rest_suppress_image_upload_error', false, $upload, $variation->get_id(), array( $image ) ) ) { throw new WC_REST_Exception( 'woocommerce_variation_image_upload_error', $upload->get_error_message(), 400 ); } } $attachment_id = wc_rest_set_uploaded_image_as_attachment( $upload, $variation->get_id() ); } if ( ! wp_attachment_is_image( $attachment_id ) ) { /* translators: %s: attachment ID */ throw new WC_REST_Exception( 'woocommerce_variation_invalid_image_id', sprintf( __( '#%s is an invalid image ID.', 'woocommerce' ), $attachment_id ), 400 ); } $variation->set_image_id( $attachment_id ); // Set the image alt if present. if ( ! empty( $image['alt'] ) ) { update_post_meta( $attachment_id, '_wp_attachment_image_alt', wc_clean( $image['alt'] ) ); } // Set the image name if present. if ( ! empty( $image['name'] ) ) { wp_update_post( array( 'ID' => $attachment_id, 'post_title' => $image['name'], ) ); } return $variation; } `
inside the class-wc-rest-product-variations-controller.php
but any modification at this function is not changing the way that the file is uploaded.
the idea is to upload the image and apply the same variation image to each variation available, but can’t find the function that saves the image to the product.
@edit after itzmekhokan reply
i created a plugin that add action to woocommerce_save_product_variation
add_action('woocommerce_save_product_variation', 'salvarfoto',10,2); function salvarfoto($variation_id,$i){ $variation = new WC_Product_Variation($variation_id); $produto_pai =wc_get_product( $variation->get_parent_id() ); foreach( $produto_pai->get_children() as $variation_values ){ // $variation_id = $variation_values['variation_id']; // variation id $product = new WC_Product_Variation( $variation_values ); if( $product->get_attribute( 'pa_cor' ) == $variation->get_attribute('pa_cor')) { $id_img = $variation->get_image_id(); $product->set_image_id($id_img); } $product->save(); } }
this code apply the same image_id to all variations that has the same color attribute
Advertisement
Answer
Its WC_Meta_Box_Product_Data::save_variations( $post_id, $post )
within file class-wc-meta-box-product-data.php
.
And –
$errors = $variation->set_props( array( 'image_id' => isset( $_POST['upload_image_id'][ $i ] ) ? wc_clean( wp_unslash( $_POST['upload_image_id'][ $i ] ) ) : null, ) );
image_id
basically storing the each variation image id. Check the functionalities first, then I think you can do your modifications using this do_action( 'woocommerce_save_product_variation', $variation_id, $i );
hook.