I have created custom meta fields to add custom price according to user roles for both simple product and variable type product.
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;
}
}
I use above code to change price in front end according to user roles for simple type products and it works correctly . its show change price and also when I click on add to cart button its add changed price in cart.
In case of variable product I use below code
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));
}
}
}
from this variation price changed according to user roles but the problem is that when I clicked on add to cart button its add 0.00 price in cart for all variable product
so please if u have any idea then solve this.
Thanks & Regards
Suresh Kumar
Try with this hooks-
Thanks for your support I used below code and ot solved my problems.
add_filter(‘woocommerce_get_price’,’change_price’, 10, 2);
function change_price($price, $productd){
}