Last active
June 2, 2022 13:26
-
-
Save Faisalawanisee/da7e12bf1511583db5317d308f4a67fe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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