function navetta_importa_singolo_prodotto($row) {
if (empty($row['A'])) return false;
$sku = trim($row['A']);
$post_id = wc_get_product_id_by_sku($sku);
// Titolo IT
$product_title = $row['G'] ?? $sku;
// Descrizione IT
$product_description = $row['K'] ?? '';
if (!$post_id) {
$post_id = wp_insert_post([
'post_title' => $product_title,
'post_content' => $product_description,
'post_status' => 'publish',
'post_type' => 'product',
]);
} else {
// Aggiorna titolo e descrizione
wp_update_post([
'ID' => $post_id,
'post_title' => $product_title,
'post_content' => $product_description,
]);
}
if (is_wp_error($post_id) || !$post_id) {
error_log("Errore creazione prodotto per SKU $sku");
return false;
}
// Tassonomie
if (!empty($row['B'])) {
wp_set_object_terms($post_id, [(int)$row['B']], 'product_cat');
}
if (!empty($row['C'])) {
wp_set_object_terms($post_id, [(int)$row['C']], 'product_tag');
}
// Prezzi e stock
update_post_meta($post_id, '_sku', $sku);
update_post_meta($post_id, '_stock', isset($row['E']) ? (int)$row['E'] : 0);
update_post_meta($post_id, '_manage_stock', 'yes');
update_post_meta($post_id, '_stock_status', 'instock');
update_post_meta($post_id, '_regular_price', isset($row['O']) ? (float)$row['O'] : 0);
update_post_meta($post_id, '_price', isset($row['O']) ? (float)$row['O'] : 0);
// Shipping class
$livello = strtolower(trim($row['F']));
if (!empty($livello)) {
$shipping_class = get_term_by('slug', $livello, 'product_shipping_class');
if ($shipping_class) {
wp_set_object_terms($post_id, [$shipping_class->term_id], 'product_shipping_class');
}
}
// Campi ACF opzionali (es. ebay price, posizione magazzino)
if (function_exists('update_field')) {
update_field('ebay_euro', $row['P'] ?? '', $post_id);
update_field('ebay_dollar', $row['Q'] ?? '', $post_id);
update_field('ebay_pound', $row['R'] ?? '', $post_id);
update_field('ebay_au_dollar', $row['S'] ?? '', $post_id);
update_field('posizione_magazzino', $row['AJ'] ?? '', $post_id);
}
// Immagini da U ad AF (Immagini 1 ? 12)
$image_columns = range('U', 'AF');
$image_urls = [];
foreach ($image_columns as $col) {
if (!empty($row[$col]) && filter_var($row[$col], FILTER_VALIDATE_URL)) {
// Trasforma ?dl=0 o ?raw=1 in ?dl=1
$clean_url = preg_replace('/\?.*/', '', trim($row[$col])) . '?dl=1';
$image_urls[] = $clean_url;
}
}
if (!empty($image_urls)) {
require_once ABSPATH . 'wp-admin/includes/image.php';
require_once ABSPATH . 'wp-admin/includes/file.php';
require_once ABSPATH . 'wp-admin/includes/media.php';
$gallery_ids = [];
foreach ($image_urls as $index => $url) {
$tmp = download_url($url);
if (is_wp_error($tmp)) continue;
$file_array = [
'name' => basename($url),
'tmp_name' => $tmp,
];
$id = media_handle_sideload($file_array, $post_id);
if (!is_wp_error($id)) {
$gallery_ids[] = $id;
if ($index === 0) {
set_post_thumbnail($post_id, $id);
}
} else {
@unlink($tmp);
}
}
if (!empty($gallery_ids)) {
update_post_meta($post_id, '_product_image_gallery', implode(',', $gallery_ids));
}
}
return true;
}
Azienda | Desmo Market | Pezzi di ricambio nuovi ed usati per moto Ducati
Vai al contenuto