Change product variation prices via a hook in WooCommerce 3.3

12,858

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
}
Share:
12,858
Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

    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
    }
    
  • Admin
    Admin about 6 years
    Thank you! I had the other part too but must have had some copy-paste error on that.
  • Jack Robson
    Jack Robson about 5 years
    @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
    Jack Robson about 5 years
    I've posted more details at stackoverflow.com/questions/55636612/…