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