//product update and order created woo webhooks
add_action('wp_ajax_ced_ebay_webhook_product_updated_action', array($this, 'ced_ebay_webhook_product_updated_action'));
add_action('wp_ajax_nopriv_ced_ebay_webhook_product_updated_action', array($this, 'ced_ebay_webhook_product_updated_action'));
add_action('wp_ajax_nopriv_ced_ebay_webhook_order_created_action', array($this, 'ced_ebay_webhook_order_created_action'));
add_action('ced_ebay_process_stock_change_webhook', array($this, 'ced_ebay_webhook_update_product_stock'), 10, 1);
public function ced_ebay_webhook_product_updated_action(){
$logger=wc_get_logger();
$context = array('source' => 'ced_ebay_webhook_product_updated_action');
$logger->info('Receiving Data', $context);
if(file_get_contents("php://input")) {
$response_body = json_decode(file_get_contents("php://input"), true);
// $logger->info(wc_print_r($response_body, true), $context);
// return;
$product_id = $response_body['id'];
if ( ! empty( get_option( 'ced_ebay_all_account_details' ) ) ) {
$details = get_option( 'ced_ebay_all_account_details' );
foreach ( $details as $key => $ebay_account ) {
$user_id = $ebay_account['user_id'];
}
}
if(!empty(get_post_meta($product_id, '_ced_ebay_listing_id_'.$user_id))){
$product = wc_get_product($product_id);
if($product->is_type('simple')){
$logger->info('Simple Product', $context);
$manage_stock = !empty($response_body['manage_stock']) ? $response_body['manage_stock'] : false;
$stock_status = $response_body['stock_status'];
if ( 'true' != $manage_stock && 'instock' == $stock_status ){
$logger->info('Manage Stock OFF & Picking up default stock value', $context);
$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;
if(empty($default_stock)){
$logger->info('No default stock is set. Exiting!', $context);
return;
}
$product_actual_stock = $default_stock;
} else if('true' != $manage_stock && 'outofstock' == $stock_status) {
$logger->info('Manage Stock OFF & Product Out of Stock', $context);
$product_actual_stock = 0;
}
else {
$logger->info('Manage Stock OFF & Product In Stock', $context);
$product_actual_stock = $response_body['stock_quantity'];
$logger->info(wc_print_r($product_actual_stock, true), $context);
if('null' == $product_actual_stock){
$logger->info('Manage Stock OFF & Product Out of Stock', $context);
$product_actual_stock = 0;
}
}
$stock_update_data = [];
$stock_update_data[] = array('user_id' => $user_id, 'product_stock' => $product_actual_stock, 'product_id' => $product_id, 'product_type' => 'simple');
as_enqueue_async_action( 'ced_ebay_process_stock_change_webhook', $stock_update_data, 'ced_ebay_notification' );
} else if($product->is_type('variable')){
$logger->info('Variable Product', $context);
$stock_update_data = [];
$stock_update_data[] = array('user_id' => $user_id, 'product_stock' => 0, 'product_id' => $product_id, 'product_type' => 'variable');
as_enqueue_async_action( 'ced_ebay_process_stock_change_webhook', $stock_update_data, 'ced_ebay_notification' );
}
}
}
}
public function ced_ebay_webhook_order_created_action(){
$logger=wc_get_logger();
$context = array('source' => 'ced_ebay_webhook_order_created_action');
$logger->info('Receiving Data', $context);
if(file_get_contents("php://input")) {
$response_body = json_decode(file_get_contents("php://input"), true);
// $logger->info(wc_print_r($response_body, true), $context);die;
$order_id = $response_body['id'];
$order = wc_get_order($order_id);
if ( ! empty( get_option( 'ced_ebay_all_account_details' ) ) ) {
$details = get_option( 'ced_ebay_all_account_details' );
foreach ( $details as $key => $ebay_account ) {
$user_id = $ebay_account['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 );
if($order){
foreach($order->get_items() as $item_id => $item){
$product = $item->get_product();
$product_id = $product->id;
if(!empty(get_post_meta($product_id, '_ced_ebay_listing_id_'.$user_id, true))){
if($product->get_stock_quantity() == 0){
$logger->info(wc_print_r('product '. $product_id.' is out of stock. Removing from eBay.', true), $context);
$itemIDs = array();
$already_uploaded = get_post_meta( $product_id, '_ced_ebay_listing_id_' . $user_id, true );
if ( $already_uploaded ) {
$itemIDs[ $product_id ] = $already_uploaded;
}
$archiveProducts = $ebayUploadInstance->endItems( $itemIDs );
$logger->info(wc_print_r($archiveProducts, true), $context);
if ( is_array( $archiveProducts ) && ! empty( $archiveProducts ) ) {
if ( isset( $archiveProducts['Ack'] ) ) {
if ( 'Warning' == $archiveProducts['Ack'] || 'Success' == $archiveProducts['Ack'] ) {
delete_post_meta( $product_id, '_ced_ebay_listing_id_' . $user_id );
delete_post_meta($product_id, '_ced_ebay_relist_item_id_'.$user_id);
} else if('Failure' == $archiveProducts['Ack']){
if(isset($archiveProducts['EndItemResponseContainer'])){
$endResponse = $archiveProducts['EndItemResponseContainer'];
if($product_id == $endResponse['CorrelationID']){
if('1047' == $endResponse['Errors']['ErrorCode']){
delete_post_meta( $product_id, '_ced_ebay_listing_id_' . $user_id );
delete_post_meta($product_id, '_ced_ebay_relist_item_id_'.$user_id);
}
}
}
}
}
}
else {
$logger->info('Product not found on eBay', $context);
}
} else {
$SimpleXml = $ced_ebay_manager->prepareProductHtmlForUpdateStock( $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 ) ) {
foreach ( $uploadOnEbay as $key => $errors ) {
if ( isset( $errors['Errors'][0] ) ) {
foreach ( $errors['Errors'] as $key => $value ) {
if ( 'Error' == $value['SeverityCode'] ) {
$logger->info(wc_print_r($value, true), $context);
}
}
} else {
$logger->info(wc_print_r($errors, true), $context);
}
}
} else {
if ( isset( $uploadOnEbay['Errors'][0] ) ) {
foreach ( $uploadOnEbay['Errors'] as $key => $value ) {
if ( 'Error' == $value['SeverityCode'] ) {
$logger->info(wc_print_r($value, true), $context);
}
}
} else {
$logger->info(wc_print_r($uploadOnEbay, true), $context);
}
}
}
} else {
$logger->info('Not an eBay product', $context);
}
}
}
}
}
public function ced_ebay_webhook_update_product_stock($stock_update_data){
$logger = wc_get_logger();
$context = array('source' => 'ced_ebay_webhook_update_product_stock');
$logger->info(wc_print_r($stock_update_data, true), $context);
if(is_array($stock_update_data)){
$user_id = $stock_update_data['user_id'];
$ced_ebay_manager = $this->ced_ebay_manager;
$shop_data = ced_ebay_get_shop_data( $user_id );
$product_id = $stock_update_data['product_id'];
$product_stock = $stock_update_data['product_stock'];
$product_type = $stock_update_data['product_type'];
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 );
if($product_stock > 0 || 'variable' == $product_type){
$SimpleXml = $ced_ebay_manager->prepareProductHtmlForUpdateStock( $product_id, $user_id );
// $logger->info(wc_print_r($SimpleXml, true), $context);
// return;
if ( is_array( $SimpleXml ) && ! empty( $SimpleXml ) ) {
foreach ( $SimpleXml as $key => $value ) {
$uploadOnEbay[] = $ebayUploadInstance->cedEbayUpdateInventory( $value );
}
} else {
$uploadOnEbay = $ebayUploadInstance->cedEbayUpdateInventory( $SimpleXml );
}
if ( is_array( $uploadOnEbay ) ) {
foreach ( $uploadOnEbay as $key => $errors ) {
if ( isset( $errors['Errors'][0] ) ) {
foreach ( $errors['Errors'] as $key => $value ) {
if ( 'Error' == $value['SeverityCode'] ) {
$logger->info(wc_print_r($value, true), $context);
}
}
} else {
$logger->info(wc_print_r($errors, true), $context);
}
}
} else {
if ( isset( $uploadOnEbay['Errors'][0] ) ) {
foreach ( $uploadOnEbay['Errors'] as $key => $value ) {
if ( 'Error' == $value['SeverityCode'] ) {
$logger->info(wc_print_r($value, true), $context);
}
}
} else {
$logger->info(wc_print_r($uploadOnEbay, true), $context);
}
}
} else if($product_stock == 0) {
$logger->info(wc_print_r('product '. $product_id.' is out of stock. Removing from eBay.', true), $context);
$itemIDs = array();
$already_uploaded = get_post_meta( $product_id, '_ced_ebay_listing_id_' . $user_id, true );
if ( $already_uploaded ) {
$itemIDs[ $product_id ] = $already_uploaded;
}
$archiveProducts = $ebayUploadInstance->endItems( $itemIDs );
if ( is_array( $archiveProducts ) && ! empty( $archiveProducts ) ) {
if ( isset( $archiveProducts['Ack'] ) ) {
if ( 'Warning' == $archiveProducts['Ack'] || 'Success' == $archiveProducts['Ack'] ) {
delete_post_meta( $product_id, '_ced_ebay_listing_id_' . $user_id );
delete_post_meta($product_id, '_ced_ebay_relist_item_id_'.$user_id);
}
}
}
else {
$logger->info('Product not found on eBay', $context);
}
}
}
}
```
Last active
January 3, 2022 08:26
-
-
Save ali-cedcoss/7b924485f898a1dc8afcca91707f09f4 to your computer and use it in GitHub Desktop.
update stock on ebay using woo webhooks as well as end the product when stock reaches 0
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment