Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Woocommerce 如何根据用户角色显示不同的产品价格(尤其是可变产品)?_Woocommerce_Hook Woocommerce_User Roles - Fatal编程技术网

Woocommerce 如何根据用户角色显示不同的产品价格(尤其是可变产品)?

Woocommerce 如何根据用户角色显示不同的产品价格(尤其是可变产品)?,woocommerce,hook-woocommerce,user-roles,Woocommerce,Hook Woocommerce,User Roles,我想为商业中的不同角色显示产品的价格。为了实现这个目标,我通过一个输入字段为产品在post meta中设置了一个带有价格元值的meta_键。然后,我使用下面的代码筛选所需角色的该产品的显示价格。该角色是客户2 例:对于产品,一般价格是200美元,对于客户,价格是150美元 而产品的常规价格是350美元,客户的2场演出是200美元,以此类推 function custom_price($price, $product){ $product_co_price = get_post_meta($pro

我想为商业中的不同角色显示产品的价格。为了实现这个目标,我通过一个输入字段为产品在post meta中设置了一个带有价格元值的meta_键。然后,我使用下面的代码筛选所需角色的该产品的显示价格。该角色是客户2

例:对于产品,一般价格是200美元,对于客户,价格是150美元 而产品的常规价格是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 );
存储价格元值的元键中的“\u co\u price”


但是什么也没发生

函数中只有两个小错误:

在下一行中,您无法正确访问
$product->ID
属性。您应该使用
$product->get_id()
方法:

$product_co_price = get_post_meta($product->ID, '_co_price');
此外,如果在
get\u post\u 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;
}
如果在可变产品页面上显示价格范围,则需要根据自定义元产品
\u co\u价格
更改逻辑

因此,您需要使用:

// 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中。

我使用了你的函数,但什么都没有发生。它对简单产品有效,但对变量无效products@WebDesign它对我有用。您是否为每个产品变体添加了自定义meta
\u co\u price
?您可以再说一遍!我没有为不同的产品加上价格!我会的。非常感谢。woocommerce\u product\u variation\u get\u price filter的角色是什么?