Skip to content
Advertisement

Change product variation prices via a hook in WooCommerce 3.3

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
}

Advertisement

Answer

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
}
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement