post_id lookup $skuToPostId = []; $results = $wpdb->get_results( $wpdb->prepare( "SELECT pm.meta_value AS sku, pm.post_id FROM {$wpdb->postmeta} pm INNER JOIN {$wpdb->posts} p ON p.ID = pm.post_id WHERE pm.meta_key = '_sku' AND p.post_type = 'product_variation' AND pm.meta_value IN ($placeholders)", ...$allSkus ) ); foreach ($results as $row) { $skuToPostId[$row->sku] = (int) $row->post_id; } if (empty($skuToPostId)) { return JobResult::success(); } $postIds = array_values($skuToPostId); $idPlaceholders = implode(',', array_fill(0, count($postIds), '%d')); // 3. Batch meta pre-fetch $metaMap = []; $metaResults = $wpdb->get_results( $wpdb->prepare( "SELECT post_id, meta_key, meta_value FROM {$wpdb->postmeta} WHERE post_id IN ($idPlaceholders) AND meta_key IN ('brand_id', 'cat_ids')", ...$postIds ) ); foreach ($metaResults as $meta) { $metaMap[$meta->post_id][$meta->meta_key] = $meta->meta_value; } // 4. Process each item $updatedIds = []; foreach ($skuToPostId as $sku => $postId) { try { $basePrice = $skuPriceMap[$sku]; $brandId = (string) ($metaMap[$postId]['brand_id'] ?? ''); $catIds = json_decode($metaMap[$postId]['cat_ids'] ?? '[]', true) ?? []; $priceRecharger = new PriceRecharger($basePrice); $finalPrice = $priceRecharger->getPrice($brandId, $catIds); if ((float) $finalPrice > 0) { $formattedPrice = wc_format_decimal($finalPrice); update_post_meta($postId, '_regular_price', $formattedPrice); update_post_meta($postId, '_price', $formattedPrice); update_post_meta($postId, 'original_base_price', $basePrice); delete_post_meta($postId, '_needs_price_sync'); $wpdb->update( $wpdb->prefix . 'wc_product_meta_lookup', ['min_price' => $formattedPrice, 'max_price' => $formattedPrice], ['product_id' => $postId], ['%s', '%s'], ['%d'] ); $updatedIds[] = $postId; } } catch (\Throwable $e) { Log::write("BulkPriceBatch: Error processing SKU {$sku}: " . $e->getMessage(), Log::ERROR, Log::CONTEXT_CRONJOB); } } // 5. Sync parent product lookup tables + invalidate caches if (!empty($updatedIds)) { $parentIds = []; foreach ($updatedIds as $variationId) { $parentId = wp_get_post_parent_id($variationId); if ($parentId) { $parentIds[$parentId] = true; } wc_delete_product_transients($variationId); } foreach (array_keys($parentIds) as $parentId) { // Recompute the variable product's aggregated price range from its // variations. wc_update_product_lookup_tables_column() takes a single // COLUMN argument (it rebuilds that column for ALL products), so calling // it with a product id was a no-op and left parent min/max prices stale. $parent = wc_get_product($parentId); if ($parent && $parent->is_type('variable')) { \WC_Data_Store::load('product-variable')->sync_price($parent); } wc_delete_product_transients($parentId); } } // On final batch, reset cache and update timestamp if (!JobQueue::hasPending('bulk_price_batch', (int) $job->id)) { PriceRecharger::resetCache(); update_option('_ACTIONWEAR_LAST_BULK_PRICE_SYNC_COMPLETED', time()); Log::write("BulkPriceBatch: All batches completed", Log::INFO, Log::CONTEXT_CRONJOB); } return JobResult::success(); } }