Php WooCommerce错误调试文件:为foreach()提供的参数无效
1°我使用WooCommerce V.4.7.0 2°在版本V.4.6.1中,我没有看到这种情况发生 3°每次下单时,我都会在debug.log文件中收到此错误消息 4°为什么会发生这种情况,我该如何解决 debug.log文件中的错误消息:Php WooCommerce错误调试文件:为foreach()提供的参数无效,php,wordpress,woocommerce,wordpress-theming,hook-woocommerce,Php,Wordpress,Woocommerce,Wordpress Theming,Hook Woocommerce,1°我使用WooCommerce V.4.7.0 2°在版本V.4.6.1中,我没有看到这种情况发生 3°每次下单时,我都会在debug.log文件中收到此错误消息 4°为什么会发生这种情况,我该如何解决 debug.log文件中的错误消息: [20-Nov-2020 23:58:31 UTC] PHP Warning: Invalid argument supplied for foreach() in C:\xampppserver2\htdocs\mrdigital\wp-conten
[20-Nov-2020 23:58:31 UTC] PHP Warning: Invalid argument supplied for foreach()
in C:\xampppserver2\htdocs\mrdigital\wp-content\themes\astra-child\woocommerce\order\order-details.php
on line 82
[21-Nov-2020 00:02:08 UTC] PHP Warning: Invalid argument supplied for foreach()
in C:\xampppserver2\htdocs\mrdigital\wp-content\themes\astra-child\woocommerce\order\order-details.php
on line 82
这是第82行
foreach ( $order->get_order_item_totals() as $key => $total ) {
这是我的模板文件-woocommerce/order/order-details.php
<?php
/**
* Order details
*
* This template can be overridden by copying it to yourtheme/woocommerce/order/order-details.php.
*
* HOWEVER, on occasion WooCommerce will need to update template files and you
* (the theme developer) will need to copy the new files to your theme to
* maintain compatibility. We try to do this as little as possible, but it does
* happen. When this occurs the version of the template file will be bumped and
* the readme will list any important changes.
*
* @see https://docs.woocommerce.com/document/template-structure/
* @package WooCommerce\Templates
* @version 4.6.0
*/
defined( 'ABSPATH' ) || exit;
$order = wc_get_order( $order_id ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
if ( ! $order ) {
return;
}
$order_items = $order->get_items( apply_filters( 'woocommerce_purchase_order_item_types', 'line_item' ) );
$show_purchase_note = $order->has_status( apply_filters( 'woocommerce_purchase_note_order_statuses', array( 'completed', 'processing' ) ) );
$show_customer_details = is_user_logged_in() && $order->get_user_id() === get_current_user_id();
$downloads = $order->get_downloadable_items();
$show_downloads = $order->has_downloadable_item() && $order->is_download_permitted();
if ( $show_downloads ) {
wc_get_template(
'order/order-downloads.php',
array(
'downloads' => $downloads,
'show_title' => true,
)
);
}
?>
<section class="woocommerce-order-details">
<?php do_action( 'woocommerce_order_details_before_order_table', $order ); ?>
<h2 class="woocommerce-order-details__title"><?php esc_html_e( 'Order details', 'woocommerce' ); ?></h2>
<table class="woocommerce-table woocommerce-table--order-details shop_table order_details">
<thead>
<tr>
<th class="woocommerce-table__product-name product-name"><?php esc_html_e( 'Product', 'woocommerce' ); ?></th>
<th class="woocommerce-table__product-table product-total"><?php esc_html_e( 'Total', 'woocommerce' ); ?></th>
</tr>
</thead>
<tbody>
<?php
do_action( 'woocommerce_order_details_before_order_table_items', $order );
foreach ( $order_items as $item_id => $item ) {
$product = $item->get_product();
wc_get_template(
'order/order-details-item.php',
array(
'order' => $order,
'item_id' => $item_id,
'item' => $item,
'show_purchase_note' => $show_purchase_note,
'purchase_note' => $product ? $product->get_purchase_note() : '',
'product' => $product,
)
);
}
do_action( 'woocommerce_order_details_after_order_table_items', $order );
?>
</tbody>
<tfoot>
<?php
foreach ( $order->get_order_item_totals() as $key => $total ) {
?>
<tr>
<th scope="row"><?php echo esc_html( $total['label'] ); ?></th>
<td><?php echo ( 'payment_method' === $key ) ? esc_html( $total['value'] ) : wp_kses_post( $total['value'] ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></td>
</tr>
<?php
}
?>
<?php if ( $order->get_customer_note() ) : ?>
<tr>
<th><?php esc_html_e( 'Note:', 'woocommerce' ); ?></th>
<td><?php echo wp_kses_post( nl2br( wptexturize( $order->get_customer_note() ) ) ); ?></td>
</tr>
<?php endif; ?>
</tfoot>
</table>
<?php do_action( 'woocommerce_order_details_after_order_table', $order ); ?>
</section>
<?php
/**
* Action hook fired after the order details.
*
* @since 4.4.0
* @param WC_Order $order Order data.
*/
do_action( 'woocommerce_after_order_details', $order );
if ( $show_customer_details ) {
wc_get_template( 'order/order-details-customer.php', array( 'order' => $order ) );
}
似乎在某个时候,当还没有总计时,文件可能会被加载。可能需要有条件地检查它们以避免错误消息。您可以将总计保存到varibale,然后检查以确保它们不是空的,并且是如下所示的数组:
<?php
// save totals to variable
$totals = $order->get_order_item_totals();
// only loop and display if an array and not empty
if (is_array($totals) && !empty($totals)) {
foreach ( $totals as $key => $total ) {
?>
<tr>
<th scope="row"><?php echo esc_html( $total['label'] ); ?></th>
<td><?php echo ( 'payment_method' === $key ) ? esc_html( $total['value'] ) : wp_kses_post( $total['value'] ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></td>
</tr>
<?php
}
}
?>
Hello@mikerojas,非常感谢您的大力帮助。它就像一个符咒。最好的是,debug.log文件
完全干净;)