4

I´m using hooks for customizing variable product prices.

However, this answer does not seem to be working for Woocommerce 3.3.5.

I use following (from the previous post) on my functions.php file:

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 ) {
    // Delete product cached price  (if needed)
wc_delete_product_transients($variation->get_id());

return $price * 3; // X2 for testing
}

On product page the price range for product variations shows correct prices (original price*3) but when I select options, price shown for variation is unfiltered. Am I missing something here?


EDIT: I had a slightly different price calculation for simple products and for product variations. I ended up with 3 functions:

// Simple
add_filter('woocommerce_product_get_price', 'custom_price', 99, 2 );
add_filter('woocommerce_product_get_regular_price', 'custom_price', 99, 2 );

// Variable
add_filter('woocommerce_product_variation_get_regular_price', 'custom_price_2', 99, 2 );
add_filter('woocommerce_product_variation_get_price', 'custom_price_2' , 99, 2 );

// 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_price( $price, $product ) {
    // Delete product cached price  (if needed)
    wc_delete_product_transients($product->get_id());

    return $price * 3; // X3 for testing
}

function custom_price_2( $price, $product ) {
    // Delete product cached price  (if needed)
    wc_delete_product_transients($product->get_id());

    return $price * 2; // X2 for testing
}

function custom_variation_price( $price, $variation, $product ) {
    // Delete product cached price  (if needed)
    wc_delete_product_transients($variation->get_id());


    return $price * 2; // X2 for testing
}
7uc1f3r
  • 28,449
  • 17
  • 32
  • 50
Daisy
  • 43
  • 1
  • 1
  • 4

2 Answers2

5

The linked code still work for Woocommerce 3.3.x, see this related recent accepted working answer tested on Woocommerce 3.3.x … Your code is just uncompleted…

You need to use:

// Variable
add_filter('woocommerce_product_variation_get_regular_price', 'custom_price', 99, 2 );
add_filter('woocommerce_product_variation_get_price', 'custom_price' , 99, 2 );

// 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_price( $price, $product ) {
    // Delete product cached price  (if needed)
    wc_delete_product_transients($product->get_id());

    return $price * 3; // X3 for testing
}

function custom_variation_price( $price, $variation, $product ) {
    // Delete product cached price  (if needed)
    wc_delete_product_transients($variation->get_id());

    return $price * 3; // X3 for testing
}
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thank you! I had the other part too but must have had some copy-paste error on that. – Daisy Apr 23 '18 at 05:38
  • @LoicTheAztec is there anyway to do this but only on cart/checkout? My variation price is based on the users inputs on the product page. So I use JS to update price on product page. But am unable to find a function to change the price when it comes to cart/checkout. – Jack Robson Apr 11 '19 at 15:48
  • I've posted more details at https://stackoverflow.com/questions/55636612/display-custom-price-for-variation-product-on-cart-checkout – Jack Robson Apr 11 '19 at 15:54
0
// Simple
add_filter('woocommerce_product_get_price', 'custom_price', 99, 2 );
add_filter('woocommerce_product_get_regular_price', 'custom_price', 99, 2 );
// Variable
add_filter('woocommerce_product_variation_get_regular_price', 'custom_price', 99, 2 );
add_filter('woocommerce_product_variation_get_price', 'custom_price' , 99, 2 );
// 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_price( $price, $product ) {
    // Delete product cached price  (if needed)
    wc_delete_product_transients($product->get_id());

    return $price * 3; // X3 for testing
}

function custom_variation_price( $price, $variation, $product ) {
    // Delete product cached price  (if needed)
    wc_delete_product_transients($variation->get_id());

    return $price * 3; // X3 for testing
}
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 28 '22 at 07:39