Last active
October 12, 2021 12:59
-
-
Save ali-cedcoss/f59237f947cdfc4670acb6dc74dc51b3 to your computer and use it in GitHub Desktop.
ebay to woo price sync
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Run the existing product sync first to make sure that the eBay listings are synced on WooCommerce. Then run this code to sync the price of products from eBay to Woo. | |
add_action('ced_ebay_sync_price_to_woo_action', array($this, 'ced_ebay_sync_price_to_woo_action_cb')); | |
add_action('wp_ajax_nopriv_ced_ebay_sync_price_from_ebay_to_woo', array($this, 'ced_ebay_sync_price_from_ebay_to_woo')); | |
public function ced_ebay_sync_price_from_ebay_to_woo(){ | |
$logger = wc_get_logger(); | |
$context = array( 'source' => 'ced_ebay_sync_price_from_ebay_to_woo' ); | |
$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_sync_price_to_woo_action', array('data'=>array('existing_listing_id'=>$existing_listing_id, 'user_id'=>$user_id, 'product_id' => $existing_product_id)), 'ced_ebay_to_woo_sync' ); | |
} | |
} | |
} | |
public function ced_ebay_sync_price_to_woo_action_cb($product_data){ | |
$logger = wc_get_logger(); | |
$context = array('source' => 'ced_ebay_sync_price_to_woo_action_cb'); | |
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']; | |
} | |
require_once CED_EBAY_DIRPATH . 'admin/ebay/lib/ebayUpload.php'; | |
$get_item_xml = ' | |
<?xml version="1.0" encoding="utf-8"?> | |
<GetItemRequest xmlns="urn:ebay:apis:eBLBaseComponents"> | |
<RequesterCredentials> | |
<eBayAuthToken>' . $token . '</eBayAuthToken> | |
</RequesterCredentials> | |
<DetailLevel>ReturnAll</DetailLevel> | |
<ItemID>' . $existing_listing_id . '</ItemID> | |
</GetItemRequest>'; | |
$ebayUploadInstance = EbayUpload::get_instance( $siteID, $token ); | |
$itemDetails = $ebayUploadInstance->get_item_details( $get_item_xml ); | |
if ( 'Success' == $itemDetails['Ack'] || 'Warning' == $itemDetails['Ack'] ) { | |
if(isset($itemDetails['Item']['SellingStatus']['CurrentPrice']) && $itemDetails['Item']['SellingStatus']['CurrentPrice'] > 0){ | |
$logger->info(wc_print_r('Syncing Price ('.$itemDetails['Item']['SellingStatus']['CurrentPrice'].') from eBay Listing '.$existing_listing_id.' to Woo Product '.$existing_product_id, true), $context); | |
//change this logic. | |
update_post_meta($existing_product_id, '_price_ebay', $itemDetails['Item']['SellingStatus']['CurrentPrice']); | |
} | |
} else { | |
$logger->info('Failed to get listing details from API', $context); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment