Skip to content

Instantly share code, notes, and snippets.

@projoomexperts
Created April 10, 2018 14:51
Show Gist options
  • Save projoomexperts/61b78a0c1839bcf026c3459d5f831733 to your computer and use it in GitHub Desktop.
Save projoomexperts/61b78a0c1839bcf026c3459d5f831733 to your computer and use it in GitHub Desktop.
// Scheduled Action Hook
function gnc_woo_cron_update( ) {
// Get currency conversion rate from settings
$currentcy_settings = get_option( 'gnc_currency_settings' );
$rate = $currentcy_settings[gnc_currency_text_field_0];
// Query argument for woocommerce products.
$args = array(
// WC product post types
'post_type' => array('product', 'product_variation'),
// all posts
'numberposts' => -1,
'post_status' => 'publish',
);
// Get all products
$shop_products = get_posts( $args );
foreach( $shop_products as $item){
// Get Dollar value from acf
$dolares = get_field('dolares' , $item->ID );
// check if the product has Dollar value
if (!empty( $dolares )){
$meta_data = get_post_meta($item->ID);
// Multiply Dollar value with conversion rate
$regular_price = $dolares * $rate;
// Round up the price in colon
$regular_price = round($regular_price);
// Update the price for the product.
update_post_meta($item->ID, '_regular_price', $regular_price);
update_post_meta($item->ID, '_price', $regular_price);
}
}
}
// Schedule Cron Job Event
function gnc_woo_cron() {
if ( ! wp_next_scheduled( 'gnc_woo_cron_update' ) ) {
wp_schedule_event( current_time( 'timestamp' ), 'hourly', 'gnc_woo_cron_update' );
}
}
add_action( 'wp', 'gnc_woo_cron' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment