Change product prices via a hook in WooCommerce 3+ - php

IN WooCommerce, I need to multiply all product prices by a number. So I have used the following (via a 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 doesn't work for variation products. I have tried the following hooks 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 half way is this one:
add_filter('woocommerce_variation_prices_price', array( $this, 'my_custom_price'), 99);
But that just changed the overall price, not the selected variation price. See the image below, price is BsF. 200 and the overall price is right, 200 x 2 = 400, but the variation price when selected still shows 200:
Note: I need it to actually change, so display html hooks wont work.
Is there anything I'm missing, or something wrong?

Update (December 2020)
2 code versions for themes and plugins (works in Woocommerce 3.3.x too)
Cached variations prices in Woocommerce 3 (Update and addition): Now using woocommerce_get_variation_prices_hash filter hook much more efficient, instead of wc_delete_product_transients()… See this related thread
Added product price filter widget hooks (see at the end).
1) Plugin version with a constructor function:
The hooks that you are using are deprecated in WooCommerce 3+
To make it work for all products prices, including variations prices, you should use this:
## 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, 3 );
## 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 (float) $price * get_price_multiplier();
}
public function custom_variable_price( $price, $variation, $product ) {
return (float) $price * get_price_multiplier();
}
public function add_price_multiplier_to_variation_prices_hash( $price_hash, $product, $for_display ) {
$price_hash[] = get_price_multiplier();
return $price_hash;
}
The code tested and perfectly works (only) in WooCommerce 3+.
2) For theme version: functions.php file on active child theme (or active theme):
// 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 (float) $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 (float) $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, 3 );
function add_price_multiplier_to_variation_prices_hash( $price_hash, $product, $for_display ) {
$price_hash[] = get_price_multiplier();
return $price_hash;
}
Tested and works on woocommerce 3+
For products in sale you have those hooks:
woocommerce_product_get_sale_price (Simple, grouped and external products)
woocommerce_variation_prices_sale_price (Variable products (min-max))
woocommerce_product_variation_get_sale_price (Products variations)
Cached prices and woocommerce 3:
The 3 filters hooks involved in variations cached prices are:
woocommerce_variation_prices_price
woocommerce_variation_prices_regular_price
woocommerce_variation_prices_sale_price
Introduced in Woocommerce 3, woocommerce_get_variation_prices_hash filter hook will allow to refresh variations cached prices in a much more efficient way, without deleting related transients anytime that this hooks are executed.
So performances will stay boosted (Thanks to Matthew Clark that pointed this better way)
See: Caching and dynamic pricing – upcoming changes to the get_variation_prices method
For filtering product prices with a widget (min and max price), use the following hooks:
woocommerce_price_filter_widget_min_amount that has one argument $price
woocommerce_price_filter_widget_max_amount that has one argument $price

Related

Problem with change product prices via a hook

I have a Woocommerce based shop.
I used This Link to Change variations product prices via a hook by LoicTheAztec.
It's working perfect, but i found that when user open product page or any page including price, it will giving error (stored in error log file and users can not see that).
Means Error is shown in Debug mode and also stored in error_log file in Root.
Error: [25-Feb-2020 11:12:57 ...] PHP Warning: A non-numeric value encountered in /home/website/public_html/wp-content/themes/name/functions.php on line 1147
Line 1147: return $price * get_price_multiplier();
This hook is used:
// Utility function to change the prices with a multiplier (number)
function get_price_multiplier() {
return 1.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 Version: 3.9.2
PHP version: 7.2
Before return you can convert $price to a numeric value.
$price = (float)$price;

When adding new product, Auto Increase the price of 10% in Woocommerce

I am using the following code in my WordPress site to increase the product price by 10%. It works fine but the issue is that the price is updated every time by 10% when making any edit in the product. I want it increases the price by 10% only when adding a new product.
I am using WC Marketplace plugin. The code works in the vendor side but not in the WordPress admin panel.
Here is the code I am using:
function get_price_multiplier() {
return get_option(10);
}
// 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 ) {
$price_per = round($price * get_price_multiplier()/100);
return round($price + $price_per);
}
Any help is appreciated.

Change programmatically products prices for a product category globally in Woocommerce 3

Is it possible to change price for all products in a specific product category in Woocommerce?
for example if I want to change all products prices in 'shirts' product category to 50, is it possible?
Any help will be appreciated.
To change the product prices with a fixed amount of 50 for an entire product category shirts:
// Simple, grouped and external products
add_filter('woocommerce_product_get_price', 'conditional_custom_price', 99, 2 );
add_filter('woocommerce_product_get_regular_price', 'conditional_custom_price', 99, 2 );
// Variable
add_filter('woocommerce_product_variation_get_regular_price', 'conditional_custom_price', 99, 2 );
add_filter('woocommerce_product_variation_get_price', 'conditional_custom_price', 99, 2 );
function conditional_custom_price( $price, $product ) {
if( has_term( 'shirts', 'product_cat', $product->get_id() ) ){
// Delete product cached price (if needed uncomment it)
// wc_delete_product_transients($product->get_id());
$price = 50; // X2 for testing
}
return $price;
}
// Variations
add_filter('woocommerce_variation_prices_price', 'conditional_custom_variation_price', 99, 3 );
add_filter('woocommerce_variation_prices_regular_price', 'conditional_custom_variation_price', 99, 3 );
function conditional_custom_variation_price( $price, $variation, $product ) {
if( has_term( 'shirts', 'product_cat', $product->get_id() ) ){
// Delete product cached price (if needed uncomment below)
// wc_delete_product_transients($variation->get_id());
$price = 50;
}
return $price;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Based on this answer: Change product prices via a hook in WooCommerce 3
It handle all kind of Woocommerce product types. The conditional Wordpress function has_term() can handle a term Id, a term slug or a term name regarding any product category.

Set product sale price programmatically in WooCommerce 3

I have a Woocommerce store set up with various product categories.
I want to apply a 20% discount to all products, which belong to the product category Cuckoo
For now all I'm trying to achieve is set a sale price in my functions.php
It tried as follows:
/*
* For a specific date, 20% off all products with product category as cuckoo clock.
*/
function cuckoo_minus_twenty($sale_price, $product) {
$sale_price = $product->get_price() * 0.8;
return $sale_price;
};
// add the action
add_filter( 'woocommerce_get_sale_price', 'cuckoo_minus_twenty', 10, 2 );
If I var_dump the result of $sale_price after the calculation I get the correct answer, however the price display on the front-end strikes out the regular price and displays the sale price as the regular price.
Is there a hook/filter I can use to achieve this?
I've also tried setting the sale price by doing:
$product->set_sale_price($sale_price);
to no avail.
The hook woocommerce_get_sale_price is deprecated since WooCommerce 3 and replaced by woocommerce_product_get_sale_price.
Also Product displayed prices are cached. When sale price is active, regular price is also active.
Try this instead:
// Generating dynamically the product "regular price"
add_filter( 'woocommerce_product_get_regular_price', 'custom_dynamic_regular_price', 10, 2 );
add_filter( 'woocommerce_product_variation_get_regular_price', 'custom_dynamic_regular_price', 10, 2 );
function custom_dynamic_regular_price( $regular_price, $product ) {
if( empty($regular_price) || $regular_price == 0 )
return $product->get_price();
else
return $regular_price;
}
// Generating dynamically the product "sale price"
add_filter( 'woocommerce_product_get_sale_price', 'custom_dynamic_sale_price', 10, 2 );
add_filter( 'woocommerce_product_variation_get_sale_price', 'custom_dynamic_sale_price', 10, 2 );
function custom_dynamic_sale_price( $sale_price, $product ) {
$rate = 0.8;
if( empty($sale_price) || $sale_price == 0 )
return $product->get_regular_price() * $rate;
else
return $sale_price;
};
// Displayed formatted regular price + sale price
add_filter( 'woocommerce_get_price_html', 'custom_dynamic_sale_price_html', 20, 2 );
function custom_dynamic_sale_price_html( $price_html, $product ) {
if( $product->is_type('variable') ) return $price_html;
$price_html = wc_format_sale_price( wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ), wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price() ) ) ) . $product->get_price_suffix();
return $price_html;
}
Code goes in function.php file of your active child theme (active theme).
Tested and works on single product, shop, product category and tag archive pages.
The continuation in:
Wrong Woocommerce cart item price after setting programmatically product sale price
I realiced, you more or less need all of the following filters to make the HTML work out of the box.
add_filter( 'woocommerce_product_get_price', 'custom_dynamic_sale_price', 10, 2 );
add_filter( 'woocommerce_product_variation_get_price', 'custom_dynamic_sale_price', 10, 2 );
add_filter( 'woocommerce_product_get_sale_price', 'custom_dynamic_sale_price', 10, 2 );
add_filter( 'woocommerce_product_variation_get_sale_price', 'custom_dynamic_sale_price', 10, 2 );
add_filter( 'woocommerce_variation_prices_price', 'custom_dynamic_sale_price', 10, 2 );
add_filter( 'woocommerce_variation_prices_sale_price', 'custom_dynamic_sale_price', 10, 2 );
And you need to make sure you've changed the woocommerce_get_variation_prices_hash becasue of the stored transients if you want to display it correctly for variable products.
You may find the gist i've created for a client useful
https://gist.github.com/xandl/743fb6af60827eb95ad42b20b478b020
use woocomerce_get_sale_price filter.
add_filter('woocommerce_get_sale_price', 'my_custom_price', 99, 2);
add_filter('woocommerce_get_price', 'my_custom_price', 99, 2);
function my_custom_price( $price, $product )
{
//your logic for calculating the new price here
$price = $product->get_regular_price() * 0.8;
//Return the new price (this is the price that will be used everywhere in the store)
return $price;
}

How to dynamically change the price of a product in Woocommerce - WordPress?

I need to change the price of my products in my store, with a 10% discount, if my customer is from some specific place, so, I wrote this code:
add_filter('woocommerce_price_html', 'my_price_edit');
function my_price_edit() {
$product = new WC_Product( get_the_ID() );
$price = $product->price;
echo $price * 0.9;
}
Ok, it works! But when in the checkout the price are normals without the 10% discount!
Does have some hook for change the price of the products in the checkout area or some different code to change correctly in the both (in the product page and checkout)?
New in Woocommerce too.
Your question looks really similiar to this one.
Adding Custom price with woocomerce product price in Cart & Checkout
I think you need to use the woocommerce_cart_item_subtotal hook to change the price in the cart directly (not exactly as a parameter)
I made a slightly modification to the code (changed price formula). I think that may help you.
add_filter( 'woocommerce_get_discounted_price', 'calculate_discounted_price', 10, 2 );
// Display the line total price
add_filter( 'woocommerce_cart_item_subtotal', 'display_discounted_price', 10, 2 );
function calculate_discounted_price( $price, $values ) {
// You have all your data on $values;
$price = $price*.09;
return $price;
}
// wc_price => format the price with your own currency
function display_discounted_price( $values, $item ) {
return wc_price( $item[ 'line_total' ] );
}

Categories