Обновлено: Вы не используете правильный крючки и правильный путь.Следующее заменит весь ваш код.Изменения цен будут производиться для простых продуктов, переменных продуктов и вариантов продукта.
1-ая функция - это ставка, применяемая к ценам вашего продукта (поэтому вы определите в ней свой расчет ставки) .
2-я функция изменит цену всех продуктов, отображаемую на всех страницах продукта, вместо использования вашей пользовательской функции, подключенной к фильтру woocommerce_product_get_price
.
3-я функция изменит цену продуктав элементах корзины, на страницах корзины и оформления заказа, а также в элементах заказов.
// The price rate to be applied
function get_product_price_rate() {
// HERE define the price rate to be applied
return 1.25; // +25%
}
// Change the product displayed price on product pages
add_filter( 'woocommerce_get_price_html', 'custom_price_html', 10, 2 );
function custom_price_html( $price, $product ){
$rate = get_product_price_rate();
// Simple products and product variations
if( in_array( $product->get_type() , array( 'simple' , 'variation' ) ) ) {
$regular_price = wc_get_price_to_display( $product, array( 'price' => ( $product->get_regular_price() * $rate ) ) );
$active_price = wc_get_price_to_display( $product, array( 'price' => ( $product->get_price() * $rate ) ) );
if ( '' === $product->get_price() ) {
$price = apply_filters( 'woocommerce_empty_price_html', '', $product );
} elseif ( $product->is_on_sale() ) {
$price = wc_format_sale_price( $regular_price, $active_price ) . $product->get_price_suffix();
} else {
$price = wc_price( $active_price ) . $product->get_price_suffix();
}
}
// Variable products
elseif ( 'variable' === $product->get_type() ) {
$prices = $product->get_variation_prices( true );
if ( empty( $prices['price'] ) ) {
$price = apply_filters( 'woocommerce_variable_empty_price_html', '', $this );
} else {
$min_price = current( $prices['price'] ) * $rate;
$max_price = end( $prices['price'] ) * $rate;
$min_reg_price = current( $prices['regular_price'] ) * $rate;
$max_reg_price = end( $prices['regular_price'] ) * $rate;
if ( $min_price !== $max_price ) {
$price = wc_format_price_range( $min_price, $max_price );
} elseif ( $this->is_on_sale() && $min_reg_price === $max_reg_price ) {
$price = wc_format_sale_price( wc_price( $max_reg_price ), wc_price( $min_price ) );
} else {
$price = wc_price( $min_price );
}
$price .= $product->get_price_suffix();
}
}
return $price;
}
// Change cart items prices (and order items prices)
add_action('woocommerce_before_calculate_totals', 'increase_cart_item_prices', 100, 1 );
function increase_cart_item_prices( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// Loop Through cart items
foreach ( $cart->get_cart() as $cart_item ) {
$rate = get_product_price_rate();
$cart_item['data']->set_price( $cart_item['data']->get_price() * $rate );
}
}
Код находится в файле function.php вашей активной дочерней темы (или активной темы).Проверено и работает.