add_action('wp_ajax_bpu_ultra_update', function(){ global $wpdb; if(!current_user_can('manage_options')) wp_send_json_error('No permission'); $rows = isset($_POST['rows']) ? json_decode(stripslashes($_POST['rows']), true) : []; if(!$rows || !is_array($rows)) wp_send_json_error('Tušti duomenys'); $mapping = isset($_POST['mapping']) ? json_decode(stripslashes($_POST['mapping']), true) : get_option('bpu_ultra_mapping',[]); $lookup_csv_col = $mapping['lookup_sku'] ?? ''; if(!$lookup_csv_col) wp_send_json_error('Trūksta Lookup: SKU'); $cancel_flag = get_option('bpu_ultra_cancel_flag', false); if($cancel_flag) wp_send_json_error('Importas sustabdytas'); $batch_size = 200; $batches = array_chunk($rows, $batch_size); // Surenkame visus pa_pristatymo-laikas priskyrimus batchui $taxonomy_assignments = []; foreach($batches as $batch){ $meta_cases = []; $post_ids = []; foreach($batch as $row){ $sku_val = trim($row[$lookup_csv_col] ?? ''); $sku_val = remove_utf8_bom($sku_val); $sku_val = preg_replace('/\x{00a0}/u', '', $sku_val); $sku_val = trim(mb_convert_encoding($sku_val, 'UTF-8', 'auto')); if($sku_val === '') { error_log("Tuščias SKU eilutėje: ".print_r($row,true)); continue; } $pid = wc_get_product_id_by_sku($sku_val); if(!$pid){ error_log("Produktas nerastas SKU: $sku_val"); continue; } $post_ids[] = $pid; $stock_val = null; $rpesp_meta = []; foreach($mapping as $wc_field => $csv_col){ if($wc_field === 'lookup_sku' || $csv_col === '') continue; $val = trim($row[$csv_col] ?? ''); $val = remove_utf8_bom($val); $val = trim(mb_convert_encoding($val, 'UTF-8', 'auto')); $val = preg_replace('/\x{00a0}/u', '', $val); $val = str_replace(',', '.', $val); if($val === '') error_log("Tuščias laukas: SKU=$sku_val, field=$wc_field, csv_col=$csv_col"); switch($wc_field){ case '_price': case '_regular_price': $meta_cases[$wc_field][$pid] = floatval($val); break; case '_stock': $stock_val = intval($val); break; case '_alg_ean': $ean = preg_replace('/\D/','',$val); if($ean !== '') $meta_cases['_alg_ean'][$pid] = $ean; break; case 'esttime': $rpesp_meta['esttime'] = $val !== null ? $val : ''; break; case 'taxonomy:pa_pristatymo-laikas': $taxonomy_assignments[$pid] = trim($val); break; default: $meta_cases[$wc_field][$pid] = $val; break; } } // Stock update if($stock_val !== null){ $meta_cases['_stock'][$pid] = $stock_val; $meta_cases['_stock_status'][$pid] = $stock_val > 0 ? 'instock' : 'outofstock'; $meta_cases['_manage_stock'][$pid] = 'yes'; } // rpesp-meta-setting update if($rpesp_meta){ $existing = get_post_meta($pid,'rpesp-meta-setting',true); $existing = maybe_unserialize($existing); if(!is_array($existing)) $existing = []; $merged = array_merge($existing, $rpesp_meta); $meta_cases['rpesp-meta-setting'][$pid] = maybe_serialize($merged); error_log("RPESP meta updated SKU=$sku_val: ".print_r($merged,true)); } } // SQL update per CASE foreach($meta_cases as $meta_key => $cases){ if(!$cases) continue; $ids = implode(',', array_map('intval', array_keys($cases))); $case_sql = []; foreach($cases as $pid => $val){ if(is_numeric($val)) $case_sql[] = "WHEN $pid THEN $val"; else $case_sql[] = "WHEN $pid THEN '".$wpdb->_real_escape($val)."'"; } $case_sql = implode(' ', $case_sql); $wpdb->query(" UPDATE {$wpdb->postmeta} SET meta_value = CASE post_id $case_sql END WHERE meta_key = '$meta_key' AND post_id IN ($ids) "); } // Batch update post_modified if($post_ids){ $ids_str = implode(',', array_map('intval', $post_ids)); $wpdb->query("UPDATE {$wpdb->posts} SET post_modified=NOW() WHERE ID IN ($ids_str)"); } } // ------------------------ // pa_pristatymo-laikas batch update (tik produkto lygyje) // ------------------------ if(!empty($taxonomy_assignments)){ $taxonomy = 'pa_pristatymo-laikas'; $attr_slug = 'pristatymo-laikas'; $attributes = wc_get_attribute_taxonomies(); $exists = false; foreach($attributes as $attr){ if($attr->attribute_name === $attr_slug){ $exists = true; break; } } if(!$exists && function_exists('wc_create_attribute')){ $attribute_id = wc_create_attribute([ 'name' => 'Pristatymo laikas', 'slug' => $attr_slug, 'type' => 'select', 'order_by' => 'menu_order', 'has_archives' => false ]); if($attribute_id && !taxonomy_exists($taxonomy)){ register_taxonomy($taxonomy,'product',[ 'label' => 'Pristatymo laikas', 'hierarchical' => false, 'public' => true, 'show_ui' => true, 'show_in_nav_menus' => false, 'show_admin_column' => true, 'rewrite' => ['slug'=>$attr_slug] ]); } } $term_slugs = []; foreach(array_unique($taxonomy_assignments) as $term_name){ $term_slug = sanitize_title($term_name); if(!term_exists($term_slug,$taxonomy)){ wp_insert_term($term_name,$taxonomy,['slug'=>$term_slug]); } $term_slugs[$term_name] = $term_slug; } foreach($taxonomy_assignments as $pid => $term_name){ $term_slug = $term_slugs[$term_name]; $term_id = $wpdb->get_var($wpdb->prepare("SELECT term_id FROM {$wpdb->terms} WHERE slug=%s", $term_slug)); $term_tax_id = $wpdb->get_var($wpdb->prepare("SELECT term_taxonomy_id FROM {$wpdb->term_taxonomy} WHERE term_id=%d AND taxonomy=%s", $term_id, $taxonomy)); if($term_tax_id){ $wpdb->query($wpdb->prepare(" DELETE tr FROM {$wpdb->term_relationships} tr JOIN {$wpdb->term_taxonomy} tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tr.object_id = %d AND tt.taxonomy = %s ", $pid, $taxonomy)); $wpdb->insert($wpdb->term_relationships,[ 'object_id'=>$pid, 'term_taxonomy_id'=>$term_tax_id ]); } $existing_attrs = get_post_meta($pid, '_product_attributes', true); if(!is_array($existing_attrs)) $existing_attrs = []; $existing_attrs[$taxonomy] = [ 'name' => $taxonomy, 'value' => '', 'position' => 0, 'is_visible' => 0, 'is_variation' => 0, 'is_taxonomy' => 1 ]; update_post_meta($pid,'_product_attributes',$existing_attrs); } } wc_delete_product_transients(); wp_send_json_success(['count'=>count($rows)]); });