Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php WooCommerce优惠券-如何对实际订单进行响应_Php_Wordpress_Woocommerce - Fatal编程技术网

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;
      }
   }
}