【问题标题】:How to show different product price based on user role (especially for variable products)? [duplicate]如何根据用户角色(尤其是可变产品)显示不同的产品价格? [复制]
【发布时间】:2021-05-10 11:08:39
【问题描述】:

我想显示 woocommerce 中不同角色的产品价格。为了这个目标,我通过一个输入字段在 post meta 中为产品设置了一个 meta_key,其中 meta_key 为 price 的 meta 值。然后我使用以下代码过滤该产品的显示价格以获得所需的角色。角色是customer_2

例如:对于产品_A 客户的正常价格为 200 美元_2 显示为 150 美元 而product_B 的正常价格是350 美元,客户_2 显示200 美元,依此类推...

function custom_price($price, $product){
$product_co_price = get_post_meta($product->ID, '_co_price');
$user = wp_get_current_user();
if($product_co_price){
    if(in_array('customer_2', (array) $user->roles)){
        $price = $product_co_price;
    }
}
return $price;}
add_filter('woocommerce_product_get_price', 'custom_price', 10, 2);
add_filter('woocommerce_product_variation_get_price', 'custom_price', 10, 2 );
add_filter( 'woocommerce_product_get_regular_price', 'custom_price', 10, 2 );
add_filter( 'woocommerce_product_get_sale_price', 'custom_price', 10, 2 );

'_co_price' 在存储价格元值的元键中。

但是什么也没发生!

【问题讨论】:

    标签: woocommerce hook-woocommerce user-roles


    【解决方案1】:

    你的函数只有两个小错误:

    在以下行中,您错误地访问了$product->ID 属性。你应该使用$product->get_id() 方法:

    $product_co_price = get_post_meta($product->ID, '_co_price');
    

    此外,如果在get_post_meta() 函数中,您没有将第三个参数设为 true,您将返回错误,因为在以下几行中您将比较当前价格(字符串) 以及从自定义元(数组)中获得的价格。欲了解更多信息:

    所以正确的函数是:

    // shows a customized price based on the user role
    add_filter( 'woocommerce_product_get_price', 'custom_price', 10, 2);
    add_filter( 'woocommerce_product_variation_get_price', 'custom_price', 10, 2 );
    add_filter( 'woocommerce_product_get_regular_price', 'custom_price', 10, 2 );
    add_filter( 'woocommerce_product_get_sale_price', 'custom_price', 10, 2 );
    function custom_price( $price, $product ) {
    
        // if the user is not logged in or has a role other than "customer_2", it shows the default price of the product
        $user = wp_get_current_user();
        if ( ! in_array( 'customer_2', (array) $user->roles ) ) {
            return $price;
        }
    
        // gets the price to display for the user role "customer_2"
        $co_price = get_post_meta( $product->get_id(), '_co_price', true );
    
        // if the custom meta is not set or is empty or equal to zero it shows the default price
        if ( ! isset( $co_price ) || empty( $co_price ) || $co_price == 0 ) {
            return $price;
        // otherwise it shows the custom price
        } else {
            return $co_price;
        }
    
        return $price;
    }
    

    如果您在可变产品页面上显示价格范围,您需要根据自定义元产品 _co_price 更改逻辑。

    所以你需要使用:

    // shows the price range of the variable product based on the price of the custom meta "_co_price" of each single variation
    add_filter( 'woocommerce_variable_price_html', 'show_price_range_based_on_custom_meta_price', 99, 2 );
    function show_price_range_based_on_custom_meta_price( $price, $product ) {
    
        // initializes the array which will contain all variation prices
        $prices = array();
    
        $variation_ids = $product->get_children();
        foreach ( $variation_ids as $variation_id ) {
            $variation = wc_get_product( $variation_id );
            // gets the net default price
            $prices[] = $variation->get_price();
            // gets the price from the custom meta product
            $co_price = $variation->get_meta( '_co_price', true );
            // replaces the comma with the period to make it numeric
            $co_price = str_replace( ',', '.', $co_price );
            $prices[] = $co_price;
        }
    
        // removes empty values from the array
        $prices = array_filter($prices);
        // gets the minimum value from the array
        $min_price = min( $prices );
        // gets the maximum value from the array
        $max_price = max( $prices );
    
        if ( $min_price == $max_price ) {
            $price = wc_price( $min_price );
        } else {
            $price = wc_format_price_range( $min_price, $max_price );
        }
        
        return $price;
    }
    

    代码已经过测试并且可以运行。将其添加到活动主题的 functions.php 中。

    【讨论】:

    • 我使用了你的功能,但没有任何反应
    • 它适用于简单产品,但不适用于可变产品
    • @WebDesign 它对我有用。您是否为每个单独的产品变体添加了自定义元 _co_price
    • 你可以再说一遍!我没有为可变产品添加 _co_price!我去做。非常感谢。
    • woocommerce_product_variation_get_price 过滤器的作用是什么?
    猜你喜欢
    • 2022-12-14
    • 2017-11-24
    • 1970-01-01
    • 2015-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-10
    • 1970-01-01
    相关资源
    最近更新 更多