我需要根据帐单国家/地区更改货币,使用以下代码可以以某种方式进行操作,但它仅适用于简单的产品,不适用于可变产品。
add_filter('woocommerce_get_price', 'return_custom_price', $product, 2);
function return_custom_price($price, $product) {
global $post, $woocommerce;
// Array containing country codes
$county = array('US');
// Amount to increase by
//$amount = 5;
// If the custromers shipping country is in the array
if ( in_array( $woocommerce->customer->get_billing_country(), $county ) && is_checkout() ){
// Return the price plus the $amount
return $new_price = ($price/260)*1.25;
} else {
// Otherwise just return the normal price
return $price;
}
}
挂钩
woocommerce_get_price
自 WooCommerce 3 起已过时并已弃用,并已替换为以下挂钩:
woocommerce_product_get_price
(针对产品)woocommerce_product_variation_get_price
(针对可变产品的变体)。您的代码中还有一些其他错误。尝试以下修改后的代码:
add_filter('woocommerce_product_get_price', 'country_based_cart_item_price', 100, 2);
add_filter('woocommerce_product_variation_get_price', 'country_based_cart_item_price', 100, 2);
function country_based_cart_item_price( $price, $product ) {
// Define below in the array the desired country codes
$targeted_countries = array('US');
$billing_country = WC()->customer->get_billing_country();
if ( is_checkout() && in_array($billing_country, $targeted_countries) ){
// Returns changed price
return $price / 260 * 1.25;
}
return $price;
}
代码位于子主题的functions.php 文件中(或插件中)。已测试并有效。