2
votes

I modified a function to create custom prices for some of my members i.e. the normal price is $1 but if you're a bronze member it's $2, a silver member $3, etc.

The prices are changed on the shop and single product page. When the product is added to the cart, however, the price reverts to the original amount. Is there additional code I should be including to have the price accurately changed all the way through checkout and billing?

// Variations (of a variable product)
add_filter('woocommerce_variation_prices_price', 'custom_variation_price', 99, 3 );
add_filter('woocommerce_variation_prices_regular_price', 'custom_variation_price', 99, 3 );
function custom_variation_price( $price, $variation, $product ) {

global $product;
$id = $product->get_id();

$user_id = get_current_user_id();
$plan_id = 1628;

  if ( wc_memberships_is_user_member( $user_id, $plan_id )  ) {

  $new = $price * 2;  

  return ($new);
  }

}
1

1 Answers

3
votes

With your code you are just changing the displayed variation price range. So you will need a bit more for this:

// Simple, grouped and external products
add_filter('woocommerce_product_get_price', 'custom_price', 90, 2 );
add_filter('woocommerce_product_get_regular_price', 'custom_price', 90, 2 );

// Product variations (of a variable product)
add_filter('woocommerce_product_variation_get_regular_price', 'custom_price', 99, 2 );
add_filter('woocommerce_product_variation_get_price', 'custom_price', 90, 2 );

// Variable product price ramge
add_filter('woocommerce_variation_prices_price', 'custom_variation_price', 90, 3 );
add_filter('woocommerce_variation_prices_regular_price', 'custom_variation_price', 90, 3 );

function custom_price( $price, $product ) {
    // Only logged in users
    if ( ! is_user_logged_in() ) return $price; 

    // HERE the defined plan ID
    $plan_id = 1628;

    if ( wc_memberships_is_user_member( get_current_user_id(), $plan_id )  ) {
        $price *= 2; // set price x 2
    }
    return $price;
}

function custom_variation_price( $price, $variation, $product ) {
    // Only logged in users
    if ( ! is_user_logged_in() ) return $price; 

    // HERE the defined plan ID
    $plan_id = 1628;

    if ( wc_memberships_is_user_member( get_current_user_id(), $plan_id )  ) {
        $price *= 2; // set price x 2
    }
    return $price;
}

Code goes in function.php file of the active child theme (or active theme).

Tested and works on woocommerce 3+

Now custom prices in cart will be also reflected