fetch_option( Option::FLUSH_PRODUCTS ) ) { $this->remove_all_airalo_products( $options ); } $environment = $options->get_environment(); $sync_images = $options->fetch_option( Option::SYNC_IMAGES ); $settings = [ 'setting_create' => $options->fetch_option( Option::AUTO_PUBLISH ), 'setting_update' => $options->fetch_option( Option::AUTO_PUBLISH_AFTER_UPDATE ), 'setting_name' => $options->fetch_option( Option::USE_AIRALO_SIM_NAME ), 'update_product_title' => $options->fetch_option( Option::UPDATE_PRODUCT_TITLE ) !== Option::DISABLED, 'update_product_description'=> $options->fetch_option( Option::UPDATE_PRODUCT_DESCRIPTION ) !== Option::DISABLED, ]; try { $airalo_products = $this->fetch_airalo_products(); $client = ( new AiraloClient( $options ) )->getClient(); $allPackages = $client->getSimPackages(); $data = $allPackages->data; $options->insert_option( Option::LAST_SYNC, gmdate( 'Y-m-d H:i:s' ) ); $error = ''; if ( ! $data ) { $error = 'No data fetched. Please check your credentials.'; } $image_ids_by_operator = []; foreach ( $data as $item ) { foreach ( $item->operators as $operator ) { $image_id = null; if ( Option::ENABLED == $sync_images ) { if ( !isset( $image_ids_by_operator[ $operator->id ] ) ) { $term = new Term(); $term = $term->fetch_or_create_image_term( $operator ); $image_ids_by_operator[ $operator->id ] = get_term_meta( $term->term_id, Term::IMAGE_METADATA_KEY, true ); } $image_id = $image_ids_by_operator[ $operator->id ]; } foreach ( $operator->packages as $package ) { $product = new Product(); $product->update_or_create( $settings, $package, $operator, $item, $image_id, $environment, $airalo_products ); } } } $this->check_stock( $airalo_products ); $options->insert_option( Option::LAST_SUCCESSFUL_SYNC, gmdate( 'Y-m-d H:i:s' ) ); } catch ( \Exception $ex ) { $error_message = wp_strip_all_tags( $ex->getMessage() ); $error = " failed due to: `$error_message` on `$environment` environment"; error_log( $ex->getMessage() ); } finally { add_filter( 'woocommerce_background_image_regeneration', '__return_true' ); } $options->insert_option( Option::SYNC_ERROR, $error ); } private function check_stock( $airalo_products ) { foreach ( $airalo_products as $airalo_product ) { $product = $airalo_product['product']; $stock_status = Product::IN_STOCK; if ( ! isset ( $airalo_product['processed'] ) ) { $stock_status = Product::OUT_OF_STOCK; } $product->set_stock_status( $stock_status ); $product->save(); } } private function fetch_airalo_products() { $products_by_sku = []; $query = $this->get_airalo_products_query(); if ( $query->have_posts() ) { while ( $query->have_posts() ) { $query->the_post(); $product = wc_get_product( get_the_ID() ); $products_by_sku[$product->get_sku()] = ['product' => $product]; } wp_reset_postdata(); } return $products_by_sku; } private function get_airalo_products_query(): \WP_Query { $args = [ 'post_type' => 'product', 'posts_per_page' => -1, 'meta_query' => [ [ 'key' => '_sku', 'value' => Product::SKU_PREFIX, 'compare' => 'LIKE', ], ], ]; return new \WP_Query( $args ); } private function remove_all_airalo_products( Option $option ) { $query = $this->get_airalo_products_query(); if ( $query->have_posts() ) { while ( $query->have_posts() ) { $query->the_post(); $product_id = get_the_ID(); $product = wc_get_product( $product_id ); $taxonomy_name = Term::IMAGE_NAME_PREFIX . $product->get_attribute( 'operator_id' ); $term_name = $taxonomy_name . '_id'; $term = get_term_by( 'slug', $term_name, $taxonomy_name ); if ( $term && ! is_wp_error( $term ) ) { $image_id = get_term_meta( $term->term_id, Term::IMAGE_METADATA_KEY, true ); wp_delete_term( $term->term_id, $taxonomy_name ); if ( $image_id ) { wp_delete_post( $image_id, true ); } } wp_delete_post( $product_id, true ); } wp_reset_postdata(); } } }