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

启用woocommerce_before_calculate_totals钩子时常量与函数调用报错

问题解决方案

1. 修复未定义常量错误

  • 首先检查常量拼写:代码里的API_PASSORD是拼写错误,正确应为API_PASSWORD,这是导致未定义常量错误的直接原因。
  • 为避免因常量加载异常引发致命错误,建议在使用前添加存在性检查:
    if (!defined('API_CUSTOMER_NAME') || !defined('API_PASSWORD')) {
        return; // 常量未定义时直接退出函数
    }
    $customerArray = api_authCustomer(API_CUSTOMER_NAME, API_PASSWORD);
    

2. 移除无效的get_meta()调用

WordPress核心没有get_meta()这个函数,你代码中这一行完全多余且错误:

$prodID = get_meta('bis_prodID', true);

直接删除该行即可,后面已经通过get_post_meta()正确获取了自定义字段值。

3. 补全未定义的$customerToken变量

代码中多次使用$customerToken但未赋值,需要从api_authCustomer()的返回结果中提取令牌:

$customerArray = api_authCustomer(API_CUSTOMER_NAME, API_PASSWORD);
// 根据API实际返回结构调整,比如返回数组就用['token'],返回对象就用->token
$customerToken = $customerArray['token']; 

4. 优化钩子执行逻辑(避免重复计算)

woocommerce_before_calculate_totals钩子会被多次触发,添加条件可避免重复执行:

if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) {
    return;
}

修正后的完整代码

add_action( 'woocommerce_before_calculate_totals', 'bis_recalc_price', 10 );
function bis_recalc_price( $cart_object ){
    // 避免重复计算
    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) {
        return;
    }

    // 检查常量是否定义
    if (!defined('API_CUSTOMER_NAME') || !defined('API_PASSWORD')) {
        return;
    }

    $customerArray = api_authCustomer(API_CUSTOMER_NAME, API_PASSWORD);
    // 确保获取到有效令牌
    if (empty($customerArray['token'])) {
        return;
    }
    $customerToken = $customerArray['token'];

    foreach ($cart_object->get_cart() as $hash => $value) {
        if(isset($_COOKIE['wt_customerID'])) {
            $_product = $value['data'];
            $is_on_sale = $_product->is_on_sale();
            $bisProductArray = api_getProductByIDonly($_product->get_sku(), $customerToken);
            
            // 校验API返回数据有效性
            if (empty($bisProductArray[0][0]['SellPerText'])) {
                continue;
            }
            $SellPerText = $bisProductArray[0][0]['SellPerText'];
            $custID = $_COOKIE['wt_customerID'];
            $prodID = get_post_meta($_product->get_id(), 'bis_prodID', true);
            
            if ($prodID > 0) {
                $priceStockArray = api_customerpriceandstocklevel($custID, $prodID, $SellPerText, $customerToken);
                // 校验价格数据有效性
                if (empty($priceStockArray[0]['CustomerPrice'][0]['UnitSellPrice'])) {
                    continue;
                }
                $customer_price = $priceStockArray[0]['CustomerPrice'][0]['UnitSellPrice'];
                
                if ( $is_on_sale && ($_product->get_sale_price() > 0)) {
                    if ($customer_price > $_product->get_sale_price()) {
                        $customer_price = $_product->get_sale_price();
                    }
                }
                $value['data']->set_price($customer_price);
            }
        }
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 15:03:23