WooCommerce error: Coupon properties should not be accessed directly
WooCommerce error: Coupon properties should not be accessed directly
1° 出于必要,我在模板 order-details.php 文件中创建了这个条件,我知道,不推荐这样做。
2° 现在我必须将此脚本放入 order-details.php(结帐页面)中的 运行 过滤器中.
3° 问题:当我为产品应用优惠券代码并下订单时,我总是收到以下错误debug.log 文件中的消息
[20-Nov-2020 14:19:21 UTC] PHP Notice: code was called <strong> incorrectly </strong>.
Coupon properties should not be accessed directly.
Backtrace: require('wp-blog-header.php'),
require_once('wp-includes/template-loader.php'),
include('/themes/astra/page.php'),
astra_content_page_loop,
do_action('astra_content_page_loop'),
WP_Hook->do_action,
WP_Hook->apply_filters, Astra_Loop->loop_markup_page, Astra_Loop->loop_markup,
do_action('astra_page_template_parts_content'), WP_Hook->do_action, WP_Hook->apply_filters,
Astra_Loop->template_parts_page, get_template_part, locate_template, load_template,
require('/themes/astra/template-parts/content-page.php'), the_content, apply_filters('the_content'),
WP_Hook->apply_filters, do_shortcode, preg_replace_callback, do_shortcode_tag,
WC_Shortcodes::checkout, WC_Shortcodes::shortcode_wrapper, WC_Shortcode_Checkout::output,
WC_Shortcode_Checkout::order_received, wc_get_template, include('/themes/astra-child/woocommerce/
checkout/thankyou.php'), do_action('woocommerce_thankyou'), WP_Hook->do_action, WP_Hook->app in
C:\xampppserver2\htdocs\mrdigital\wp-includes\functions.php on line 5229
这是我的订单-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 = true;
// Para exibir o template ENDEREÇO DE FATURAMENTO na página
$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,
)
);
}
?>
<ul class="list-unstyled">
<li class="text-right">
<a href="<?php echo site_url(); ?>" class="button">Página Inicial</a>
</li>
</ul>
<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
}
// **** Inserted Lines - Coupon Start ****
if( $order->get_coupon_codes() ) {
foreach( $order->get_coupon_codes() as $code) {
// Pick up the object of each coupon
$coupon = new WC_Coupon($code);
?>
<tr>
<th>Cupom</th>
<!-- Print the Coupon Code -->
<td><?php echo $coupon->code; ?></td>
</tr>
<tr>
<th>Coupon Expiration Date</th>
<!-- Print Coupon Expiration Date -->
<td><?php print_r($coupon->get_date_expires()->date('d-m-Y')); ?></td>
</tr>
<?php }
}
// **** Inserted Lines - Coupon End ****
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 ) );
}
只是要看的优惠券脚本::
// **** Inserted Lines - Coupon Start ****
if( $order->get_coupon_codes() ) {
foreach( $order->get_coupon_codes() as $code) {
// Pick up the object of each coupon
$coupon = new WC_Coupon($code);
?>
<tr>
<th>Coupon</th>
<!-- Print the Coupon Code -->
<td><?php echo $coupon->code; ?></td>
</tr>
<tr>
<th>Coupon Expiration Date</th>
<!-- Print Coupon Expiration Date -->
<td><?php print_r($coupon->get_date_expires()->date('d-m-Y')); ?></td>
</tr>
<?php }
}
// **** Inserted Lines - Coupon End ****
4° 我应该在 functions.php 文件中使用什么过滤器来生成相同的结果(没有触发错误消息)以及它应该是什么样子?
从 WooCommerce 3 开始,您需要使用 WC_coupon
method get_code()
而不是直接尝试访问优惠券“代码”属性,因此您必须在代码中替换:
<!-- Print the Coupon Code -->
<td><?php echo $coupon->code; ?></td>
只需
<!-- Print the Coupon Code -->
<td><?php echo $coupon->get_code(); ?></td>
这将解决这个问题。
您可以使用 woocommerce_get_order_item_totals
过滤器挂钩,而不是覆盖 order-details.php
模板,来添加一些自定义行来对显示的总行进行排序。
见those examples using woocommerce_get_order_item_totals
filter hook。
1° 出于必要,我在模板 order-details.php 文件中创建了这个条件,我知道,不推荐这样做。
2° 现在我必须将此脚本放入 order-details.php(结帐页面)中的 运行 过滤器中.
3° 问题:当我为产品应用优惠券代码并下订单时,我总是收到以下错误debug.log 文件中的消息
[20-Nov-2020 14:19:21 UTC] PHP Notice: code was called <strong> incorrectly </strong>.
Coupon properties should not be accessed directly.
Backtrace: require('wp-blog-header.php'),
require_once('wp-includes/template-loader.php'),
include('/themes/astra/page.php'),
astra_content_page_loop,
do_action('astra_content_page_loop'),
WP_Hook->do_action,
WP_Hook->apply_filters, Astra_Loop->loop_markup_page, Astra_Loop->loop_markup,
do_action('astra_page_template_parts_content'), WP_Hook->do_action, WP_Hook->apply_filters,
Astra_Loop->template_parts_page, get_template_part, locate_template, load_template,
require('/themes/astra/template-parts/content-page.php'), the_content, apply_filters('the_content'),
WP_Hook->apply_filters, do_shortcode, preg_replace_callback, do_shortcode_tag,
WC_Shortcodes::checkout, WC_Shortcodes::shortcode_wrapper, WC_Shortcode_Checkout::output,
WC_Shortcode_Checkout::order_received, wc_get_template, include('/themes/astra-child/woocommerce/
checkout/thankyou.php'), do_action('woocommerce_thankyou'), WP_Hook->do_action, WP_Hook->app in
C:\xampppserver2\htdocs\mrdigital\wp-includes\functions.php on line 5229
这是我的订单-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 = true;
// Para exibir o template ENDEREÇO DE FATURAMENTO na página
$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,
)
);
}
?>
<ul class="list-unstyled">
<li class="text-right">
<a href="<?php echo site_url(); ?>" class="button">Página Inicial</a>
</li>
</ul>
<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
}
// **** Inserted Lines - Coupon Start ****
if( $order->get_coupon_codes() ) {
foreach( $order->get_coupon_codes() as $code) {
// Pick up the object of each coupon
$coupon = new WC_Coupon($code);
?>
<tr>
<th>Cupom</th>
<!-- Print the Coupon Code -->
<td><?php echo $coupon->code; ?></td>
</tr>
<tr>
<th>Coupon Expiration Date</th>
<!-- Print Coupon Expiration Date -->
<td><?php print_r($coupon->get_date_expires()->date('d-m-Y')); ?></td>
</tr>
<?php }
}
// **** Inserted Lines - Coupon End ****
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 ) );
}
只是要看的优惠券脚本::
// **** Inserted Lines - Coupon Start ****
if( $order->get_coupon_codes() ) {
foreach( $order->get_coupon_codes() as $code) {
// Pick up the object of each coupon
$coupon = new WC_Coupon($code);
?>
<tr>
<th>Coupon</th>
<!-- Print the Coupon Code -->
<td><?php echo $coupon->code; ?></td>
</tr>
<tr>
<th>Coupon Expiration Date</th>
<!-- Print Coupon Expiration Date -->
<td><?php print_r($coupon->get_date_expires()->date('d-m-Y')); ?></td>
</tr>
<?php }
}
// **** Inserted Lines - Coupon End ****
4° 我应该在 functions.php 文件中使用什么过滤器来生成相同的结果(没有触发错误消息)以及它应该是什么样子?
从 WooCommerce 3 开始,您需要使用 WC_coupon
method get_code()
而不是直接尝试访问优惠券“代码”属性,因此您必须在代码中替换:
<!-- Print the Coupon Code -->
<td><?php echo $coupon->code; ?></td>
只需
<!-- Print the Coupon Code -->
<td><?php echo $coupon->get_code(); ?></td>
这将解决这个问题。
您可以使用 woocommerce_get_order_item_totals
过滤器挂钩,而不是覆盖 order-details.php
模板,来添加一些自定义行来对显示的总行进行排序。
见those examples using woocommerce_get_order_item_totals
filter hook。