programing

WooCommerce 3에서 주문 항목과 WC_Order_Item_Product를 가져옵니다.

iphone6s 2023. 2. 26. 09:18
반응형

WooCommerce 3에서 주문 항목과 WC_Order_Item_Product를 가져옵니다.

WooCommerce 3.0의 변경 내용을 읽어보면, 아이템을 직접 주문하는 것은 불가능할 것 같기 때문에 다음 코드를 변경할 필요가 있다고 생각합니다.

$order_item_id = 15;
$order_item = new WC_Order_Item_Product($order_item_id);
$return = $order_item->get_id() ? $order_item : false;

하지만 더 이상 구문이 없는 이 클래스의 최신 버전에서 올바른 새로운 getter 및 setter 함수를 사용하기 위해 이 코드를 어떻게 변경해야 할지 모르겠습니다.어떻게 하면 좋을까요?아무 것도 안 보여…get위와 같이 주문 품목을 받을 수 있습니다.
https://docs.woocommerce.com/wc-apidocs/class-WC_Order_Item_Product.html

제가 뭔가를 간과하고 있는 건 아닐까요?

메소드를 사용하면 코드에 포함된 항목 ID를 얻을 수 있습니다.

제품 ID를 가져옵니다.
정답WC_Order_Item_Product제품 ID를 취득하는 방법은 다음과 같습니다.

변동 ID를 가져옵니다.
정답WC_Order_Item_Product변동 ID를 가져오는 방법은 다음과 같습니다.

주문 ID 가져오기
정답WC_Order_Item_Product주문 ID를 취득하는 방법은 다음과 같습니다.

WC_Product 개체를 가져옵니다.
정답WC_Order_Item_Product입수 방법WC_Product오브젝트는 다음과 같습니다.

WC_Order 개체를 가져옵니다.
정답WC_Order_Item_Product입수 방법WC_order오브젝트는 다음과 같습니다.

다음 방법을 사용하여 데이터메타 데이터 가져오기 및 보호 해제:

  • get_data()
  • get_meta_data()

입수방법WC_Product주문 항목 ID의 객체:

$order_item_id = 15;
$item = new WC_Order_Item_Product($order_item_id);

// The product ID
$product_id = $item->get_product_id(); 

// The variation ID
$variation_id = $item->get_variation_id(); 

// The WC_Product object
$product = $item->get_product(); 

// The quantity
$quantity = $item->get_quantity(); 

// The order ID
$order_id = $item->get_order_id(); 

// The WC_Order object
$order = $item->get_order(); 

// The item ID
$item_id = $item->get_id(); // which is your $order_item_id

// The product name
$product_name = $item->get_name(); // … OR: $product->get_name();

// Get the product SKU (using WC_Product method)
$sku = $product->get_sku();

// Get line item totals (non discounted)
$total     = $item->get_subtotal(); // Total without tax (non discounted)
$total_tax = $item->get_subtotal_tax(); // Total tax (non discounted)

// Get line item totals (discounted when a coupon is applied)
$total     = $item->get_total(); // Total without tax (discounted)
$total_tax = $item->get_total_tax(); // Total tax (discounted)

에서 주문 아이템을 가져옵니다.WC_Order물건(및 WC_product 오브젝트):

$order_id = 156; // The order_id

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

// The loop to get the order items which are WC_Order_Item_Product objects since WC 3+
foreach( $order->get_items() as $item_id => $item ){
    //Get the product ID
    $product_id = $item->get_product_id();

    //Get the variation ID
    $variation_id = $item->get_variation_id();

    //Get the WC_Product object
    $product = $item->get_product();

    // The quantity
    $quantity = $item->get_quantity();

    // The product name
    $product_name = $item->get_name(); // … OR: $product->get_name();

    //Get the product SKU (using WC_Product method)
    $sku = $product->get_sku();

    // Get line item totals (non discounted)
    $total     = $item->get_subtotal(); // Total without tax (non discounted)
    $total_tax = $item->get_subtotal_tax(); // Total tax (non discounted)
    
    // Get line item totals (discounted when a coupon is applied)
    $total     = $item->get_total(); // Total without tax (discounted)
    $total_tax = $item->get_total_tax(); // Total tax (discounted)
}

###데이터 및 커스텀 메타데이터 접근:

1) 데이터 보호 해제 및 사용자 지정 메타 데이터:

모든 방법을 사용하거나 다음 방법을 사용하여 데이터 보호를 해제할 수 있습니다.

$order_id = 156; // The order_id

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

// The loop to get the order items which are WC_Order_Item_Product objects since WC 3+
foreach( $order->get_items() as $item_id => $item ){

    // Get the common data in an array: 
    $item_product_data_array = $item->get_data();

    // Get the special meta data in an array: 
    $item_product_meta_data_array = $item->get_meta_data();

    // Get the specific meta data from a meta_key: 
    $meta_value = $item->get_meta( 'custom_meta_key', true );

    // Get all additional meta data (formatted in an unprotected array)
    $formatted_meta_data = $item->get_formatted_meta_data( ' ', true );


    // Get line item totals (non discounted)
    $total     = $item->get_subtotal(); // Total without tax (non discounted)
    $total_tax = $item->get_subtotal_tax(); // Total tax (non discounted)
    
    // Get line item totals (discounted when a coupon is applied)
    $total     = $item->get_total(); // Total without tax (discounted)
    $total_tax = $item->get_total_tax(); // Total tax (discounted)
}

2) 어레이 액세스는 (레거시 어레이와의 하위 호환성을 위해) 공통 데이터를 직접 가져올 수 있습니다.

$order_id = 156; // The order_id

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

// The loop to get the order items which are WC_Order_Item_Product objects since WC 3+
foreach( $order->get_items() as $item_id => $item ){

    
    $product_id    = $item['product_id']; // Get the product ID
    $variation_id  = $item['variation_id']; // Get the variation ID

    $product_name  = $item['name']; // The product name
    $item_qty      = $item['quantity']; // The quantity

    // Get line item totals (non discounted)
    $line_total     = $item['subtotal']; // or $item['line_subtotal'] -- The line item non discounted total
    $line_total_tax = $item['subtotal_tax']; // or $item['line_subtotal_tax'] -- The line item non discounted tax total

    // Get line item totals (discounted)
    $line_total2     = $item['total']; // or $item['line_total'] -- The line item non discounted total
    $line_total_tax2 = $item['total_tax']; // The line item non discounted tax total

    // And so on ……
}

참고 자료:

WC_Order_Item_Product는 get_Order_id()를 가진 WC_Order_Item에서 상속되므로 주문 ID를 얻을 수 있습니다.

$order_item->get_order_id();

언급URL : https://stackoverflow.com/questions/45706007/get-order-items-and-wc-order-item-product-in-woocommerce-3

반응형