通过 WooCommerce 3.3 中的挂钩更改产品变化价格

Dai*_*isy 2 php wordpress product woocommerce price

我正在使用挂钩来定制可变产品价格。

但是,此答案似乎不适用于 Woocommerce 3.3.5。

我在我的functions.php文件中使用以下(来自上一篇文章):

add_filter('woocommerce_variation_prices_price', 'custom_variation_price', 99, 3 );
add_filter('woocommerce_variation_prices_regular_price', 'custom_variation_price', 99, 3 );
function custom_variation_price( $price, $variation, $product ) {
    // Delete product cached price  (if needed)
wc_delete_product_transients($variation->get_id());

return $price * 3; // X2 for testing
}
Run Code Online (Sandbox Code Playgroud)

在产品页面上,产品变体的价格范围显示正确的价格(原价*3),但是当我选择选项时,变体显示的价格未过滤。我在这里错过了什么吗?


编辑:我对简单产品和产品变体的价格计算略有不同。我最终得到了 3 个函数:

// Simple
add_filter('woocommerce_product_get_price', 'custom_price', 99, 2 );
add_filter('woocommerce_product_get_regular_price', 'custom_price', 99, 2 );

// Variable
add_filter('woocommerce_product_variation_get_regular_price', 'custom_price_2', 99, 2 );
add_filter('woocommerce_product_variation_get_price', 'custom_price_2' , 99, 2 );

// Variations (of a variable product)
add_filter('woocommerce_variation_prices_price', 'custom_variation_price', 99, 3 );
add_filter('woocommerce_variation_prices_regular_price', 'custom_variation_price', 99, 3 );

function custom_price( $price, $product ) {
    // Delete product cached price  (if needed)
    wc_delete_product_transients($product->get_id());

    return $price * 3; // X3 for testing
}

function custom_price_2( $price, $product ) {
    // Delete product cached price  (if needed)
    wc_delete_product_transients($product->get_id());

    return $price * 2; // X2 for testing
}

function custom_variation_price( $price, $variation, $product ) {
    // Delete product cached price  (if needed)
    wc_delete_product_transients($variation->get_id());


    return $price * 2; // X2 for testing
}
Run Code Online (Sandbox Code Playgroud)

Loi*_*tec 5

链接的代码仍然工作Woocommerce 3.3.x,看到这个相关的最近接纳了答案上Woocommerce 3.3.x测试...您的代码只是半拉子...

您需要使用:

// Variable
add_filter('woocommerce_product_variation_get_regular_price', 'custom_price', 99, 2 );
add_filter('woocommerce_product_variation_get_price', 'custom_price' , 99, 2 );

// Variations (of a variable product)
add_filter('woocommerce_variation_prices_price', 'custom_variation_price', 99, 3 );
add_filter('woocommerce_variation_prices_regular_price', 'custom_variation_price', 99, 3 );

function custom_price( $price, $product ) {
    // Delete product cached price  (if needed)
    wc_delete_product_transients($product->get_id());

    return $price * 3; // X3 for testing
}

function custom_variation_price( $price, $variation, $product ) {
    // Delete product cached price  (if needed)
    wc_delete_product_transients($variation->get_id());

    return $price * 3; // X3 for testing
}
Run Code Online (Sandbox Code Playgroud)