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

WooCommerce程序化修改产品价格后REST API返回原始价格的问题求助

Great question! The issue here is that your current code only handles price display on the frontend (woocommerce_get_price_html) and cart calculations (woocommerce_before_calculate_totals), but the WooCommerce REST API pulls prices directly from the product's core get_price()/get_regular_price() methods—those aren't being modified by your existing filters.

Here's how to fix this by adding targeted filters that apply your price calculation logic specifically to API requests:

Step 1: Add Filters for Product & Variation Prices

These filters will override the price values returned by the product's getter methods when called via the REST API:

// Filter simple product price & regular price for REST API
add_filter('woocommerce_product_get_price', 'didoprice_override_api_price', 200, 2);
add_filter('woocommerce_product_get_regular_price', 'didoprice_override_api_price', 200, 2);

// Filter product variation price & regular price for REST API
add_filter('woocommerce_product_variation_get_price', 'didoprice_override_api_price', 200, 2);
add_filter('woocommerce_product_variation_get_regular_price', 'didoprice_override_api_price', 200, 2);

// Filter variable product min/max price range for REST API
add_filter('woocommerce_variation_prices_price', 'didoprice_override_variation_range_price', 200, 3);
add_filter('woocommerce_variation_prices_regular_price', 'didoprice_override_variation_range_price', 200, 3);

function didoprice_override_api_price($price, $product) {
    // Only apply this logic for REST API requests
    if (!defined('REST_REQUEST') || !REST_REQUEST) {
        return $price;
    }

    // Reuse your existing weight handling
    $gram = $product->get_weight();
    if (empty($gram)) {
        $gram = 100;
    }

    // Apply your core price calculation
    $calculated_price = didoprice_calculation($price, $gram);

    // Add tax (match your frontend display logic)
    $all_tax_rates = [];
    $tax_classes = WC_Tax::get_tax_classes();
    if (!in_array('', $tax_classes)) {
        array_unshift($tax_classes, '');
    }
    foreach ($tax_classes as $tax_class) {
        $taxes = WC_Tax::get_rates_for_tax_class($tax_class);
        $all_tax_rates = array_merge($all_tax_rates, $taxes);
    }
    if (!empty($all_tax_rates)) {
        $tax_rate = $all_tax_rates[0]->tax_rate / 100;
        $calculated_price += $calculated_price * $tax_rate;
    }

    return $calculated_price;
}

function didoprice_override_variation_range_price($price, $variation, $product) {
    // Only apply this logic for REST API requests
    if (!defined('REST_REQUEST') || !REST_REQUEST) {
        return $price;
    }

    // Reuse your existing weight handling
    $gram = $product->get_weight();
    if (empty($gram)) {
        $gram = 100;
    }

    // Apply your core price calculation
    $calculated_price = didoprice_calculation($price, $gram);

    // Add tax (match your frontend display logic)
    $all_tax_rates = [];
    $tax_classes = WC_Tax::get_tax_classes();
    if (!in_array('', $tax_classes)) {
        array_unshift($tax_classes, '');
    }
    foreach ($tax_classes as $tax_class) {
        $taxes = WC_Tax::get_rates_for_tax_class($tax_class);
        $all_tax_rates = array_merge($all_tax_rates, $taxes);
    }
    if (!empty($all_tax_rates)) {
        $tax_rate = $all_tax_rates[0]->tax_rate / 100;
        $calculated_price += $calculated_price * $tax_rate;
    }

    return $calculated_price;
}

Step 2: Fix Price Caching for API Requests

WooCommerce caches variation prices by default, which might cause stale (uncalculated) prices to be returned. Add this filter to ensure API requests get fresh calculated prices:

add_filter('woocommerce_get_variation_prices_hash', 'didoprice_add_api_hash_modifier', 10, 3);
function didoprice_add_api_hash_modifier($hash, $product, $for_display) {
    if (defined('REST_REQUEST') && REST_REQUEST) {
        // Add a unique identifier to bypass cached prices for API requests
        $hash[] = 'rest_api_calculated_price';
    }
    return $hash;
}

How This Works:

  • The REST_REQUEST check ensures we only modify prices for API calls, so your existing frontend and cart logic stays intact.
  • We reuse your existing didoprice_calculation function and tax calculation logic to keep price consistency across all parts of your site.
  • The variation range filters handle variable products, ensuring the API returns calculated min/max price ranges instead of original values.
  • The cache hash modifier prevents WooCommerce from serving cached original prices to API requests.

After adding this code, test your REST API endpoints—you should now see the calculated prices instead of the original backend values.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.27 13:43:11