includes/admin/create-products.php
/**
	 *
	 * This functions calls the api for changed stocks and updates the articles stocks accordingly
	 *
	 * @param    The arcavis sync-settings
	 * @return   
	 *
	 */
This functions calls the api for changed stocks and updates the articles stocks accordingly
@param    The arcavis sync-settings
@return
This functions calls the api for changed stocks and updates the articles stocks accordingly @param The arcavis sync - settings @return
public function update_article_stock($options){
		global $wc_arcavis_shop;
		global $wpdb;
		$lastSync = $wc_arcavis_shop->get_last_sync('articlestocks');
		$url = $options['arcavis_link'].'/api/articlestocks?groupByArticle=true&changedSinceTicks='.$lastSync;
		$request_args = array(
		  'headers' => array(
			'Authorization' => 'Basic ' . base64_encode( $options['arcavis_username'] . ':' . $options['arcavis_password'] )
		  )
		);
		$stock_data = wp_remote_get($url,$request_args);
		if(is_wp_error($stock_data)){
			$wc_arcavis_shop->logError('wp_remote_get failed '.$stock_data->geterrormessage());
			exit;
		}
		$stocks = json_decode(wp_remote_retrieve_body($stock_data));
		if(!empty($stocks)){
			foreach ($stocks->Result as $stock) {
				$args = array(
				'post_type'		=>	array('product','product_variation'),
				'meta_query' => array(
						array(
							'key' => 'article_id',
							'value'     => $stock->ArticleId,
							'compare'   => '='
						),
					)
				);
				$products = get_posts($args);
				if(!empty($products)){
					$stockstatus='instock';
					if($stock->Stock<=0){
						$stockstatus='outofstock';
					}
					update_post_meta($products[0]->ID, '_stock_status', $stockstatus );
					update_post_meta( $products[0]->ID,'_stock',$stock->Stock);
				}
			}
			$wpdb->update($wpdb->prefix."lastSyncTicks", array('lastSync' => $stocks->DataAgeTicks), array('apiName'=>'articlestocks'));
		}
	}
public function update_article_stock ( $ options ) { global $ wc_arcavis_shop ; global $ wpdb ; $ lastSync = $ wc_arcavis_shop -> get_last_sync ( 'articlestocks' ) ; $ url = $ options [ 'arcavis_link' ] . '/api/articlestocks?groupByArticle=true&changedSinceTicks=' . $ lastSync ; $ request_args = array ( 'headers' => array ( 'Authorization' => 'Basic ' . base64_encode ( $ options [ 'arcavis_username' ] . ':' . $ options [ 'arcavis_password' ] ) ) ) ; $ stock_data = wp_remote_get ( $ url , $ request_args ) ; if ( is_wp_error ( $ stock_data ) ) { $ wc_arcavis_shop -> logError ( 'wp_remote_get failed ' . $ stock_data -> geterrormessage ( ) ) ; exit ; } $ stocks = json_decode ( wp_remote_retrieve_body ( $ stock_data ) ) ; if ( ! empty ( $ stocks ) ) { foreach ( $ stocks -> Result as $ stock ) { $ args = array ( 'post_type' => array ( 'product' , 'product_variation' ) , 'meta_query' => array ( array ( 'key' => 'article_id' , 'value' => $ stock -> ArticleId , 'compare' => '=' ) , ) ) ; $ products = get_posts ( $ args ) ; if ( ! empty ( $ products ) ) { $ stockstatus = 'instock' ; if ( $ stock -> Stock <= 0 ) { $ stockstatus = 'outofstock' ; } update_post_meta ( $ products [ 0 ] -> ID , '_stock_status' , $ stockstatus ) ; update_post_meta ( $ products [ 0 ] -> ID , '_stock' , $ stock -> Stock ) ; } } $ wpdb -> update ( $ wpdb -> prefix . " lastSyncTicks " , array ( 'lastSync' => $ stocks -> DataAgeTicks ) , array ( 'apiName' => 'articlestocks' ) ) ; } }
This functions calls the api for changed stocks and updates the articles stocks accordingly
 @param    The arcavis sync-settings
 @return
This functions calls the api for changed stocks and updates the articles stocks accordingly @param The arcavis sync - settings @return