ВОПРОС
Мне нужно умножить цены на все товары с сайта на определенное число. Я попытался сделать все самостоятельно и для этого я воспользовался этим скриптом:
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;
}
Но есть одна проблема. Этот код не работает для вариативных продуктов. Поэтому я решил попробовать немного другие хуки, но они вообще не дают никакого результата:
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);
Я перепробовал массу разных вариантов. Спустя долгие часы работы и поисков подходящих способов мне удалось найти один метод, который хотя бы немного похож на то, что мне нужно. Он работает, но не совсем так, как я хочу. Этот скрипт изменяет только общую цену:
add_filter(‘woocommerce_variation_prices_price’, array( $this, ‘my_custom_price’), 99);
Может быть, я просто допустил какую-то ошибку или сделал что-то не так? Надеюсь, вы поможете мне с решением.
ОТВЕТ
У тебя возникли трудности из-за того, что хуки, которые ты пытаешься использовать устарели для третьей версии расширения. Поэтому ты должен попробовать применить более новые варианты:
## 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;
}