Skip to content

Instantly share code, notes, and snippets.

@ali-cedcoss
Last active March 30, 2022 15:19
Show Gist options
  • Save ali-cedcoss/2060bd491fb2f7895afcc1234908ab80 to your computer and use it in GitHub Desktop.
Save ali-cedcoss/2060bd491fb2f7895afcc1234908ab80 to your computer and use it in GitHub Desktop.
syncing listing status from eBay to Woo
```php
//check woo synced product status on eBay
add_action('ced_ebay_check_product_status_on_ebay_action', array($this, 'ced_ebay_check_product_status_on_ebay_callback'));
add_action('wp_ajax_nopriv_ced_ebay_sync_back_ended_listings', array($this, 'ced_ebay_sync_back_ended_listings'));
public function ced_ebay_sync_back_ended_listings(){
$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_check_product_status_on_ebay_action', array('data'=>array('existing_listing_id'=>$existing_listing_id, 'user_id'=>$user_id, 'product_id' => $existing_product_id)), 'ced_ebay_sync_back_ended_listings' );
}
}
}
public function ced_ebay_check_product_status_on_ebay_callback($product_data){
$logger = wc_get_logger();
$context = array('source' => 'ced_ebay_check_product_status_on_ebay_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'];
}
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(!empty($itemDetails['Item']['ListingDetails']['RelistedItemID'])){
update_post_meta($existing_product_id, '_ced_ebay_listing_id_'.$user_id, $itemDetails['Item']['ListingDetails']['RelistedItemID']);
$logger->info(wc_print_r($existing_listing_id.' RELISTED. UPDATING CUSTOM FIELD!', true), $context);
update_post_meta($existing_product_id, '_price', $itemDetails['Item']['SellingStatus']['CurrentPrice']);
update_post_meta($existing_product_id, '_regular_price', $itemDetails['Item']['SellingStatus']['CurrentPrice']);
$product = wc_get_product($existing_product_id);
$product->save();
if ( $itemDetails['Item']['Quantity'] > $itemDetails['Item']['SellingStatus']['QuantitySold'] && ($itemDetails['Item']['Quantity'] - $itemDetails['Item']['SellingStatus']['QuantitySold']) > 0 ) {
$product->set_manage_stock( true );
$prod_stock = $itemDetails['Item']['Quantity'] - $itemDetails['Item']['SellingStatus']['QuantitySold'];
$product->set_stock_quantity( $prod_stock );
$product->set_stock_status( 'instock' );
$product->save();
} else {
$product->set_stock_status( 'outofstock' );
$product->save();
}
$existing_listing_id = $itemDetails['Item']['ListingDetails']['RelistedItemID'];
$logger->info('#########################################', $context);
} elseif(!empty($itemDetails['Item']['ListingDetails']['EndingReason']) || 'Completed' == $itemDetails['Item']['SellingStatus']['ListingStatus']){
$existing_product = wc_get_product($existing_product_id);
$existing_product->set_stock(0);
$existing_product->set_stock_status('outofstock');
$existing_product->save();
$logger->info(wc_print_r($existing_listing_id.' REMOVED. UPDATING CUSTOM FIELD!', true), $context);
delete_post_meta($existing_product_id, '_ced_ebay_listing_id_'.$user_id);
$existing_listing_id = '';
$logger->info('#########################################', $context);
} else{
update_post_meta($existing_product_id, '_price', $itemDetails['Item']['SellingStatus']['CurrentPrice']);
update_post_meta($existing_product_id, '_regular_price', $itemDetails['Item']['SellingStatus']['CurrentPrice']);
$product = wc_get_product($existing_product_id);
$product->save();
if ( $itemDetails['Item']['Quantity'] > $itemDetails['Item']['SellingStatus']['QuantitySold'] && ($itemDetails['Item']['Quantity'] - $itemDetails['Item']['SellingStatus']['QuantitySold']) > 0 ) {
$product->set_manage_stock( true );
$prod_stock = $itemDetails['Item']['Quantity'] - $itemDetails['Item']['SellingStatus']['QuantitySold'];
$product->set_stock_quantity( $prod_stock );
$product->set_stock_status( 'instock' );
$product->save();
} else {
$product->set_stock_status( 'outofstock' );
$product->save();
}
$logger->info(wc_print_r($existing_listing_id.' LISTING STILL ACTIVE. DOING NOTHING!', true), $context);
$logger->info('#########################################', $context);
}
if(!empty($existing_listing_id)){
if(empty(get_option('ced_ebay_prod_id_and_listing_id_'.$user_id))){
$prod_id_and_listing_id = [];
$prod_id_and_listing_id[$existing_product_id] = $existing_listing_id;
update_option('ced_ebay_prod_id_and_listing_id_'.$user_id, $prod_id_and_listing_id);
} else {
$temp_prod_listing_id = [];
$temp_prod_listing_id = get_option('ced_ebay_prod_id_and_listing_id_'.$user_id, true);
$temp_prod_listing_id[$existing_product_id] = $existing_listing_id;
update_option('ced_ebay_prod_id_and_listing_id_'.$user_id, $temp_prod_listing_id);
}
}
} 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