通过 WooCommerce 3.3 中的挂钩更改产品变化价格 [英] Change product variation prices via a hook in WooCommerce 3.3

查看:69
本文介绍了通过 WooCommerce 3.3 中的挂钩更改产品变化价格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用挂钩来自定义可变产品价格.

I´m using hooks for customizing variable product prices.

但是,这个答案 似乎不适用于 Woocommerce 3.3.5.

However, this answer does not seem to be working for Woocommerce 3.3.5.

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

I use following (from the previous post) on my functions.php file:

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
}

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

On product page the price range for product variations shows correct prices (original price*3) but when I select options, price shown for variation is unfiltered. Am I missing something here?

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

I had a slightly different price calculation for simple products and for product variations. I ended up with 3 functions:

// 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
}

推荐答案

链接代码 仍然适用于 Woocommerce 3.3.x,请参阅 此相关最近接受的工作答案 在 Woocommerce 3.3.x 上进行了测试……你的代码只是未完成......

The linked code still work for Woocommerce 3.3.x, see this related recent accepted working answer tested on Woocommerce 3.3.x … Your code is just uncompleted…

您需要使用:

// 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
}

这篇关于通过 WooCommerce 3.3 中的挂钩更改产品变化价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆