Php WooCommerce优惠券-如何对实际订单进行响应
我试图在页面上用于订购的特定位置回显优惠券代码(如果它被使用的话) 我正在使用以下代码:Php WooCommerce优惠券-如何对实际订单进行响应,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我试图在页面上用于订购的特定位置回显优惠券代码(如果它被使用的话) 我正在使用以下代码: if(function_exists('print_coupon_name')){ global $woocommerce; $order = new WC_Order($order_id); $coupons = $order->get_used_coupons(); echo "Coupon: " . $coupons; } 我得到了这个结果: Coupon: Array 是否需要从阵列中提
if(function_exists('print_coupon_name')){
global $woocommerce;
$order = new WC_Order($order_id);
$coupons = $order->get_used_coupons();
echo "Coupon: " . $coupons;
}
我得到了这个结果:
Coupon: Array
是否需要从阵列中提取某些数据?上面的代码真的是从实际订单中获取优惠券数据,还是我需要以某种方式预先选择订单?我做错了什么?函数返回空数组或优惠券名称数组(如果存在)。 您需要检查返回的数组是否为空,并在其上循环
if(function_exists('print_coupon_name')){
global $woocommerce;
$order = new WC_Order($order_id);
$coupons = $order->get_used_coupons();
if ( ! empty( $coupons ) ) {
foreach ( $coupons as $coupon ) {
echo "Coupon: " . $coupon;
}
}
}