Skip to content

Instantly share code, notes, and snippets.

@ali-cedcoss
Last active December 28, 2021 10:15
Show Gist options
  • Save ali-cedcoss/d3a8bcc32ccd0aa435bcdd8a07eae854 to your computer and use it in GitHub Desktop.
Save ali-cedcoss/d3a8bcc32ccd0aa435bcdd8a07eae854 to your computer and use it in GitHub Desktop.
woo to ebay bulk stock sync

Syncing stock levels from Woo to eBay in Bulk by picking up all the products marked uploaded and scheduling a single action for each product and sending them to eBay for the update.


		//check woo synced product status on eBay
		add_action('ced_ebay_bulk_sync_stock_and_price', array($this, 'ced_ebay_bulk_sync_stock_and_price_callback'));
        		add_action('wp_ajax_nopriv_ced_ebay_bulk_sync_woo_to_ebay', array($this, 'ced_ebay_bulk_sync_woo_to_ebay'));


public function ced_ebay_bulk_sync_woo_to_ebay(){

	    // print_r(get_option('ced_ebay_prod_id_and_listing_id_mroparts', true));die;
		$logger             = wc_get_logger();
		$context            = array( 'source' => 'ced_ebay_sync_back_ended_listings' );
		$fetchCurrentAction = current_action();
		if ( strpos( $fetchCurrentAction, 'wp_ajax_nopriv_' ) !== false ) {
			$user_id = isset( $_GET['user_id'] ) ? wc_clean( $_GET['user_id'] ) : false;
		}
		$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'];
		}
		$args = array(
    'post_type'  => 'product',
    'numberposts' => '-1',
    'fields'      => 'ids',
    'meta_query' => array(
        array(
            'key'   => '_ced_ebay_listing_id_'.$user_id,
            'compare' => 'EXISTS',
        )
    )
);
$store_products = get_posts($args);
		if(!empty($store_products)){
			foreach($store_products as $key => $existing_product_id){
				$existing_listing_id = get_post_meta($existing_product_id, '_ced_ebay_listing_id_' . $user_id, true);
				as_enqueue_async_action( 'ced_ebay_bulk_sync_stock_and_price', array('data'=>array('existing_listing_id'=>$existing_listing_id, 'user_id'=>$user_id, 'product_id' => $existing_product_id)), 'ced_ebay_bulk_syncing' );
			}
		}
	}

	public function ced_ebay_bulk_sync_stock_and_price_callback($product_data){
	    $logger = wc_get_logger();
	    $context = array('source' => 'ced_ebay_bulk_sync_stock_and_price_callback');
	    if(!empty($product_data)){
	        $user_id = $product_data['user_id'];
	        $existing_listing_id = $product_data['existing_listing_id'];
	        $existing_product_id = $product_data['product_id'];
	        $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'];
		    }
            if(!empty($existing_listing_id)){
                require_once CED_EBAY_DIRPATH . 'admin/ebay/lib/ebayUpload.php';
                $ebayUploadInstance = EbayUpload::get_instance( $siteID, $token );
                $SimpleXml          = $ced_ebay_manager->prepareProductHtmlForUpdateStock( $existing_product_id, $user_id );
				    if ( is_array( $SimpleXml ) && ! empty( $SimpleXml ) ) {
							foreach ( $SimpleXml as $key => $value ) {
								$uploadOnEbay[] = $ebayUploadInstance->cedEbayUpdateInventory( $value );
							}
						} else {
							$uploadOnEbay = $ebayUploadInstance->cedEbayUpdateInventory( $SimpleXml );
						}
						if ( is_array( $uploadOnEbay ) && ! empty( $uploadOnEbay[0] ) ) {
							foreach ( $uploadOnEbay as $key => $inventory_update ) {
								if ( isset( $inventory_update['Ack'] ) ) {
									if ( 'Warning' == $inventory_update['Ack'] || 'Success' == $inventory_update['Ack'] ) {
										$logger->info(wc_print_r('Stock levels updated for product '.$existing_product_id.' on eBay!', true), $context);
									} else {
										$error = '';
										if ( isset( $inventory_update['Errors'][0] ) ) {
											foreach ( $inventory_update['Errors'] as $key => $value ) {
												if ( 'Error' == $value['SeverityCode'] ) {
													$error .= $value['ShortMessage'] . '<br>';
												}
											}
										} else {
											$error .= $inventory_update['Errors']['ShortMessage'] . '<br>';
										}
                                        $logger->error(wc_print_r('Errors encountered for product '.$existing_product_id.' while trying to update stock on eBay!', true), $context);
                                        $logger->error(wc_print_r($error, true), $context);
									}
								}
							}
						} else {
							if ( isset( $uploadOnEbay['Ack'] ) ) {
								if ( 'Warning' == $uploadOnEbay['Ack'] || 'Success' == $uploadOnEbay['Ack'] ) {
									$logger->info(wc_print_r('Stock levels updated for product '.$existing_product_id.' on eBay!', true), $context);
								} else {
									$error = '';
									if ( isset( $uploadOnEbay['Errors'][0] ) ) {
										foreach ( $uploadOnEbay['Errors'] as $key => $value ) {
											if ( 'Error' == $value['SeverityCode'] ) {
												$error .= $value['ShortMessage'] . '<br>';
											}
										}
									} else {
										$error .= $uploadOnEbay['Errors']['ShortMessage'] . '<br>';
									}
                                    $logger->error(wc_print_r('Errors encountered for product '.$existing_product_id.' while trying to update stock on eBay!', true), $context);
                                    $logger->error(wc_print_r($error, true), $context);
								}
							}
						}

            }

	    }
	}





Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment