1

I have modified my product price based on a user who is logged in with a "reseller" role. This works for my default products, but not for my product variation products.

So my product price for a 50ml variation of the product is 9.20. A "reseller" should pay 6.16 (9.20 * 0.67). The output of my variation product is still 9.20..

Filter hook:

function pr_reseller_price( $price, $product ) {

   if ( ! is_user_logged_in() )
      return $price;

   // Function which checks if logged in user is reseller
   if ( pr_has_role( 'reseller' ) ) {
      $price = $price * 0.67;
   }
   return $price;

}
add_filter( 'woocommerce_get_price', 'pr_reseller_price', 10, 2 );
add_filter( 'woocommerce_get_regular_price', 'pr_reseller_price', 10, 2 );
add_filter( 'woocommerce_get_sale_price', 'pr_reseller_price', 10, 2 );

Product vs Product variation

// Product
$product_id = 69;
$product = wc_get_product( $product_id );
echo $product->get_price(); // Returns 6.16 (9.20 * 0.67)

// Product variation
$variations = $product->get_available_variations();
$variation = new WC_Product_Variation( $variations[0]['variation_id'] );
echo $variation->get_price(); // Returns 9.20
2

I had to add the filter hook for product variation as well.

add_filter( 'woocommerce_product_get_price', 'pr_reseller_price', 10, 2 );
add_filter( 'woocommerce_product_variation_get_price', 'pr_reseller_price', 10, 2 );
add_filter( 'woocommerce_product_get_regular_price', 'pr_reseller_price', 10, 2 );
add_filter( 'woocommerce_product_get_sale_price', 'pr_reseller_price', 10, 2 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.