Created
August 19, 2017 13:05
-
-
Save Katia-Kovtun/b08fe582adb0c49328b45a108bf82dfc to your computer and use it in GitHub Desktop.
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
//Для простых товаров поля и сохранение | |
function wc_uahprice_product_field() { | |
woocommerce_wp_text_input( array( 'id' => '_uah_price', 'class' => 'wc_input_price short', 'label' => __( 'UAH', 'woocommerce' ) ) ); | |
woocommerce_wp_text_input( array( 'id' => '_usd_price', 'class' => 'wc_input_price short', 'label' => __( 'USD', 'woocommerce' ) ) ); | |
woocommerce_wp_text_input( array( 'id' => '_euro_price', 'class' => 'wc_input_price short', 'label' => __( 'EUR', 'woocommerce' ) ) ); | |
} | |
add_action( 'woocommerce_product_options_pricing', 'wc_uahprice_product_field' ); | |
function wc_myprice_save_product( $product_id ) { | |
if ( ( $_POST['_uah_price'] ) ) { | |
if ( is_numeric( $_POST['_uah_price'] ) ) | |
update_post_meta( $product_id, '_uah_price', $_POST['_uah_price'] ); | |
} else delete_post_meta( $product_id, '_uah_price' ); | |
if ( ( $_POST['_usd_price'] ) ) { | |
if ( is_numeric( $_POST['_usd_price'] ) ) | |
update_post_meta( $product_id, '_usd_price', $_POST['_usd_price'] ); | |
} else delete_post_meta( $product_id, '_usd_price' ); | |
if ( ( $_POST['_euro_price'] ) ) { | |
if ( is_numeric( $_POST['_euro_price'] ) ) | |
update_post_meta( $product_id, '_euro_price', $_POST['_euro_price'] ); | |
} else delete_post_meta( $product_id, '_euro_price' ); | |
$UAH = get_post_meta( $product_id, '_uah_price', true ); | |
$USD = get_post_meta( $product_id, '_usd_price', true ); | |
$EUR = get_post_meta( $product_id, '_euro_price', true ); | |
if(get_option( '_curr_date_upd') != date('l jS F Y')){ | |
$url = 'https://api.privatbank.ua/p24api/pubinfo?json&exchange&coursid=4'; | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE); | |
curl_setopt($ch, CURLOPT_HEADER, 0); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
@curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); | |
$res = curl_exec($ch); | |
curl_close($ch); | |
$currency_data = json_decode($res, true); | |
$rates = array(); | |
if (!empty($currency_data)) { | |
foreach ($currency_data as $c) { | |
if ($c['base_ccy'] == 'UAH') { | |
$rates[$c['ccy']] = floatval($c['sale']); | |
} | |
} | |
} | |
$dollar = $rates['USD']; | |
$euro = $rates['EUR']; | |
update_option( '_curr_usdd', $dollar ); | |
update_option( '_curr_euro', $euro ); | |
update_option('_curr_date_upd', date('l jS F Y')); | |
} else{ | |
$dollar = get_option( '_curr_usdd'); | |
$euro = get_option( '_curr_euro'); | |
} | |
if (($UAH != "") && ($UAH != 0)) { | |
$custom_price = $UAH; | |
} | |
if (($USD != "") && ($USD != 0)) { | |
$custom_price = $USD * $dollar; | |
} | |
if (($EUR != "") && ($EUR != 0)) { | |
$custom_price = $EUR * $euro; | |
} | |
$custom_price = round($custom_price, 2); // округляем до сотых, чтобы в regular_price не записывались огромные дроби | |
$regular_price = get_post_meta($post->ID, '_price', true); //получаем текущую цену товара | |
update_post_meta( $product_id, '_regular_price', (float)$custom_price ); | |
update_post_meta( $product_id, '_price', (float)$custom_price ); | |
} | |
add_action( 'save_post', 'wc_myprice_save_product' ); | |
//Для ввариативных товаров поля и сохранение | |
add_action( 'woocommerce_product_after_variable_attributes', 'variation_settings_fields', 10, 3 ); | |
add_action( 'woocommerce_save_product_variation', 'save_variation_settings_fields', 10, 2 ); | |
function variation_settings_fields( $loop, $variation_data, $variation ) { | |
woocommerce_wp_text_input( | |
array( | |
'id' => '_uah_price[' . $variation->ID . ']', | |
'label' => __( 'UAH', 'woocommerce' ), | |
'desc_tip' => 'false', | |
'description' => __( 'Enter the custom number here.', 'woocommerce' ), | |
'value' => get_post_meta( $variation->ID, '_uah_price', true ), | |
'custom_attributes' => array( | |
'step' => 'any', | |
'min' => '0' | |
) | |
) | |
); | |
woocommerce_wp_text_input( | |
array( | |
'id' => '_usd_price[' . $variation->ID . ']', | |
'label' => __( 'USD', 'woocommerce' ), | |
'desc_tip' => 'true', | |
'description' => __( 'Enter the custom number here.', 'woocommerce' ), | |
'value' => get_post_meta( $variation->ID, '_usd_price', true ), | |
'custom_attributes' => array( | |
'step' => 'any', | |
'min' => '0' | |
) | |
) | |
); | |
woocommerce_wp_text_input( | |
array( | |
'id' => '_euro_price[' . $variation->ID . ']', | |
'label' => __( 'EUR', 'woocommerce' ), | |
'desc_tip' => 'true', | |
'description' => __( 'Enter the custom number here.', 'woocommerce' ), | |
'value' => get_post_meta( $variation->ID, '_euro_price', true ), | |
'custom_attributes' => array( | |
'step' => 'any', | |
'min' => '0' | |
) | |
) | |
); | |
} | |
function save_variation_settings_fields( $post_id ) { | |
$number_field = $_POST['_uah_price'][ $post_id ]; | |
if( ! empty( $number_field ) ) { | |
update_post_meta( $post_id, '_uah_price', esc_attr( $number_field ) ); | |
}else delete_post_meta( $post_id, '_uah_price' ); | |
$number_field = $_POST['_usd_price'][ $post_id ]; | |
if( ! empty( $number_field ) ) { | |
update_post_meta( $post_id, '_usd_price', esc_attr( $number_field ) ); | |
}else delete_post_meta( $post_id, '_usd_price' ); | |
$number_field = $_POST['_euro_price'][ $post_id ]; | |
if( ! empty( $number_field ) ) { | |
update_post_meta( $post_id, '_euro_price', esc_attr( $number_field ) ); | |
}else delete_post_meta( $post_id, '_euro_price' ); | |
$UAH = get_post_meta( $post_id, '_uah_price', true ); | |
$USD = get_post_meta( $post_id, '_usd_price', true ); | |
$EUR = get_post_meta( $post_id, '_euro_price', true ); | |
if(get_option( '_curr_date_upd') != date('l jS F Y')){ | |
$url = 'https://api.privatbank.ua/p24api/pubinfo?json&exchange&coursid=4'; | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE); | |
curl_setopt($ch, CURLOPT_HEADER, 0); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
@curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); | |
$res = curl_exec($ch); | |
curl_close($ch); | |
$currency_data = json_decode($res, true); | |
$rates = array(); | |
if (!empty($currency_data)) { | |
foreach ($currency_data as $c) { | |
if ($c['base_ccy'] == 'UAH') { | |
$rates[$c['ccy']] = floatval($c['sale']); | |
} | |
} | |
} | |
$dollar = $rates['USD']; | |
$euro = $rates['EUR']; | |
update_option( '_curr_usdd', $dollar ); | |
update_option( '_curr_euro', $euro ); | |
update_option('_curr_date_upd', date('l jS F Y')); | |
} else{ | |
$dollar = get_option( '_curr_usdd'); | |
$euro = get_option( '_curr_euro'); | |
} | |
if (($UAH != "") && ($UAH != 0)) { | |
$custom_price = $UAH; | |
} | |
if (($USD != "") && ($USD != 0)) { | |
$custom_price = $USD * $dollar; | |
} | |
if (($EUR != "") && ($EUR != 0)) { | |
$custom_price = $EUR * $euro; | |
} | |
$custom_price = round($custom_price, 2); // округляем до сотых, чтобы в regular_price не записывались огромные дроби | |
update_post_meta( $post_id, '_regular_price', (float)$custom_price ); | |
update_post_meta( $post_id, '_price', (float)$custom_price ); | |
} | |
// Add a new interval of a week | |
add_filter( 'cron_schedules', 'myprefix_add_weekly_cron_schedule' ); | |
function myprefix_add_weekly_cron_schedule( $schedules ) { | |
$schedules['weekly'] = array( | |
'interval' => 86400, // 1 day in seconds | |
'display' => __( 'Once Daily' ), | |
); | |
return $schedules; | |
} | |
// Schedule an action if it's not already scheduled | |
if ( ! wp_next_scheduled( 'myprefix_my_cron_action' ) ) { | |
wp_schedule_event( time(), 'weekly', 'myprefix_my_cron_action' ); | |
} | |
// Hook into that action that'll fire weekly | |
add_action( 'myprefix_my_cron_action', 'myprefix_function_to_run' ); | |
function myprefix_function_to_run() { | |
if(get_option( '_curr_date_upd') != date('l jS F Y')){ | |
$url = 'https://api.privatbank.ua/p24api/pubinfo?json&exchange&coursid=4'; | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE); | |
curl_setopt($ch, CURLOPT_HEADER, 0); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
@curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); | |
$res = curl_exec($ch); | |
curl_close($ch); | |
$currency_data = json_decode($res, true); | |
$rates = array(); | |
if (!empty($currency_data)) { | |
foreach ($currency_data as $c) { | |
if ($c['base_ccy'] == 'UAH') { | |
$rates[$c['ccy']] = floatval($c['sale']); | |
} | |
} | |
} | |
$dollar = $rates['USD']; | |
$euro = $rates['EUR']; | |
update_option( '_curr_usdd', $dollar ); | |
update_option( '_curr_euro', $euro ); | |
update_option('_curr_date_upd', date('l jS F Y')); | |
} else{ | |
$dollar = get_option( '_curr_usdd'); | |
$euro = get_option( '_curr_euro'); | |
} | |
$args = array( | |
'posts_per_page' => -1, | |
'post_type' => 'product' | |
); | |
$loop = new WP_Query( $args ); | |
if ( $loop->have_posts() ): while ( $loop->have_posts() ): $loop->the_post(); | |
global $product; | |
global $post; | |
$UAH = get_post_meta( $product->get_id(), '_uah_price', true ); | |
$USD = get_post_meta( $product->get_id(), '_usd_price', true ); | |
$EUR = get_post_meta( $product->get_id(), '_euro_price', true ); | |
$custom_price = 0; | |
if (($UAH != "") && ($UAH != 0)) { | |
$custom_price = $UAH; | |
} | |
if (($USD != "") && ($USD != 0)) { | |
$custom_price = $USD * $dollar; | |
} | |
if (($EUR != "") && ($EUR != 0)) { | |
$custom_price = $EUR * $euro; | |
} | |
$custom_price = round($custom_price, 2); // округляем до сотых, чтобы в regular_price не записывались огромные дроби | |
update_post_meta( $product->get_id(), '_regular_price', (float)$custom_price ); | |
update_post_meta( $product->get_id(), '_price', (float)$custom_price ); | |
if( $product->is_type('variable') ){ | |
foreach( $product->get_available_variations() as $variation_values ){ | |
$variation_id = $variation_values['variation_id']; // variation id | |
$UAH = get_post_meta( $variation_id, '_uah_price', true ); | |
$USD = get_post_meta( $variation_id, '_usd_price', true ); | |
$EUR = get_post_meta( $variation_id, '_euro_price', true ); | |
$custom_price = 0; | |
if (($UAH != "") && ($UAH != 0)) { | |
$custom_price = $UAH; | |
} | |
if (($USD != "") && ($USD != 0)) { | |
$custom_price = $USD * $dollar; | |
} | |
if (($EUR != "") && ($EUR != 0)) { | |
$custom_price = $EUR * $euro; | |
} | |
$custom_price = round($custom_price, 2); // округляем до сотых, чтобы в regular_price не записывались огромные дроби | |
// Updating active price and regular price | |
update_post_meta( $variation_id, '_regular_price', $custom_price ); | |
update_post_meta( $variation_id, '_price', $custom_price ); | |
wc_delete_product_transients( $variation_id ); // Clear/refresh the variation cache | |
} | |
// Clear/refresh the variable product cache | |
wc_delete_product_transients( $post->ID ); | |
} | |
endwhile; endif; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment