我正在使用Wordpress Woocommerce插件,需要下载并将图像添加到产品图像,同样的图像也可以添加到产品库。
将图像添加到“产品图像”可以正常工作,但添加到“产品图库”却不行。在产品编辑页面上手动将相同图像添加到图库可使图库可见。
缺少什么,以便不添加产品图库图像?
用于执行此操作的代码是:
$uploaded_image_path = $uploads['path'] . "/tyreimage_$imageId.jpg";
copy("http://tyreimagesource/images/tyres/$imageId.jpg", $uploaded_image_path);
$wp_filetype = wp_check_filetype($uploaded_image_path, null);
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => $imageId . ".jpg",
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment($attachment, $uploaded_image_path, $productId);
require_once(ABSPATH . 'wp-admin/includes/image.php');
$attach_data = wp_generate_attachment_metadata($attach_id, $uploaded_image_path);
wp_update_attachment_metadata($attach_id, $attach_data);
set_post_thumbnail($productId, $attach_id);
add_post_meta($productId, '_product_image_gallery', $attach_id);
运行此代码后,我看到了
手动添加相同图像后,我看到了