我想知道是否有这方面知识的人可以帮忙。
如果我尝试更新订单,即:在后端查看订单时,以订单 Actons > 选择和操作 > 重新发送订单通知为例,它会给我一个严重错误,如下所示。
注意:这似乎只发生在某些看起来在订单上使用优惠券的订单上?
请查看错误:
寻求有关此问题的帮助时,可能会要求您提供以下一些信息: WordPress 版本 6.3 活动主题:Woodmart Child(版本 1.0.0) 当前插件:(版本) PHP 版本 8.1.22
Error Details
=============
An error of type E_ERROR was caused in line 21 of the file /usr/www/users/africgebks/wp-content/themes/woodmart-child/functions.php. Error message: Uncaught TypeError: count(): Argument #1 ($value) must be of type Countable|array, null given in /usr/www/users/africgebks/wp-content/themes/woodmart-child/functions.php:21
Stack trace:
#0 /usr/www/users/africgebks/wp-includes/class-wp-hook.php(310): display_applied_coupons(Object(Automattic\WooCommerce\Admin\Overrides\Order), true, false, Object(WC_Email_New_Order))
#1 /usr/www/users/africgebks/wp-includes/class-wp-hook.php(334): WP_Hook->apply_filters('', Array)
#2 /usr/www/users/africgebks/wp-includes/plugin.php(517): WP_Hook->do_action(Array)
#3 /usr/www/users/africgebks/wp-content/plugins/woocommerce/templates/emails/admin-new-order.php(35): do_action('woocommerce_ema...', Object(Automattic\WooCommerce\Admin\Overrides\Order), true, false, Object(WC_Email_New_Order))
#4 /usr/www/users/africgebks/wp-content/plugins/woocommerce/includes/wc-core-functions.php(345): include('/usr/www/users/...')
#5 /usr/www/users/africgebks/wp-content/plugins/woocommerce/includes/wc-core-functions.php(364): wc_get_template('emails/admin-ne...', Array, '', '')
#6 /usr/www/users/africgebks/wp-content/plugins/woocommerce/includes/emails/class-wc-email-new-order.php(134): wc_get_template_html('emails/admin-ne...', Array)
#7 /usr/www/users/africgebks/wp-content/plugins/woocommerce/includes/emails/class-wc-email.php(581): WC_Email_New_Order->get_content_html()
#8 /usr/www/users/africgebks/wp-content/plugins/woocommerce/includes/emails/class-wc-email-new-order.php(111): WC_Email->get_content()
#9 /usr/www/users/africgebks/wp-content/plugins/woocommerce/includes/admin/meta-boxes/class-wc-meta-box-order-actions.php(156): WC_Email_New_Order->trigger(14992, Object(Automattic\WooCommerce\Admin\Overrides\Order), true)
#10 /usr/www/users/africgebks/wp-includes/class-wp-hook.php(310): WC_Meta_Box_Order_Actions::save(14992, Object(WP_Post))
#11 /usr/www/users/africgebks/wp-includes/class-wp-hook.php(334): WP_Hook->apply_filters(NULL, Array)
#12 /usr/www/users/africgebks/wp-includes/plugin.php(517): WP_Hook->do_action(Array)
#13 /usr/www/users/africgebks/wp-content/plugins/woocommerce/includes/admin/class-wc-admin-meta-boxes.php(258): do_action('woocommerce_pro...', 14992, Object(WP_Post))
#14 /usr/www/users/africgebks/wp-includes/class-wp-hook.php(312): WC_Admin_Meta_Boxes->save_meta_boxes(14992, Object(WP_Post))
#15 /usr/www/users/africgebks/wp-includes/class-wp-hook.php(334): WP_Hook->apply_filters('', Array)
#16 /usr/www/users/africgebks/wp-includes/plugin.php(517): WP_Hook->do_action(Array)
#17 /usr/www/users/africgebks/wp-includes/post.php(4751): do_action('save_post', 14992, Object(WP_Post), true)
#18 /usr/www/users/africgebks/wp-includes/post.php(4853): wp_insert_post(Array, false, true)
#19 /usr/www/users/africgebks/wp-admin/includes/post.php(445): wp_update_post(Array)
#20 /usr/www/users/africgebks/wp-admin/post.php(227): edit_post()
#21 {main}
thrown
**另请参阅 woocommerce coupons 添加到functions.php 文件中的代码,我认为这就是错误的来源?
// The email function hooked that display the text
add_action( 'woocommerce_email_order_details', 'display_applied_coupons', 10, 4 );
function display_applied_coupons( $order, $sent_to_admin, $plain_text, $email ) {
// Only for admins and when there at least 1 coupon in the order
if ( ! $sent_to_admin && count($order->get_items('coupon') ) == 0 ) return;
foreach( $order->get_items('coupon') as $coupon ){
$coupon_codes[] = $coupon->get_code();
}
// For one coupon
if( count($coupon_codes) == 1 ){
$coupon_code = reset($coupon_codes);
echo '<p>'.__( 'Coupon Used: ').$coupon_code.'<p>';
}
// For multiple coupons
else {
$coupon_codes = implode( ', ', $coupon_codes);
echo '<p>'.__( 'Coupons Used: ').$coupon_codes.'<p>';
}
}
如果我尝试更新订单,即:在后端查看订单时,以订单 Actons > 选择和操作 > 重新发送订单通知为例,它会给我一个严重错误,如下所示。
您的代码中有一些错误,请尝试使用这个简化的代码版本:
add_action( 'woocommerce_email_order_details', 'display_applied_coupons', 10, 4 );
function display_applied_coupons( $order, $sent_to_admin, $plain_text, $email ) {
if ( $sent_to_admin ) {
$coupon_codes = (array) $order->get_coupon_codes();
if( count($coupon_codes) === 1 ){
echo '<p>'.__( 'Coupon Used: ').reset($coupon_codes).'<p>';
} elseif( count($coupon_codes) > 1 ) {
echo '<p>'.__( 'Coupons Used: ').implode( ', ', $coupon_codes).'<p>';
}
}
}
代码位于子主题的functions.php 文件中(或插件中)。经过测试并有效,它将解决您的问题。问题出在
count($order->get_items('coupon') )
。