Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Faisalawanisee/da7e12bf1511583db5317d308f4a67fe to your computer and use it in GitHub Desktop.
Save Faisalawanisee/da7e12bf1511583db5317d308f4a67fe to your computer and use it in GitHub Desktop.
<?php
function fab_woocommerce_product_importer_pre_expand_data($data)
{
$ext = "jpg";
$sku = $data['sku'];
$url = "https://sample.com/images/";
if (isset($data['images'])) {
$images = $data['images'];
foreach ($images as $index => $image) {
if (isValidURL($image)) {
$images[$index] = $image;
} else {
$images[$index] = $url . $image;
}
}
$data['raw_image_id'] = array_shift($images);
if (!empty($images)) {
$data['raw_gallery_image_ids'] = $images;
}
unset($data['images']);
} else {
$data['raw_image_id'] = $url . $sku . '.' . $ext;
}
return $data;
}
add_action('woocommerce_product_importer_pre_expand_data', 'fab_woocommerce_product_importer_pre_expand_data', 10, 2);
function isValidURL($url)
{
return preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*
(:[0-9]+)?(/.*)?$|i', $url);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment