WooCommerce Product Primary Image
€8-30 EUR
Pagado a la entrega
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 [url removed, login to view] 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.
Nº del proyecto: #6657974
Sobre el proyecto
Adjudicado a:
HI Steve, as per our discussion on skype i have edited my bid. and i am done with you project. looking forward. Michael
8 freelancers están ofertando un promedio de €42 por este trabajo
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