I have a WordPress website using Woocommerce. I need to attach images to products automatically when uploaded through the Add Media. At the moment I have the following code:
add_action('add_attachment', function( $attachmentID ) {
if ( ! class_exists( 'WC_Product' ) ) return; // if no WooCommerce do nothing
// an attachment was jus saved, first of all get the file name
$src = wp_get_attachment_image_src( $attachmentID, 'full' );
$filename = pathinfo( $src[0], PATHINFO_FILENAME );
// now let's see if exits a product with the sku that match filename
$args = array(
'meta_key' => '_sku',
'meta_value' => $filename,
'post_type' => 'product',
'posts_per_page' => '1' // assuming sku is unique get only one post
);
$prods = get_posts( $args );
if ( ! empty($prods) ) {
// ok we have a match, exists a product having sku that match filename
$product = array_pop( $prods );
// set the thumbnail for the product
set_post_thumbnail( $product, $attachmentID );
// now "attach" the post to the product setting 'post_parent'
$attachment = get_post( $attachmentID );
$attachment->post_parent = $product->ID;
wp_update_post( $attachment );
}
});
It takes the name of the image and automatically attaches to the product with the matching SKU. For example [login to view URL] becomes automatically the Main Product Image of the product having the SKU set as 45rt10.
The problem is that is only working with products who are set as Simple Product. Variations (each having a unique SKU) - children of a Variable Product - is not having the images attached.
I need someone to start and finish today.
Hello,
I'm an experienced wordpress developer and familar with woocommerce. I can modify the automatic image attaching in such a way that it also works for product variations. I can make it within max a day after you select my bid.
Regards,
Tamas
Greetings, we are a team of dedicated professionals along with several years of experience in this field and provide original work. We have reviewed your requirements , have check your attachment and are ready to work on this project under the specified time and budget framework. Kindly consider our bid and provide us a chance to satisfy you with our services.
I will fix it in next 2 to 3 hours . Please give me admin details for this site and ftp . No worry it will be fixed as per your desire . Thanks in advance . Best Regards
Hello
we are in team having 2000+ hours of experience please come on chat to discuss more about the project to do this work in quick time.
we can start and end this work asap.
Thanks
softbyteinfotech