$item['sku'] ?? null, 'name' => $item['name'] ?? '', 'price' => (float) ($item['price'] ?? 0), 'stock' => (int) ($item['stock'] ?? 0), 'description' => $item['description'] ?? '', 'short_description' => $item['short_description'] ?? '', 'brand' => $item['brand'] ?? null, 'categories' => self::map_categories($item, $category_map), 'images' => self::map_images($item), ]; } protected static function map_categories( array $item, array $category_map ): array { if (empty($item['categories']) || !is_array($item['categories'])) { return []; } $wp_category_ids = []; foreach ($item['categories'] as $cat) { if (empty($cat['id'])) { continue; } $external_id = (string) $cat['id']; // map external id → wp term id if (isset($category_map[$external_id])) { $wp_category_ids[] = $category_map[$external_id]; } else { // log category chưa map được (rất quan trọng) WPI_Logger::log( "[CATEGORY_MAP] Missing category external_id={$external_id} ({$cat['name']})" ); } } return array_values(array_unique($wp_category_ids)); } protected static function map_images(array $item): array { if (empty($item['images']) || !is_array($item['images'])) { return []; } $default = []; $others = []; foreach ($item['images'] as $img) { if (empty($img['url'])) { continue; } if (!empty($img['is_default'])) { $default[] = $img['url']; } else { $others[] = $img['url']; } } // default image lên trước return array_merge($default, $others); } }