在Thankyou.php上获取Woocommerce优惠券ID

时间:2020-02-11 18:09:43

标签: php wordpress woocommerce coupon

我正尝试在Woocommerce谢谢页面上将一些订单详细信息传递给第三方。一切正常,除了获取优惠券ID。

我想念什么?我已经阅读了this question,但并没有过多地阐明主题。

似乎没有关于WC优惠券的大量文档...谢谢!

function goodcart_tracking( $order_id ) {

    $order = new WC_Order( $order_id );
    $order_number = $order->get_id();
    $currency = $order->get_order_currency();
    $email = $order->get_billing_email();
    $total = $order->get_total();
    $coupon = $order->get_used_coupons().implode(",",$coupons);
    $date = $order->order_date;
?>

    <script type="text/javascript">
        var params = {
            'gc_hash': 'HASH_ID',
            'customer_email': '<?php echo $email ?>',
            'order_id': '<?php echo $order_number; ?>',
            'order_amount': '<?php echo $total; ?>',
            'order_amount_type': '<?php echo $currency; ?>',
            'promo_code': '<?php echo $coupon; ?>'
        };
        iGCCpnObj = new _iGCBannerObj(params); iGCCpnObj.start();
  </script>
<?php  } 

1 个答案:

答案 0 :(得分:1)

假设这可以帮助您进一步

$order = new WC_Order( $order_id );

$coupons = $order->get_used_coupons();

foreach( $coupons as $coupon ){

    // Coupon id
    $coupon_post_object = get_page_by_title($coupon, OBJECT, 'shop_coupon');
    $coupon_id          = $coupon_post_object->ID;

    echo $coupon_id . '<br>';

    // Coupon object
    $coupon_object = new WC_Coupon($coupon_id);

    // echo...
    echo $coupon_object->get_code() . '<br>';
    //etc...
    // view: https://docs.woocommerce.com/wc-apidocs/class-WC_Coupon.html
}