php - 显示特定用户角色的含税或不含税的 WooCommerce 产品价格
问题描述
我有这段代码,它显示了“客户”用户角色的含税价格和“customer_2”用户角色的不含税价格。
它适用于“简单产品”。但它根本不适用于 "Variable Products"。我需要对此代码进行哪些更改以确保它适用于两种类型的产品(简单和可变)
function cowo_edit_price_display($price) {
global $product;
$user = wp_get_current_user();
$price_excl = $product->get_price_excluding_tax(); // price without VAT
$price_incl = $product->get_price_including_tax(); // price included VAT
if ( in_array( 'customer', (array) $user->roles ) ) {
//The user has the "customer" role
$price = wc_price($price_excl);
return $price;
}
if ( in_array( 'customer_2', (array) $user->roles ) ) {
//The user has the "customer_2" role
$price = wc_price($price_excl);
return $price;
}
return $price;
}
add_filter('woocommerce_get_price_html', 'cowo_edit_price_display', 30, 2);
解决方案
自 WooCommerce 3 以来,您的代码有点过时了……代码句柄处理简单产品、可变产品和产品变化价格。有2种情况:
1)。显示的特定用户角色的不含税价格:
您的“在商店中显示价格”的Woocommerce设置为“含税”。
add_filter('woocommerce_get_price_html', 'display_prices_excl_taxes_user_role', 100, 2 );
function display_prices_excl_taxes_user_role( $price_html, $product ) {
// Here define your user roles in the array
$targeted_user_roles = array('customer_2', 'administrator');
$user = wp_get_current_user(); // The WP_User Object
// For specific user roles (price excluding taxes)
if ( array_intersect( $user->roles, $targeted_user_roles ) ) {
// Simple products and products variation
if( $product->is_type('simple') || $product->is_type('variation') ) {
if( $product->is_on_sale() ) {
$regular_price = wc_get_price_excluding_tax( $product, array( 'price' => $product->get_regular_price() ) );
$price_html = wc_format_sale_price( $regular_price, wc_get_price_excluding_tax( $product ) );
} else {
$price_html = wc_price( wc_get_price_excluding_tax( $product ) );
}
$price_html .= $product->get_price_suffix();
}
// variable pproducts
elseif( $product->is_type('variable') ) {
$prices = $product->get_variation_prices( true );
if ( ! empty( $prices['price'] ) ) {
$act_keys = array_keys($prices['price']);
$reg_keys = array_keys($prices['regular_price']);
$min_price = wc_get_price_excluding_tax( wc_get_product(current($act_keys)));
$max_price = wc_get_price_excluding_tax( wc_get_product(end($act_keys)));
$min_reg_price = wc_get_price_excluding_tax( wc_get_product(current($reg_keys)));
$max_reg_price = wc_get_price_excluding_tax( wc_get_product(end($reg_keys)));
if ( $min_price !== $max_price ) {
$price_html = wc_format_price_range( $min_price, $max_price );
} elseif ( $product->is_on_sale() && $min_reg_price === $max_reg_price ) {
$price_html = wc_format_sale_price( wc_price( $max_reg_price ), wc_price( $min_price ) );
} else {
$price_html = wc_price( $min_price );
}
$price_html .= $product->get_price_suffix();
}
}
}
return $price_html;
}
2)。显示的价格包括特定用户角色的税费:
您的“在商店中显示价格”的 Woocommerce设置是“不含税”。
add_filter('woocommerce_get_price_html', 'display_prices_incl_taxes_user_role', 100, 2 );
function display_prices_incl_taxes_user_role( $price_html, $product ) {
// Here define your user roles in the array
$targeted_user_roles = array('customer_2', 'administrator');
$user = wp_get_current_user(); // The WP_User Object
// For specific user roles (price excluding taxes)
if ( array_intersect( $user->roles, $targeted_user_roles ) ) {
// Simple products and products variation
if( $product->is_type('simple') || $product->is_type('variation') ) {
if( $product->is_on_sale() ) {
$regular_price = wc_get_price_including_tax( $product, array( 'price' => $product->get_regular_price() ) );
$price_html = wc_format_sale_price( $regular_price, wc_get_price_including_tax( $product ) );
} else {
$price_html = wc_price( wc_get_price_including_tax( $product ) );
}
$price_html .= $product->get_price_suffix();
}
// variable pproducts
elseif( $product->is_type('variable') ) {
$prices = $product->get_variation_prices( true );
if ( ! empty( $prices['price'] ) ) {
$act_keys = array_keys($prices['price']);
$reg_keys = array_keys($prices['regular_price']);
$min_price = wc_get_price_including_tax( wc_get_product(current($act_keys)));
$max_price = wc_get_price_including_tax( wc_get_product(end($act_keys)));
$min_reg_price = wc_get_price_including_tax( wc_get_product(current($reg_keys)));
$max_reg_price = wc_get_price_including_tax( wc_get_product(end($reg_keys)));
if ( $min_price !== $max_price ) {
$price_html = wc_format_price_range( $min_price, $max_price );
} elseif ( $product->is_on_sale() && $min_reg_price === $max_reg_price ) {
$price_html = wc_format_sale_price( wc_price( $max_reg_price ), wc_price( $min_price ) );
} else {
$price_html = wc_price( $min_price );
}
$price_html .= $product->get_price_suffix();
}
}
}
return $price_html;
}
代码在您的活动子主题(或活动主题)的functions.php 文件中。测试和工作。
推荐阅读
- javascript - 如何制作一排复选框,每个复选框显示不同的文本?
- javascript - 为什么不响应本机注册用户按下按钮?
- c# - 使用注入到类中的 JsonSerializerOptions 编写单元测试
- python - 从 Python 中的运行基线中减去和添加值
- swiftui - Swiftui 继续使用 Facebook 失败
- visual-studio-code - VSCode 选项卡下的蓝色移动条
- excel - 使用VBA按文件名的指定子字符串对文件夹中的文件进行排序
- r - 连接中的自引用和按引用更新有什么区别?
- kubernetes - 保险柜机密中的气流配置不起作用
- php - Laravel 无法提交到数据库,因为参数太少而无法运行