M
M
MisTFoR2021-12-11 20:35:50
PHP
MisTFoR, 2021-12-11 20:35:50

How to add custom value to variation price?

Hello!
I use WooCommerce for products. I have a product (let's call it "Pizza"), it has 3 variations with its own price. I'm wondering how to add 100 rubles to this price in the product card using PHP code?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
WP Panda, 2021-12-12
@MisTFoR

//функция для изменения цен с помощью числа
function wpp_price_plus() {
    return 100;
}

add_filter('woocommerce_product_variation_get_regular_price', 'wpp_custom_price', 99, 2 );
add_filter('woocommerce_product_variation_get_price', 'wpp_custom_price', 99, 2 );
function wpp_custom_price( $price, $product ) {
    return (float) $price + wpp_price_plus();
}

add_filter('woocommerce_variation_prices_price', 'wpp_custom_variable_price', 99, 3 );
add_filter('woocommerce_variation_prices_regular_price', 'wpp_custom_variable_price', 99, 3 );
function wpp_custom_variable_price( $price, $variation, $product ) {
     // Удаление кэшированной цены продукта
    wc_delete_product_transients($variation->get_id());
    return (float) $price + wpp_price_plus();
}

// Обработка кэширования цен
add_filter( 'woocommerce_get_variation_prices_hash', 'add_price_plus_to_variation_prices_hash', 99, 3 );
function add_price_plus_to_variation_prices_hash( $price_hash, $product, $for_display ) {
    $price_hash[] = wpp_price_plus();
    return $price_hash;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question