我从一个answer to a previous question:
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 );
}
});
当我将图像添加到媒体库时,它将解决我将图像与现有产品匹配的问题。然而,我不知道我需要在wordpress的什么地方添加它。我对这一切都很陌生。有人能帮忙吗?