且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

更改 Woocommerce 3 中特定产品标签的所有产品价格

更新时间:2023-11-30 12:37:40

我已经重新访问了您的代码……我已将定价更新合并到一个单独的实用程序函数中……

I have revisited your code… I have merged the pricing updates in a separated utility function…

为了避免您的问题需要定义产品 ID (之前检查产品类型).
然后我们在 WordPress has_term() 条件函数中设置这个正确定义的产品 ID.

To avoid your problems is necessary to define the product ID (checking the product type before).
Then we set this correct defined product ID in WordPress has_term() conditional function.

现在您的价格也适用于购物车和结帐页面......

Now your prices will work on cart and checkout pages too…

您重新访问的代码:

// Utility pricing function
function filtering_product_prices( $price, $product ) {
    // Get the product ID
    $product_id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id();

    // Only for Woocomerce Product Tag "ama"
    if ( ! has_term( 'ama', 'product_tag', $product_id ) ) return $price; // Exit

    if ( $price < 5 ) {
        $price *= 2.5;
    } elseif ( $price >=  5 && $price < 10 ) {
        $price *= 2;
    } elseif ( $price >= 10 && $price < 20 ) {
        $price *= 1.75;
    } elseif ( $price >= 20 && $price < 40 ) {
        $price *= 1.5;
    } elseif ( $price >= 40 && $price < 60 ) {
        $price *= 1.35;
    } elseif ( $price >= 60 && $price < 80 ) {
        $price *= 1.25;
    } elseif ( $price >= 80 && $price < 1000 ) {
        $price *= 1.20;
    }
    return ceil($price + 0.01) - 0.01;
}

// Simple, grouped and external products
add_filter('woocommerce_product_get_price', 'custom_price', 90, 2 );
add_filter('woocommerce_product_get_regular_price', 'custom_price', 90, 2 );

// Product variations (of a variable product)
add_filter('woocommerce_product_variation_get_regular_price', 'custom_price', 99, 2 );
add_filter('woocommerce_product_variation_get_price', 'custom_price', 90, 2 );

function custom_variation_price( $price, $variation, $product ) {
    return filtering_product_prices( $price, $product );
}

// Variable product price range
add_filter('woocommerce_variation_prices_price', 'custom_variation_price', 90, 3 );
add_filter('woocommerce_variation_prices_regular_price', 'custom_variation_price', 90, 3 );

function custom_price( $price, $product ) {
    return filtering_product_prices( $price, $product );
}

代码位于您的活动子主题(或活动主题)的 function.php 文件中.经过测试和工作.

相关答案:有条件的产品价格购物车问题WooCommerce 3