Changing the price of goods through a hook in WooCommerce 3

In WooCommerce, I need to multiply the prices of all products by a number. So I used the following (through the plugin):

add_filter('woocommerce_get_regular_price', array( $this, 'my_custom_price'), 99);
add_filter('woocommerce_get_price', array( $this, 'my_custom_price'), 99);

function my_custom_price( $original_price ) {
  global $post, $woocommerce;

  //Logic for calculating the new price here
  $new_price = $original_price * 2;

  //Return the new price (this is the price that will be used everywhere in the store)
  return $new_price;
 }

But that does not work for variation products. I tried the following interceptors with no luck:

add_filter('woocommerce_get_variation_regular_price', array( $this, 'my_custom_price'), 99);
add_filter('woocommerce_get_variation_price', array( $this, 'my_custom_price'), 99);

The only one that works halfway is the following:

add_filter('woocommerce_variation_prices_price', array( $this, 'my_custom_price'), 99);

But that just changed the overall price, not the chosen price of the variation. See the image below for the price of BsF. 200, and the total price is right, 200 x 2 = 400, but the variational price when choosing still shows 200:

Note. I need this to really change, so html hooks will not be displayed.

Variation Price

Is there something I'm missing or is something wrong?

+11
1

Update 4 (September 2019)

  • 2 ( Woocommerce 3.3.x)
  • Woocommerce 3 ( ):
    woocommerce_get_variation_prices_hash , wc_delete_product_transients()
  • (. ).

1) :

WooCommerce 3+

, , :

## The following goes inside the constructor ##

// Simple, grouped and external products
add_filter('woocommerce_product_get_price', array( $this, 'custom_price' ), 99, 2 );
add_filter('woocommerce_product_get_regular_price', array( $this, 'custom_price' ), 99, 2 );
// Variations 
add_filter('woocommerce_product_variation_get_regular_price', array( $this, 'custom_price' ), 99, 2 );
add_filter('woocommerce_product_variation_get_price', array( $this, 'custom_price' ), 99, 2 );

// Variable (price range)
add_filter('woocommerce_variation_prices_price', array( $this, 'custom_variable_price' ), 99, 3 );
add_filter('woocommerce_variation_prices_regular_price', array( $this, 'custom_variable_price' ), 99, 3 );

// Handling price caching (see explanations at the end)
add_filter( 'woocommerce_get_variation_prices_hash', array( $this, 'add_price_multiplier_to_variation_prices_hash' ), 99, 1 );


## This goes outside the constructor ##

// Utility function to change the prices with a multiplier (number)
public function get_price_multiplier() {
    return 2; // x2 for testing
}

public function custom_price( $price, $product ) {
    return $price * get_price_multiplier();
}

public function custom_variable_price( $price, $variation, $product ) {
    return $price * get_price_multiplier();
}

public function add_price_multiplier_to_variation_prices_hash( $hash ) {
    $hash[] = get_price_multiplier();
    return $hash;
}

() WooCommerce 3+.


2) : functions.php ( ):

// Utility function to change the prices with a multiplier (number)
function get_price_multiplier() {
    return 2; // x2 for testing
}

// Simple, grouped and external products
add_filter('woocommerce_product_get_price', 'custom_price', 99, 2 );
add_filter('woocommerce_product_get_regular_price', 'custom_price', 99, 2 );
// Variations
add_filter('woocommerce_product_variation_get_regular_price', 'custom_price', 99, 2 );
add_filter('woocommerce_product_variation_get_price', 'custom_price', 99, 2 );
function custom_price( $price, $product ) {
    return $price * get_price_multiplier();
}

// Variable (price range)
add_filter('woocommerce_variation_prices_price', 'custom_variable_price', 99, 3 );
add_filter('woocommerce_variation_prices_regular_price', 'custom_variable_price', 99, 3 );
function custom_variable_price( $price, $variation, $product ) {
    // Delete product cached price  (if needed)
    // wc_delete_product_transients($variation->get_id());

    return $price * get_price_multiplier();
}

// Handling price caching (see explanations at the end)
add_filter( 'woocommerce_get_variation_prices_hash', 'add_price_multiplier_to_variation_prices_hash', 99, 1 );
function add_price_multiplier_to_variation_prices_hash( $hash ) {
    $hash[] = get_price_multiplier();
    return $hash;
}

woocommerce 3+


:

  • woocommerce_product_get_sale_price (, )
  • woocommerce_variation_prices_sale_price ( (-))
  • woocommerce_product_variation_get_sale_price ( )

woocommerce 3:

, :

  • woocommerce_variation_prices_price
  • woocommerce_variation_prices_regular_price
  • woocommerce_variation_prices_sale_price

Woocommerce 3, woocommerce_get_variation_prices_hash , , .

, ( , )

.: - get_variation_prices


( ) :

  • woocommerce_price_filter_widget_min_amount $price
  • woocommerce_price_filter_widget_max_amount $price
+13

Source: https://habr.com/ru/post/1684165/


All Articles