2016-06-11 65 views

回答

2

這是基於圍繞解決您的問題編制的搜索,但我不確定這(未經測試)代碼片段:

function wc_my_order_coupons($order_id) { 
    $order = new WC_Order($order_id); 

    // checking if order has some used coupons 
    if($order->get_used_coupons()) { 
     $order_coupons = $order->get_used_coupons(); 
     /* echo var_dump($order_coupons); */ 

     foreach($order->$order_coupons as $coupon) { 
      $coupon_id = $coupon->id; 
      /* echo var_dump($coupon_id); */ 

      // here is a list of most all data that you can use for a coupon 
      $cp_discount_type = get_post_meta($coupon_id, 'discount_type', true); 
      $cp_amount = get_post_meta($coupon_id, 'coupon_amount', true); 
      $cp_indiv_use = get_post_meta($coupon_id, 'individual_use', true); 
      $cp_products = get_post_meta($coupon_id, 'product_ids'); 
      $cp_excl_products = get_post_meta($coupon_id, 'exclude_product_ids'); 
      $cp_usage_limit = get_post_meta($coupon_id, 'usage_limit', true); 
      $cp_expiry_date = get_post_meta($coupon_id, 'expiry_date', true); 
      $cp_apply_before_tax = get_post_meta($coupon_id, 'apply_before_tax', true); 
      $cp_free_shipping = get_post_meta($coupon_id, 'free_shipping', true); 

      // Getting the products in this order_id 
      $items = $order->get_items(); 
      foreach ($items as $key => $item) { 
       $product_name = $item['name']; 
       $product_id = $item['product_id']; 
       /* echo var_dump($product_id); */ 

       // Example: we use product_ids authorized in the coupon $cp_products 
       if (in_array($product_id, $cp_products)) { 
        echo $product_name . 'uses the coupon: ' . $coupon->code . '<br>'; 
       } 
      } 
     } 
    } 
} 

給我一些反饋...我會調整這個代碼,因爲有多種方式可以做到這一點。

參考文獻:

+0

Thaks的片段@LoicTheAztec,真的很感激。對不起,延遲迴復。我已使用此代碼,但如果優惠券可應用於兩種或更多產品,則會出現問題,我是否正確? –

+0

@MûhámmàdYäsårK是的,這可能是正確的。但它可以肯定地解決,因爲任何事情都可能取決於你的時間和努力。感謝您投票給我:)這段代碼是否工作? – LoicTheAztec

相關問題