首页 > 解决方案 > 显示特定用户角色的含税或不含税的 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);

标签: phpwoocommercehook-woocommerceuser-rolesprice

解决方案


自 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 文件中。测试和工作。


推荐阅读