sync_products(); } } /** * Main function to fetch products from Kogan and synchronize them with WooCommerce. */ public function sync_products() { // API URL and credentials (update these with your actual values). $api_url = 'https://api.kogan.com/v1/products'; $api_key = 'YOUR_API_KEY'; // Replace with your actual API key. // Make the API request. $response = wp_remote_get( $api_url, array( 'headers' => array( 'Authorization' => 'Bearer ' . $api_key, 'Accept' => 'application/json' ), 'timeout' => 30, ) ); // Check for errors in the API request. if ( is_wp_error( $response ) ) { error_log( 'Advanced Kogan API Integration Error: ' . $response->get_error_message() ); return; } $body = wp_remote_retrieve_body( $response ); $data = json_decode( $body, true ); if ( empty( $data ) || ! isset( $data['products'] ) ) { error_log( 'Advanced Kogan API Integration Error: Invalid API response.' ); return; } // Loop through each product returned by the Kogan API. foreach ( $data['products'] as $product ) { // Extract and sanitize product details. $title = isset( $product['name'] ) ? sanitize_text_field( $product['name'] ) : 'Unnamed Product'; $price = isset( $product['price'] ) ? floatval( $product['price'] ) : 0; $description = isset( $product['description'] ) ? wp_kses_post( $product['description'] ) : ''; // Determine category mapping from API data. $kogan_category = isset( $product['category'] ) ? sanitize_text_field( $product['category'] ) : 'Uncategorized'; $woo_category_id = $this->get_or_create_category( $kogan_category ); // Use a unique identifier if available (e.g., $product['id']) instead of title for better matching. $existing_product = get_page_by_title( $title, OBJECT, 'product' ); if ( $existing_product ) { // Update the existing product. $product_id = $existing_product->ID; $updated_product = array( 'ID' => $product_id, 'post_title' => $title, 'post_content' => $description, ); wp_update_post( $updated_product ); update_post_meta( $product_id, '_price', $price ); update_post_meta( $product_id, '_regular_price', $price ); } else { // Insert a new product. $new_product = array( 'post_title' => $title, 'post_content' => $description, 'post_status' => 'publish', 'post_type' => 'product' ); $new_product_id = wp_insert_post( $new_product ); if ( $new_product_id ) { update_post_meta( $new_product_id, '_price', $price ); update_post_meta( $new_product_id, '_regular_price', $price ); $product_id = $new_product_id; } } // Associate the product with the appropriate WooCommerce category. if ( isset( $product_id ) ) { wp_set_object_terms( $product_id, intval( $woo_category_id ), 'product_cat', true ); } } } /** * Get or create a WooCommerce product category based on the provided Kogan category name. * * @param string $category_name The category name from Kogan. * @return int The WooCommerce product category ID. */ private function get_or_create_category( $category_name ) { $term = term_exists( $category_name, 'product_cat' ); if ( $term === 0 || $term === null ) { // Create the category if it doesn't exist. $term = wp_insert_term( $category_name, 'product_cat' ); if ( is_wp_error( $term ) ) { error_log( 'Error creating product category: ' . $term->get_error_message() ); return 0; } return $term['term_id']; } else { return $term['term_id']; } } } // Initialize the plugin. new Advanced_Kogan_API_WooCommerce();