Last active
January 29, 2022 08:49
-
-
Save shivapoudel/38ca18fb67b4c77be1cc8c41727915a5 to your computer and use it in GitHub Desktop.
Action Schedular queue in loop
This file contains hidden or 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
<?php | |
function args_update() { | |
$loop = 0; | |
$args = array( | |
'name', | |
'surname' | |
); | |
foreach ( $args as $arg ) { | |
WC()->queue()->schedule_single( | |
time() + $loop, | |
'woocommerce_run_arg_callback', | |
array( | |
'arg' => $arg, | |
), | |
'woocommerce-test-args' | |
); | |
$loop++; | |
} | |
} | |
/** | |
* Register activation. | |
* | |
* @since 1.1.0 | |
*/ | |
function wc_test_install_actions() { | |
if ( defined( 'WC_VERSION' ) && version_compare( WC_VERSION, '5.6.0', '>=' ) ) { | |
args_update(); | |
} | |
} | |
register_activation_hook( __FILE__, 'wc_test_install_actions' ); | |
// Action Scheduler - Background job queue actions. | |
add_action( 'woocommerce_run_arg_callback', '_run_arg_callback', 10, 1 ); | |
function _run_arg_callback( $arg ) { | |
error_log( print_r( $arg, true ) ); | |
} |
This file contains hidden or 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
<?php | |
/** | |
* Sync WooCommerce Products from NetSuite. | |
* | |
* @since 1.0.0 | |
*/ | |
function webo_sync_wc_products() { | |
$productSKUs = array(); | |
$wc_products = wc_get_products( array( | |
'return' => 'objects', | |
'status' => 'publish', | |
'limit' => -1, | |
) ); | |
if ( ! empty( $wc_products ) ) { | |
foreach ( $wc_products as $wc_product ) { | |
$productSKUs[] = $wc_product->get_sku(); | |
} | |
} | |
try { | |
$limit = 100; | |
$restlet = new NetSuiteRestlet(); | |
$countProductsResponse = $restlet->callRestlet( 'GET', 'countTotalProducts' ); | |
if ( 200 === wp_remote_retrieve_response_code( $countProductsResponse ) ) { | |
$countTotalProducts = wp_remote_retrieve_body( $countProductsResponse['response'] ); | |
addToLog( 'product', 'Found total products on NetSuite: ', (int) $countTotalProducts ); | |
// Loop through all products. | |
for ( $start = 0; $start < $countTotalProducts; $start++ ) { | |
$searchResponse = $restlet->callRestlet( 'POST', 'getProductsWithLimit', array( | |
'action' => 'getProductsWithLimit', | |
'data' => array( | |
'start' => $start, | |
'end' => $start + $limit, | |
) | |
) ); | |
if ( 200 === wp_remote_retrieve_response_code( $searchResponse ) ) { | |
$products = wp_remote_retrieve_body( $searchResponse['response'] ); | |
if ( is_array( $products ) ) { | |
foreach ( $products as $product ) { | |
// Check the valid product ID. | |
if ( ! isset( $product->itemID ) ) { | |
continue; | |
} | |
$id = webo_get_product_by_sku( $product->itemID ); | |
if ( $id ) { | |
$key = array_search( $product->itemID, $productSKUs ); | |
if ( $key !== false ) { | |
unset( $productSKUs[ $key ] ); | |
} | |
webo_update_product_to_wp( $product, $id ); | |
wp_update_post( array( | |
'ID' => $id, | |
'post_status' => $product->isInactive ? 'draft' : 'publish' | |
) ); | |
} else { | |
webo_create_wc_product( $product ); | |
} | |
$start++; | |
} | |
$start--; | |
} | |
// Update product to draft. | |
if ( ! empty( $productSKUs ) ) { | |
foreach ( $productSKUs as $sku ) { | |
$id = webo_get_product_by_sku( $sku ); | |
if ( $id ) { | |
wp_update_post( array( | |
'ID' => $id, | |
'post_status' => 'draft' | |
) ); | |
} | |
} | |
addToLog( 'product', 'Total Products drafted: ', count( $productSKUs ) ); | |
} | |
} else { | |
addToLog( 'product_search', $searchResponse, 'search failed' ); | |
} | |
} | |
} | |
} catch ( \Exception $exception ) { | |
send_email( 'Product Search Exception', $exception, 'exception' ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment