WooCommerce变量商品出现Unsupported operand types错误求助
错误分析:Unsupported operand types(减号运算符引发的致命错误)
问题描述
我遇到了如下错误:
Fatal error: Uncaught Error: Unsupported operand types when using the minus - sign
我推测这和字符串、数组与数字字段搭配减号运算符使用有关,而且只在变量商品上出现,怀疑和子商品相关,可能需要用array_sum()计算多维数组总和。
最初编写的库存阈值控制代码
add_filter('woocommerce_is_purchasable', 'non_purchasable_oos_threshold', 10, 2 ); function non_purchasable_oos_threshold( $is_purchasable, $product ) { $product_id = $product->get_id(); $stock_threshold = get_post_meta( $product_id, '_out_of_stock_threshold', true ); $threshold = $product->get_meta( '_out_of_stock_threshold' ); if ( $product->managing_stock() && ! empty( $threshold ) ) { $stock = $product->get_stock_quantity(); $new_stock_quantity = $product->get_stock_quantity() - $threshold; // var_dump($new_stock_quantity); if ( $new_stock_quantity == 0 ) { return false; } return $is_purchasable; } }
我怀疑错误不是单纯减号导致的,而是代码本身有问题,想请教错误原因。
针对商品变体的更新代码
add_filter('woocommerce_is_purchasable', 'variations_purchasable_oos_threshold', 10, 2 ); function variations_purchasable_oos_threshold( $is_purchasable, $product ) { if ( $product->get_type() == 'variable' ) { foreach ( $product->get_available_variations() as $key ) { $variation = wc_get_product( $key['variation_id'] ); $stock = $variation->get_availability(); $product_id = $product->get_id(); $stock_threshold = get_post_meta( $product_id, '_out_of_stock_threshold', true ); $threshold = $product->get_meta( '_out_of_stock_threshold' ); if ( $product->managing_stock() && ! empty( $threshold ) ) { $stock = $product->get_stock_quantity(); $new_stock_quantity = $stock - $threshold; // var_dump($new_stock_quantity); if ( $new_stock_quantity == 0 ) { return false; } return $is_purchasable; } } } }
错误原因分析
阈值数据类型不匹配
从get_post_meta或get_meta获取的$threshold可能是字符串类型,甚至可能因为元数据存储异常变成数组。PHP无法直接对字符串/数组和数字执行减法运算,触发Unsupported operand types错误。变量商品库存逻辑错误
- 变量商品本身通常不管理库存(除非开启父商品库存管理),真正管理库存的是子变体。更新代码循环变体时,仍调用父商品的
$product->get_stock_quantity(),而非子变体的$variation->get_stock_quantity()。 - 若父商品未开启库存管理,
$product->get_stock_quantity()会返回null,用null和$threshold做减法同样会触发类型错误。
- 变量商品本身通常不管理库存(除非开启父商品库存管理),真正管理库存的是子变体。更新代码循环变体时,仍调用父商品的
代码冗余与逻辑漏洞
- 重复调用
get_post_meta和get_meta获取同一阈值,完全冗余;未指定get_meta的第二个参数为true,可能返回数组而非单个值。 - 最初的函数未处理非变量商品的默认返回值,可能导致逻辑异常。
- 重复调用
修复建议
强制转换数据类型
获取阈值后强制转为整数,确保运算时类型匹配:$threshold = (int) $product->get_meta('_out_of_stock_threshold', true);修正变量商品库存处理逻辑
针对变量商品,遍历子变体时使用子变体的库存数据,优先使用变体自身阈值,无阈值则继承父商品设置:add_filter('woocommerce_is_purchasable', 'variations_purchasable_oos_threshold', 10, 2 ); function variations_purchasable_oos_threshold( $is_purchasable, $product ) { // 处理变量商品 if ( $product->get_type() === 'variable' ) { $parent_threshold = (int) $product->get_meta('_out_of_stock_threshold', true); foreach ( $product->get_available_variations() as $variation_data ) { $variation = wc_get_product( $variation_data['variation_id'] ); $threshold = (int) $variation->get_meta('_out_of_stock_threshold', true) ?: $parent_threshold; if ( $variation->managing_stock() && $threshold > 0 ) { $stock = (int) $variation->get_stock_quantity(); if ( !is_numeric($stock) ) continue; $new_stock_quantity = $stock - $threshold; if ( $new_stock_quantity <= 0 ) { return false; } } } return $is_purchasable; } // 处理普通商品 if ( $product->managing_stock() ) { $threshold = (int) $product->get_meta('_out_of_stock_threshold', true); if ( $threshold > 0 ) { $stock = (int) $product->get_stock_quantity(); if ( !is_numeric($stock) ) return $is_purchasable; $new_stock_quantity = $stock - $threshold; if ( $new_stock_quantity <= 0 ) { return false; } } } return $is_purchasable; }增加有效性判断
在运算前检查库存和阈值是否为有效数字,避免无效值参与运算:if ( !is_numeric($stock) || !is_numeric($threshold) ) { return $is_purchasable; }
内容的提问来源于stack exchange,提问作者user2589071
相关产品推荐
相关产品推荐

