admin管理员组文章数量:1426072
I have a very specific question. In Belgium we need to add a BEBAT tax when you sell batteries. I'm now trying to figure out how I can tackle this.
The code below works until a certain part so I hope I'm close but I can't seem to find the issue and hope you guys can help me.
I'm checking via IP to see what country you're in. Based on country, I want to add an extra fee of (for example) .99 and add this only on the products that have a certain category (for example batteries).
But with the code below, it shows on all products even if they are not in the category.
I also need to figure out how I can add this as text on the product and show the same message on the invoice. All tips are welcome!
function add_tax_bebat() {
$getIp = $_SERVER['REMOTE_ADDR'];
$ipDetails = json_decode( @file_get_contents( "/{$getIp}/json" ) );
$country = $ipDetails->country;
if ( $country =="BE" ){
function return_custom_price($price, $products) {
$args = array(
'category' => array( 'batteries' ),
);
$products = wc_get_products( $args );
$price = ($price+0.99);
return $price;
}
add_filter('woocommerce_get_price', 'return_custom_price', 10, 2);
}
}
add_action ( 'init', 'add_tax_bebat' );
I have a very specific question. In Belgium we need to add a BEBAT tax when you sell batteries. I'm now trying to figure out how I can tackle this.
The code below works until a certain part so I hope I'm close but I can't seem to find the issue and hope you guys can help me.
I'm checking via IP to see what country you're in. Based on country, I want to add an extra fee of (for example) .99 and add this only on the products that have a certain category (for example batteries).
But with the code below, it shows on all products even if they are not in the category.
I also need to figure out how I can add this as text on the product and show the same message on the invoice. All tips are welcome!
function add_tax_bebat() {
$getIp = $_SERVER['REMOTE_ADDR'];
$ipDetails = json_decode( @file_get_contents( "http://ipinfo.io/{$getIp}/json" ) );
$country = $ipDetails->country;
if ( $country =="BE" ){
function return_custom_price($price, $products) {
$args = array(
'category' => array( 'batteries' ),
);
$products = wc_get_products( $args );
$price = ($price+0.99);
return $price;
}
add_filter('woocommerce_get_price', 'return_custom_price', 10, 2);
}
}
add_action ( 'init', 'add_tax_bebat' );
Share
Improve this question
edited May 24, 2019 at 21:18
LoicTheAztec
3,39117 silver badges24 bronze badges
asked May 21, 2019 at 13:06
FlintFlint
433 bronze badges
1 Answer
Reset to default 1WooCommerce has already a Geo IP feature that you can use through WC_Geolocation
Class.
I have revisited completely your code. To target a product category you can use WordPress has_term()
function on WooCommerce Product Category custom taxonomy as follow:
function is_geo_country_belgium() {
// Get an instance of the WC_Geolocation object class
$geolocation_instance = new WC_Geolocation();
// Get user IP
$user_ip_address = $geolocation_instance->get_ip_address();
// Get geolocated user IP country code.
$user_geolocation = $geolocation_instance->geolocate_ip( $user_ip_address );
return $user_geolocation['country'] === 'BE' ? true : false;
}
add_filter( 'woocommerce_get_price', 'change_specific_products_price', 10, 2 );
function change_specific_products_price( $price, $product ) {
$category = 'batteries';
if ( has_term( $category, 'product_cat', $product->get_id() ) && is_geo_country_belgium() ) {
$price += 0.99;
}
return $price;
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
本文标签: customizationChange WooCommerce product price based on category and GEO IP country
版权声明:本文标题:customization - Change WooCommerce product price based on category and GEO IP country 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745466972a2659573.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论