Skip to content

Instantly share code, notes, and snippets.

@webtoffee-git
Created February 13, 2020 05:56
Show Gist options
  • Select an option

  • Save webtoffee-git/3521b47a20e1ffef0ec7eb6133d56839 to your computer and use it in GitHub Desktop.

Select an option

Save webtoffee-git/3521b47a20e1ffef0ec7eb6133d56839 to your computer and use it in GitHub Desktop.
Import product images by WooCommerce additional variation images - WebToffee Product Import Export for WooCommerce
function webtoffee_import_additional_product_variation_images($post_data = array()) {
$key = array_search('woo_variation_gallery_images', array_column($post_data['postmeta'], 'key'));
if (isset($key) && !empty($key) && isset($post_data['postmeta'][$key]['value']) && !empty($post_data['postmeta'][$key]['value'])) {
$imge_urls = explode(',', $post_data['postmeta'][$key]['value']);
if ($imge_urls) {
foreach ($imge_urls as $url) {
$filename = substr(basename($url), 0, -5);
$post = array(
'post_title' => $filename,
'post_status' => 'inherit',
);
$upload = WF_ProdImpExpCsv_Product_Import::fetch_remote_file($url, $post);
if (!is_wp_error($upload)) {
if ($info = wp_check_filetype($upload['file']))
$post['post_mime_type'] = $info['type'];
$post['guid'] = $upload['url'];
$attachment_id = wp_insert_attachment($post, $upload['file']);
if (!is_wp_error($attachment_id) && $attachment_id > 0) {
$attachment_ids[] = $attachment_id;
}
}
}
}
if (!empty($attachment_ids)) {
$post_data['postmeta'][$key]['value'] = $attachment_ids;
}
}
return $post_data;
}
add_filter('hf_alter_product_import_data', 'webtoffee_import_additional_product_variation_images');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment