Skip to content

Instantly share code, notes, and snippets.

@ali-cedcoss
Last active December 28, 2021 08:12
Show Gist options
  • Save ali-cedcoss/5cd6d8a33a2ebbd257f568ab76a8073f to your computer and use it in GitHub Desktop.
Save ali-cedcoss/5cd6d8a33a2ebbd257f568ab76a8073f to your computer and use it in GitHub Desktop.
async inventory update
### settings-view.php code changes

if ( isset( $_POST['ced_ebay_setting_nonce'] ) && wp_verify_nonce( sanitize_text_field( $_POST['ced_ebay_setting_nonce'] ), 'ced_ebay_setting_page_nonce' ) ) {
	if ( isset( $_POST['global_settings'] ) ) {
		$settings             = array();
		$sanitized_array      = filter_input_array( INPUT_POST, FILTER_SANITIZE_STRING );
		$settings             = get_option( 'ced_ebay_global_settings', array() );
		$settings[ $user_id ] = isset( $sanitized_array['ced_ebay_global_settings'] ) ? ( $sanitized_array['ced_ebay_global_settings'] ) : array();
		update_option( 'ced_ebay_global_settings', $settings );

		$inventory_schedule       = isset( $sanitized_array['ced_ebay_global_settings']['ced_ebay_inventory_schedule_info'] ) && '0' != $sanitized_array['ced_ebay_global_settings']['ced_ebay_inventory_schedule_info'] ? ( $sanitized_array['ced_ebay_global_settings']['ced_ebay_inventory_schedule_info'] ) : as_unschedule_all_actions( 'ced_ebay_inventory_scheduler_job_' . $user_id );
		$order_schedule           = isset( $sanitized_array['ced_ebay_global_settings']['ced_ebay_order_schedule_info'] ) && '0' != $sanitized_array['ced_ebay_global_settings']['ced_ebay_order_schedule_info'] ? ( $sanitized_array['ced_ebay_global_settings']['ced_ebay_order_schedule_info'] ) : wp_clear_scheduled_hook( 'ced_ebay_order_scheduler_job_' . $user_id );
		$existing_product_sync    = isset( $sanitized_array['ced_ebay_global_settings']['ced_ebay_existing_products_sync'] ) && '0' != $sanitized_array['ced_ebay_global_settings']['ced_ebay_existing_products_sync'] ? ( $sanitized_array['ced_ebay_global_settings']['ced_ebay_existing_products_sync'] ) : wp_clear_scheduled_hook( 'ced_ebay_existing_products_sync_job_' . $user_id );
		$import_products_schedule = isset( $sanitized_array['ced_ebay_global_settings']['ced_ebay_import_product_scheduler_info'] ) && '0' != $sanitized_array['ced_ebay_global_settings']['ced_ebay_import_product_scheduler_info'] ? ( $sanitized_array['ced_ebay_global_settings']['ced_ebay_import_product_scheduler_info'] ) : wp_clear_scheduled_hook( 'ced_ebay_import_products_job_' . $user_id );

		if('0' == $sanitized_array['ced_ebay_global_settings']['ced_ebay_inventory_schedule_info']){
			delete_option('ced_ebay_stock_sync_progress_'.$user_id);
		}
		if ( ! empty( $inventory_schedule ) ) {
			if($inventory_schedule == 'daily'){
				$action_duration = 86400;
			} else if($inventory_schedule == 'twicedaily'){
				$action_duration = 43200;
			} else if($inventory_schedule == 'ced_ebay_6min'){
				$action_duration = 360;
			} else if($inventory_schedule == 'ced_ebay_10min'){
				$action_duration = 600;
			} else if($inventory_schedule == 'ced_ebay_15min'){
				$action_duration = 900;
			} else if($inventory_schedule == 'ced_ebay_30min'){
				$action_duration = 1800;
			}
			// wp_schedule_event( time(), $inventory_schedule, 'ced_ebay_inventory_scheduler_job_' . $user_id );
			as_schedule_recurring_action( time(), $action_duration, 'ced_ebay_inventory_scheduler_job_'.$user_id );
			// as_schedule_cron_action( time(), $inventory_schedule, 'ced_ebay_inventory_scheduler_job_' . $user_id );
			update_option( 'ced_ebay_inventory_scheduler_job_' . $user_id, $user_id );
		}

		if ( ! empty( $order_schedule ) ) {
			wp_schedule_event( time(), $order_schedule, 'ced_ebay_order_scheduler_job_' . $user_id );
			update_option( 'ced_ebay_order_scheduler_job_' . $user_id, $user_id );
		}

		if ( ! empty( $existing_product_sync ) ) {
			wp_schedule_event( time(), $existing_product_sync, 'ced_ebay_existing_products_sync_job_' . $user_id );
			update_option( 'ced_ebay_existing_products_sync_job_' . $user_id, $user_id );
		}
		if ( ! empty( $import_products_schedule ) ) {
			wp_schedule_event( time(), $import_products_schedule, 'ced_ebay_import_products_job_' . $user_id );
			update_option( 'ced_ebay_import_products_job_' . $user_id, $user_id );
		}

		update_option( 'ebay_auto_syncing' . $user_id, 'off' );
		if ( isset( $_POST['ced_ebay_global_settings']['ced_ebay_auto_import'] ) ) {
			update_option( 'ced_ebay_enable_product_import', 'on' );
			wp_clear_scheduled_hook( 'ced_ebay_auto_import_cron_' . $user_id );
			wp_schedule_event( time(), 'ced_ebay_10min', 'ced_ebay_auto_import_cron_' . $user_id );
		} else {
			wp_clear_scheduled_hook( 'ced_ebay_auto_import_cron_' . $user_id );
			update_option( 'ced_ebay_enable_product_import', 'off' );
		}
		$admin_success_notice = '<div class="notice notice-success is-dismissible" style="margin:0;padding:8px;">Your configuration has been saved!</div>';
		print_r( $admin_success_notice );
	}
}


### class admin file code changes

add_action('ced_ebay_async_update_stock_action', array($this, 'ced_ebay_async_update_stock_callback'));


public function ced_ebay_inventory_schedule_manager() {
		$fetchCurrentAction = current_action();
		$user_id            = str_replace( 'ced_ebay_inventory_scheduler_job_', '', $fetchCurrentAction );
		$ced_ebay_manager   = $this->ced_ebay_manager;
		$shop_data          = ced_ebay_get_shop_data( $user_id );
		$logger = wc_get_logger();
		$context = array('source' => 'ced_ebay_inventory_schedule_manager');
		if ( ! empty( $shop_data ) ) {
				$siteID      = $shop_data['siteID'];
				$token       = $shop_data['token']['eBayAuthToken'];
				$getLocation = $shop_data['Location'];
		}

		$products_to_update = get_option( 'ced_eBay_update_chunk_product_' . $user_id, array() );
		if ( empty( $products_to_update ) ) {
			$logger->info('#####Commencing Stock Update#####', $context);
			$store_products     = get_posts(
				array(
					'numberposts' => -1,
					'post_type'   => 'product',
					'post_status' => 'publish',
					'meta_query'  => array(
						array(
							'key'     => '_ced_ebay_listing_id_' . $user_id,
							'compare' => 'EXISTS',
						),
					),
				)
			);
			$store_products     = wp_list_pluck( $store_products, 'ID' );
			update_option( 'ced_ebay_stock_sync_progress_' . $user_id, array('total_count' => count($store_products), 'synced' => 0) );
			$products_to_update = array_chunk( $store_products, 50 );
		}
		if ( is_array( $products_to_update[0] ) && ! empty( $products_to_update[0] ) ) {
			$prodIDs = $products_to_update[0];
			foreach ( $products_to_update[0] as $key => $value ) {
				$product_actual_stock = get_post_meta( $value, '_stock', true );
				$manage_stock         = get_post_meta( $value, '_manage_stock', true );
				$product_status       = get_post_meta( $value, '_stock_status', true );
				if ( 'yes' != $manage_stock && 'instock' == $product_status ) {
					$renderDataOnGlobalSettings = get_option( 'ced_ebay_global_settings', false );
					$default_stock              = isset( $renderDataOnGlobalSettings[ $user_id ]['ced_ebay_product_default_stock'] ) ? $renderDataOnGlobalSettings[ $user_id ]['ced_ebay_product_default_stock'] : 0;
					$product_actual_stock       = $default_stock;
				}

				$async_action_id = as_enqueue_async_action( 'ced_ebay_async_update_stock_action', array('data'=>array('product_id'=>$value, 'user_id'=>$user_id)), 'ced_ebay_inventory_scheduler' );
				if($async_action_id){
					$logger->info(wc_print_r('Product ID '.$value, true), $context);
					$logger->info(wc_print_r('Async Stock Update Scheduled with ID '.$async_action_id, true), $context);
				}
			}
			unset( $products_to_update[0] );
			$products_to_update = array_values( $products_to_update );
			update_option( 'ced_eBay_update_chunk_product_' . $user_id, $products_to_update );
			$logger->info('--------------------------------', $context);


		}
	}


public function ced_ebay_async_update_stock_callback($stock_update_data){
		$synced = 0;
		$logger = wc_get_logger();
	    $context = array('source' => 'ced_ebay_async_update_stock_callback');
	    if(!empty($stock_update_data) && !empty($stock_update_data['product_id']) && !empty($stock_update_data['user_id'])){
	    	$product_id = $stock_update_data['product_id'];
			$logger->info(wc_print_r('Product ID '.$product_id, true), $context);
	    	$user_id = $stock_update_data['user_id'];
			$ced_ebay_manager   = $this->ced_ebay_manager;
		$shop_data          = ced_ebay_get_shop_data( $user_id );
		if ( ! empty( $shop_data ) ) {
				$siteID      = $shop_data['siteID'];
				$token       = $shop_data['token']['eBayAuthToken'];
				$getLocation = $shop_data['Location'];
		}
			require_once CED_EBAY_DIRPATH . 'admin/ebay/lib/ebayUpload.php';
						$ebayUploadInstance = EbayUpload::get_instance( $siteID, $token );
						$SimpleXml          = $ced_ebay_manager->prepareProductHtmlForUpdateStock( $product_id, $user_id );
					if ( is_array( $SimpleXml ) && ! empty( $SimpleXml ) ) {
						$stock_sync_progress = get_option( 'ced_ebay_stock_sync_progress_' . $user_id);
						if(!empty($stock_sync_progress)){
							$synced = $stock_sync_progress['synced'];
							$total_count = $stock_sync_progress['total_count'];
							$synced++;
							update_option( 'ced_ebay_stock_sync_progress_' . $user_id, array('total_count' => $total_count, 'synced' => $synced) );
						}
						$logger->info('Product has more than 4 variations!', $context);
						foreach ( $SimpleXml as $key => $value ) {
							$uploadOnEbay[] = $ebayUploadInstance->cedEbayUpdateInventory( $value );
						}
					} else {
						$stock_sync_progress = get_option( 'ced_ebay_stock_sync_progress_' . $user_id);
						if(!empty($stock_sync_progress)){
							$synced = $stock_sync_progress['synced'];
							$total_count = $stock_sync_progress['total_count'];
							$synced++;
							update_option( 'ced_ebay_stock_sync_progress_' . $user_id, array('total_count' => $total_count, 'synced' => $synced) );
						}
						$uploadOnEbay = $ebayUploadInstance->cedEbayUpdateInventory( $SimpleXml );
						$logger->info('Stock Update Status => '.wc_print_r($uploadOnEbay, true), $context);
					}
		} else {
			$logger->error('Missing Product ID or User ID!', $context);
		}
	}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment