You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WooCommerce购物车页添加优惠券折扣百分比报错求助

问题:WooCommerce购物车显示优惠券折扣百分比触发PHP错误

尝试在WooCommerce购物车页面显示优惠券折扣百分比,但出现了PHP错误,以下是自定义代码:

// Add Coupon Percentage to Cart

function my_coupon_percentage_cart($value, $coupon)
{
    if($coupon->discount_type == 'percent' && !empty($coupon->coupon_amount))
    {
        $amt = "<br><br><p><em><strong>{$coupon->coupon_amount}% OFF</strong></em></p>";   
    }

    return $value.$amt;
}
add_filter('woocommerce_cart_totals_coupon_html','my_coupon_percentage_cart',10,2);

出现的PHP错误如下:

[15-Sep-2022 13:55:13 UTC] discount_type was called incorrectly. Coupon properties should not be accessed directly. Backtrace: require('wp-blog-header.php'), require_once('wp-includes/template-loader.php'), do_action('template_redirect'), WP_Hook->do_action, WP_Hook->apply_filters, WC_AJAX::do_wc_ajax, do_action('wc_ajax_update_shipping_method'), WP_Hook->do_action, WP_Hook->apply_filters, WC_AJAX::update_shipping_method, WC_AJAX::get_cart_totals, woocommerce_cart_totals, wc_get_template, include('/plugins/woocommerce/templates/cart/cart-totals.php'), wc_cart_totals_coupon_html, apply_filters('woocommerce_cart_totals_coupon_html'), WP_Hook->apply_filters, my_coupon_percentage_cart, WC_Legacy_Coupon->__get, wc_doing_it_wrong. This message was added in version 3.0.
[15-Sep-2022 13:55:13 UTC] coupon_amount was called incorrectly. Coupon properties should not be accessed directly. Backtrace: require('wp-blog-header.php'), require_once('wp-includes/template-loader.php'), do_action('template_redirect'), WP_Hook->do_action, WP_Hook->apply_filters, WC_AJAX::do_wc_ajax, do_action('wc_ajax_update_shipping_method'), WP_Hook->do_action, WP_Hook->apply_filters, WC_AJAX::update_shipping_method, WC_AJAX::get_cart_totals, woocommerce_cart_totals, wc_get_template, include('/plugins/woocommerce/templates/cart/cart-totals.php'), wc_cart_totals_coupon_html, apply_filters('woocommerce_cart_totals_coupon_html'), WP_Hook->apply_filters, my_coupon_percentage_cart, WC_Legacy_Coupon->__get, wc_doing_it_wrong. This message was added in version 3.0.
[15-Sep-2022 13:55:13 UTC] coupon_amount was called incorrectly. Coupon properties should not be accessed directly. Backtrace: require('wp-blog-header.php'), require_once('wp-includes/template-loader.php'), do_action('template_redirect'), WP_Hook->do_action, WP_Hook->apply_filters, WC_AJAX::do_wc_ajax, do_action('wc_ajax_update_shipping_method'), WP_Hook->do_action, WP_Hook->apply_filters, WC_AJAX::update_shipping_method, WC_AJAX::get_cart_totals, woocommerce_cart_totals, wc_get_template, include('/plugins/woocommerce/templates/cart/cart-totals.php'), wc_cart_totals_coupon_html, apply_filters('woocommerce_cart_totals_coupon_html'), WP_Hook->apply_filters, my_coupon_percentage_cart, WC_Legacy_Coupon->__get, wc_doing_it_wrong. This message was added in version 3.0.

问题原因与修复方案

问题根源

错误提示明确指出:优惠券属性不能直接访问,这是WooCommerce 3.0及以后版本的开发规范。你的代码直接调用了优惠券对象的discount_type和coupon_amount属性,违反了官方规范,因此触发了警告。

修复后的代码

// Add Coupon Percentage to Cart
function my_coupon_percentage_cart($value, $coupon) {
    // 初始化变量避免未定义警告
    $amt = '';
    
    // 使用官方推荐的getter方法获取属性
    if($coupon->get_discount_type() == 'percent' && !empty($coupon->get_amount())) {
        $discount_amount = $coupon->get_amount();
        $amt = "<br><br><p><em><strong>{$discount_amount}% OFF</strong></em></p>";   
    }

    return $value . $amt;
}
add_filter('woocommerce_cart_totals_coupon_html', 'my_coupon_percentage_cart', 10, 2);

关键修改点

  • 用$coupon->get_discount_type()替代直接访问$coupon->discount_type
  • 用$coupon->get_amount()替代直接访问$coupon->coupon_amount
  • 提前初始化$amt为空字符串,避免当优惠券不符合条件时出现未定义变量的PHP警告

内容的提问来源于stack exchange,提问作者Wanderlust Consulting

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 12:05:29