programing

WooCommerce 주문에서 쿠폰 데이터 가져오기

cafebook 2023. 4. 3. 21:45
반응형

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

주문 상태가 "completed"인 후 다음과 같은 할인 유형을 받고 싶습니다.

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동작하지 않습니다.

주문에 사용된 쿠폰 종류는 어떻게 받을 수 있나요?
그리고 원래 쿠폰 금액은 어떻게 받을 수 있나요?

감사해요.

업데이트 3

WooCommerce 3.7 이후부터는WC_Abstract의 메서드WC_Order명령에서 사용한 쿠폰을 가져오는 인스턴스 오브젝트.get_used_coupons()메서드는 권장되지 않습니다.

따라서 다음 코드로 대체합니다.

foreach( $order->get_used_coupons() as $coupon_code ){

기준:

foreach( $order->get_coupon_codes() as $coupon_code ){

그러면 다음과 같은 쿠폰 세부 정보를 얻을 수 있습니다.

foreach( $order->get_coupon_codes() as $coupon_code ) {
    // Get the WC_Coupon object
    $coupon = new WC_Coupon($coupon_code);

    $discount_type = $coupon->get_discount_type(); // Get coupon discount type
    $coupon_amount = $coupon->get_amount(); // Get coupon amount
}

업데이트 2

우선 WoCommerce 3 이후로는 WC 오브젝트 속성에 접근할 수 없습니다.

이제 getter 메서드를 사용하여 쿠폰 세부 정보를 얻어야 합니다.WC_Coupon오브젝트 인스턴스...

고객님의 경우 방법 또는 방법을 사용해야 합니다.

방법은 다음과 같습니다.

// 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_code ){

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

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

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

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

쿠폰 할인 금액(및 쿠폰 유형 방법 사용)을 얻으려면 다음과 같이 하십시오.

$order = wc_get_order( $order_id );

// GET THE ORDER COUPON ITEMS
$order_items = $order->get_items('coupon');

// print_r($order_items); // For testing

// LOOP THROUGH ORDER COUPON ITEMS
foreach( $order_items as $item_id => $item ){

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

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

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

        // 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->get_amount();
    }
}

그래서 쿠폰 가격을 얻기 위해 우리는 그 방법을get_amount() 사용한다.

제 경우, 특정 쿠폰이 사용되고 있는 경우는, 새로운 주문을 「프리 오더」(시스템에 커스텀 스테이터스가 이미 추가되어 있는 경우)로 변경하고, 지불(처리 스테이터스가 유효한 경우)할 필요가 있었습니다.

function change_status_to_preorder($order_id) {
    
    $order          = new WC_Order($order_id);
    $coupon_codes   = $order->get_coupon_codes();
    
    if(in_array('MYCOUPON', $coupon_codes) && $order->get_status() == 'processing') {
        $order->update_status('wc-pre-order', 'AUTO: note to shop manager');
    }
}
add_action('woocommerce_new_order', 'change_status_to_preorder', 1, 1);

언급URL : https://stackoverflow.com/questions/44977174/get-coupon-data-from-woocommerce-orders

반응형