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

问题描述 投票:0回答:1

我有这段代码,显示“客户”用户角色的含税价格包含和“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);
php woocommerce hook-woocommerce user-roles
1个回答
1
投票

自 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 文件中。已测试并有效。

© www.soinside.com 2019 - 2024. All rights reserved.