WordPress结账页面报错求助:自定义插件与WooCommerce错误修复
问题描述
错误信息
警告错误
Warning: Illegal string offset 'snumber' in /home/afraa/public_html/wp-content/plugins/avin-customize2/avin-customize.php on line 176
致命错误
Fatal error: Uncaught Error: Call to undefined method stdClass::get_label() in /home/afraa/public_html/wp-content/plugins/woocommerce/includes/wc-cart-functions.php:354 Stack trace: #0 /home/afraa/public_html/wp-content/themes/zanbil/woocommerce/cart/cart-shipping.php(39): wc_cart_totals_shipping_method_label(Object(stdClass)) #1 /home/afraa/public_html/wp-content/plugins/woocommerce/includes/wc-core-functions.php(345): include('/home/afraa/pub...') #2 /home/afraa/public_html/wp-content/plugins/woocommerce/includes/wc-cart-functions.php(236): wc_get_template('cart/cart-shipp...', Array) #3 /home/afraa/public_html/wp-content/themes/zanbil/woocommerce/cart/cart-totals.php(46): wc_cart_totals_shipping_html() #4 /home/afraa/public_html/wp-content/plugins/woocommerce/includes/wc-core-functions.php(345): include('/home/afraa/pub...') #5 /home/afraa/public_html/wp-content/plugins/woocommerce/includes/wc-template-functions.php(2136): wc_get_template('cart/cart-total...') #6 /home/afraa/public_html/wp-includes/class-wp-hook.php(30 in /home/afraa/public_html/wp-content/plugins/woocommerce/includes/wc-cart-functions.php on line 354
相关代码(第176行)
$fields['billing']['billing_s_number'] = array( 'label' => 'شماره ثبت', 'required' => false, 'clear' => false, 'class' => array('du-none'), 'type' => 'text', // 第176行 'default' => get_user_meta(get_current_user_id(), 'company_data', true)['snumber'] ?: "" );
修复方案
1. 解决第176行的警告错误
问题根源是get_user_meta返回的值不一定是数组——如果用户未设置过company_data元数据,或者该元数据存储的是字符串/非数组类型,直接用['snumber']取偏移量就会触发警告。
修改第176行代码,先判断返回值类型再安全取值:
'default' => function() { $company_data = get_user_meta(get_current_user_id(), 'company_data', true); // 确保是数组且存在snumber键才取值,否则返回空字符串 return is_array($company_data) && isset($company_data['snumber']) ? $company_data['snumber'] : ""; }
2. 解决致命错误(stdClass::get_label()未定义)
这个错误是因为WooCommerce的wc_cart_totals_shipping_method_label函数需要传入WC_Shipping_Method类实例,但实际传入的是普通stdClass对象,大概率是自定义插件avin-customize2修改了配送方法的数据结构导致的。
排查方向:
- 查找插件中是否有过滤
woocommerce_available_shipping_methods的代码,确保返回的每个配送方法都是WC_Shipping_Method实例,没有被强制转为stdClass。 - 检查插件是否有代码直接修改了购物车中的配送方法数据,比如用
json_decode把对象转成了普通stdClass。 - 如果插件有自定义配送方法,确保类继承自
WC_Shipping_Method并正确注册。
如果暂时找不到问题点,可以先禁用avin-customize2插件,若致命错误消失,就锁定是该插件的问题,再逐步排查涉及配送方法的代码块。
内容的提问来源于stack exchange,提问作者Chris
相关产品推荐
相关产品推荐

