gpt4 book ai didi

php - 针对特定用户角色显示含税或不含税的 WooCommerce 产品价格

转载 作者:行者123 更新时间:2023-12-05 06:13:55 27 4
gpt4 key购买 nike

我有这段代码,它显示“客户”用户角色的含税价格已包含,以及“customer_2”用户角色的含税价格不包含

它非常适合“简单产品”。但它根本不适用于“可变产品”。我需要对此代码做哪些更改以确保它适用于两种类型的产品(简单和可变)

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). 针对特定用户角色显示的不含税价格:

Your Woocommerce setting for "Display prices in the shop" is "Including taxes".

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). 针对特定用户角色显示的含税价格:

Your Woocommerce setting for "Display prices in the shop" is "Excluding taxes".

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 文件。经过测试并有效。

关于php - 针对特定用户角色显示含税或不含税的 WooCommerce 产品价格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63134278/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com