change the variable product price while adding in cart in woocommerce

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.

Read More

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

Related posts

3 comments

  1. Try with this hooks-

    add_filter('woocommerce_variable_sale_price_html', 'my_html' , 10, 2);
    add_filter('woocommerce_variable_price_html',  'my_html' , 10, 2);
    
  2. 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){

            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);
        }
    }
    

    }

  3. 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 );
    

Comments are closed.