You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WooCommerce 3特定标签商品自动加价函数has_term条件异常求助

Fixing WooCommerce Price Reversion with has_term() Check

Got it, let's dig into this WooCommerce pricing glitch you're hitting. I’ve seen this exact issue before—when your has_term() check works perfectly in the backend but reverts prices back to original once items hit the cart or checkout. Here’s why it’s happening and how to fix it:

Why This Happens

The root cause almost always boils down to context mismatch when using has_term() in cart-related hooks:

  • You might be passing a product ID instead of a proper WC_Product object, leading to incorrect term checks (especially for variable products).
  • You’re not explicitly specifying the product_tag taxonomy, so WordPress might be checking other taxonomies accidentally.
  • Your hook priority is too low, so other plugins/themes are overwriting your price changes after you set them.

Step-by-Step Fixes

1. Correct Your has_term() Syntax

First, make sure you’re using has_term() with all three required parameters to target product tags specifically. Skipping the taxonomy can lead to false negatives in cart context:

// ❌ Wrong (no explicit taxonomy)
if ( has_term( 'your-target-tag', $product_id ) ) { ... }

// ✅ Correct (targets product tags explicitly)
if ( has_term( 'your-target-tag', 'product_tag', $product ) ) { ... }

2. Use WC_Product Objects in Cart Hooks

When modifying cart prices, always use the WC_Product object from the cart item instead of relying on post IDs. This ensures you’re checking the exact product (or its parent, for variations) that’s in the cart:

add_action( 'woocommerce_before_calculate_totals', 'apply_custom_price_increase', 20 );
function apply_custom_price_increase( $cart ) {
    // Exit if admin area (unless it's an AJAX request)
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;

    // Loop through cart items
    foreach ( $cart->get_cart() as $cart_item ) {
        $product = $cart_item['data'];
        
        // Check if product has the target tag
        if ( has_term( 'your-target-tag', 'product_tag', $product ) ) {
            // Calculate increased price (adjust multiplier as needed)
            $original_price = $product->get_regular_price();
            $increased_price = $original_price * 1.15; // 15% increase
            
            // Update the product price in the cart
            $product->set_price( $increased_price );
            // Disable caching to ensure the new price sticks
            $product->set_cached_price( false );
        }
    }
}

3. Handle Variable Products Properly

If you sell variable products, variations might not inherit parent tags by default. Add a check to verify the parent product’s tags if the cart item is a variation:

foreach ( $cart->get_cart() as $cart_item ) {
    $product = $cart_item['data'];
    $has_target_tag = false;

    // Check if it's a variation—use parent product for tag check
    if ( $product->is_type( 'variation' ) ) {
        $parent_product = wc_get_product( $product->get_parent_id() );
        $has_target_tag = has_term( 'your-target-tag', 'product_tag', $parent_product );
    } else {
        $has_target_tag = has_term( 'your-target-tag', 'product_tag', $product );
    }

    if ( $has_target_tag ) {
        // Apply price increase logic here
        $original_price = $product->get_regular_price();
        $product->set_price( $original_price * 1.15 );
        $product->set_cached_price( false );
    }
}

4. Boost Hook Priority

If your price changes are still being overwritten, increase the hook priority to ensure your code runs after other plugins/themes:

// Use priority 20 instead of the default 10
add_action( 'woocommerce_before_calculate_totals', 'apply_custom_price_increase', 20 );

Final Notes

The key takeaway is that cart context requires more precision than backend product editing. By using explicit WC_Product objects, targeting the correct taxonomy, and handling variations, your price increase should persist from the backend all the way through checkout.

内容的提问来源于stack exchange,提问作者therealdeal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 04:20:17