獲取

2017-07-07 48 views
1

我已經在WooCommerce創建了兩個自定義的優惠券類型的WooCommerce訂單優惠券折扣類型和數量:獲取

function custom_discount_type($discount_types) { 
    $discount_types['cash_back_fixed'] =__('Cash Back fixed discount', 'woocommerce'); 
    $discount_types['cash_back_percentage'] =__('Cash Back Percentage discount', 'woocommerce'); 
     return $discount_types; 

    } 

add_filter('woocommerce_coupon_discount_types', 'custom_discount_type',10, 1); 

我想獲得的量販式後的訂單狀態是「已完成」,是這樣的:

function wc_m_move_order_money_to_user($order_id, $old_status, $new_status){ 

    if($order->get_used_coupons()) { 
     if ($coupon->type == 'cash_back_fixed'){ 
      $coupons_amont = ??? 
      .... 

     } 
    } 
} 

$coupon->type不起作用。

如何獲取訂單中使用的優惠券類型?
如何獲得原始優惠券金額?

感謝

+0

如何找到優惠券金額(不是訂單中使用的優惠券金額) – Gaurav

+0

謝謝你的支持。現在刪除了重複的問題。 – Gaurav

回答

5

更新

首先,你不能訪問到WC對象的屬性,因爲WooCommerce 3+,你需要使用WC_Couponget_discount_type()方法或is_type('cash_back_fixed')方法...

這裏是方法:

// Get an instance of WC_Order object 
$order = wc_get_order($order_id); 

// Coupons used in the order LOOP (as they can be multiple) 
foreach($order->get_used_coupons() as $coupon_name){ 

    // Retrieving the coupon ID 
    $coupon_post_obj = get_page_by_title($coupon_name, OBJECT, 'shop_coupon'); 
    $coupon_id = $coupon_post_obj->ID; 

    // Get an instance of WC_Coupon object in an array(necesary to use WC_Coupon methods) 
    $coupons_obj = new WC_Coupon($coupon_id); 

    // Now you can get type in your condition 
    if ($coupons_obj->get_discount_type() == 'cash_back_percentage'){ 
     // Get the coupon object amount 
     $coupons_amount1 = $coupons_obj->get_amount(); 
    } 

    // Or use this other conditional method for coupon type 
    if($coupons_obj->is_type('cash_back_fixed')){ 
     // Get the coupon object amount 
     $coupons_amount2 = $coupons_obj->get_amount(); 
    } 
} 

要獲得優惠券的折扣數額(並且還使用優惠券類型的方法)這裏是這樣的:得到WC_Coupon價格

global $wpdb; 
$order = wc_get_order($order_id); 

## GET THE ORDER LINE ITEMS 
$table = $wpdb->prefix . "woocommerce_order_items"; 
$order_items = $wpdb->get_results("SELECT * FROM $table WHERE order_id = $order_id "); 

print_r($order_items); 

## GET THE COUPONS AMOUNTS IN THE ORDER 
foreach($order_items as $item_values){ 

    // Targeting "coupon" order item type 
    if('coupon' == $item_values->order_item_type){ 

     // Retrieving the coupon ID reference 
     $coupon_post_obj = get_page_by_title($item_values->order_item_name, OBJECT, 'shop_coupon'); 
     $coupon_id = $coupon_post_obj->ID; 

     // Get an instance of WC_Coupon object (necesary to use WC_Coupon methods) 
     $coupons_obj = new WC_Coupon($coupon_id); 

     ## Filtering with your coupon custom types 
     if($coupons_obj->is_type('cash_back_fixed') || $coupons_obj->is_type('cash_back_percentage') || ){ 

      // Get the corresponding Item ID 
      $item_id = $item_values->order_item_id; 

      // Get the Coupon discount amounts in the order 
      $order_discount_amount = wc_get_order_item_meta($item_id, 'discount_amount', true); 
      $order_discount_tax_amount = wc_get_order_item_meta($item_id, 'discount_amount_tax', true); 

      ## Or get the coupon amount object 
      $coupons_amount = $coupons_obj->get_amount(); 
     } 
    } 
} 

現在,只需要使用get_amount()方法