我已经创建了自定义元字段,以便根据简单产品和可变类型产品的用户角色添加自定义价格。
add_filter('woocommerce_get_price','change_price', 10, 2);
add_filter('woocommerce_get_regular_price','change_price', 10, 2);
add_filter('woocommerce_get_sale_price','change_price', 10, 2);
function change_price($price, $productd){
get_currentuserinfo;
global $current_user;
$category = $current_user->roles[0];
if($productd->product_type == 'simple')
{
if($category == 'gastronomy' || $category == 'corporate' || $category == 'retail' || $category == 'distributor') {
$price = get_post_meta( $productd->id, $category.'_price',true);
}
return $price;
}
}
我使用上面的代码来改变价格在前端根据用户角色的简单类型的产品,它的工作正常。它显示更改价格,而且当我点击添加到购物车按钮时,它在购物车中添加更改的价格。
对于变量产品,我使用下面的代码
add_filter( 'woocommerce_variation_sale_price_html', 'my_html', 10, 2);
add_filter( 'woocommerce_variation_price_html', 'my_html', 10, 2);
add_filter( 'woocommerce_get_variation_price_html', 'my_html', 10, 2);
function my_html( $price, $variation ) {
get_currentuserinfo;
global $current_user;
$category = $current_user->roles[0];
if ( $variation->product_type == 'variation' ) {
if($category == 'gastronomy' || $category == 'corporate' || $category == 'retail' || $category == 'distributor') {
$price = get_post_meta( $variation->variation_id, $category.'_price',true);
return woocommerce_price($price);
}
else
{
return woocommerce_price(get_post_meta( $variation->variation_id, '_regular_price',true));
}
}
}
这个变量的价格根据用户角色而变化,但问题是,当我点击添加到购物车按钮时,它会为所有可变产品添加0.00价格
所以如果你有任何想法,请解决这个问题。
谢谢,问候苏雷什·库马尔
试试这个钩子-
add_filter('woocommerce_variable_sale_price_html', 'my_html' , 10, 2);
add_filter('woocommerce_variable_price_html', 'my_html' , 10, 2);
感谢您的支持,我使用下面的代码并没有解决我的问题。
add_filter('woocommerce_get_price','change_price', 10,2);
function change_price($price, $productd){
get_currentuserinfo();
global $current_user;
$category = $current_user->roles[0];
if($productd->product_type == 'simple')
{
if($category == 'gastronomy' || $category == 'corporate' || $category == 'retail' || $category == 'distributor') {
$custome_price = get_post_meta( $productd->id, $category.'_price',true);
return $custome_price;
}
else
{
return $price;
}
}
if ( $productd->product_type == 'variation' || $productd->product_type == 'variable' ) {
if($category == 'gastronomy' || $category == 'corporate' || $category == 'retail' || $category == 'distributor') {
$custom_price = get_post_meta( $productd->variation_id, $category.'_price',true);
return $custom_price;
}
else
{
return get_post_meta( $productd->variation_id, '_regular_price',true);
}
}
}
function m_varient_price( $price, $variation ) {
if ( $variation->product_type == 'variation' ) {
$user = $user ? new WP_User( $user ) : wp_get_current_user();
$role = $user->roles[0];
if($role == 'distributor'){$pricex = get_post_meta( $variation->variation_id, '_dist_field',true);}
else if($role == 'reseller'){$price = get_post_meta( $variation->variation_id, '_rese_field',true);}
else{ $pricex = $price;}
}
return $pricex;
}
add_filter( 'woocommerce_product_variation_get_regular_price', 'm_varient_price' , 99, 2 );
add_filter( 'woocommerce_product_variation_get_sale_price', 'm_varient_price' , 99, 2 );
add_filter( 'woocommerce_product_variation_get_price', 'm_varient_price', 99, 2 );