Last active
June 10, 2017 13:29
-
-
Save goranefbl/fe39785872515248db6c to your computer and use it in GitHub Desktop.
WooCommerce = Price per country
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 | |
// Add Canada Price to Woo as custom field and change per country. | |
// You can add as many as you need | |
add_action( 'woocommerce_product_options_pricing', 'add_can_price_box' ); | |
function add_can_price_box() { | |
woocommerce_wp_text_input( array( 'id' => 'can_price', 'class' => 'wc_input_price_extra_info short', 'label' => __( 'Price in Canadian $', 'woocommerce' ) ) ); | |
} | |
// This one is for saving above fields | |
add_action('woocommerce_process_product_meta', 'save_can_price', 2, 2); | |
function save_can_price($post_id, $post) { | |
update_post_meta($post_id, 'can_price', stripslashes($_POST['can_price'])); | |
} | |
// Here is the logic for price, if u have multiple just do switch and thats it. | |
if(!is_admin()) { | |
add_filter( 'woocommerce_get_price', 'change_price', 10, 2 ); | |
function change_price($price, $product) { | |
global $woocommerce; | |
$customer_country = $woocommerce->customer->get_country(); | |
if($customer_country == "CA") { | |
return get_post_meta($product->id, 'can_price', true); | |
} else { | |
return $price; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment