복붙노트

Woocommerce 3에서 주문 항목 및 WC_Order_Item_Product 가져 오기

PHP

Woocommerce 3에서 주문 항목 및 WC_Order_Item_Product 가져 오기

Ok, 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 함수를 사용하도록이 코드를 변경하는 방법을 모르겠습니다.이 클래스는 더 이상 구조가 없습니다. 이 작업을 올바르게 수행하는 방법은 무엇입니까? 위와 같은 방식으로 주문 항목을 가져 오는 기능이 표시되지 않습니다. https://docs.woocommerce.com/wc-apidocs/class-WC_Order_Item_Product.html

어쩌면 여기서 뭔가를 놓칠까요?

해결법

  1. ==============================

    1.제품 ID 가져 오기 : 제품 ID를 가져 오는 올바른 WC_Order_Item_Product 메소드는 다음과 같습니다. get_product_id ()

    제품 ID 가져 오기 : 제품 ID를 가져 오는 올바른 WC_Order_Item_Product 메소드는 다음과 같습니다. get_product_id ()

    주문 ID 가져 오기 순서 ID를 가져 오는 올바른 WC_Order_Item_Product 메소드는 다음과 같습니다. get_order_id ()

    WC_Product 객체 얻기 WC_Product 객체를 가져 오는 올바른 WC_Order_Item_Product 메소드는 다음과 같습니다. get_product ()

    WC_Order 객체 가져 오기 WC_order 오브젝트를 가져 오는 올바른 WC_Order_Item_Product 메소드는 다음과 같습니다. get_order ()

    WC_Data 메소드를 사용하여 데이터 및 메타 데이터 확보 및 보호 해제 : get_data () get_meta_data ()

    WC_Product 객체 가져 오기 :

    $order_item_id = 15;
    $order_item = new WC_Order_Item_Product($order_item_id);
    
    // The product ID
    $product_id = $order_item->get_product_id(); 
    // The order ID
    $order_id = $order_item->get_order_id(); 
    
    // The WC_Product object
    $product = $order_item->get_product(); 
    // The WC_Order object
    $order = $order_item->get_order(); 
    
    // The item ID
    $item_id = $order_item->get_id(); // which is your $order_item_id
    

    WC_Order 객체에서 주문 항목을 가져옵니다.

    $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 ){
        //Get the product ID
        $product_id = $item_product->get_product_id();
        //Get the WC_Product object
        $product = $item_product->get_product();
        //Get the product SKU (using WC_Product method)
        $sku = $product->get_sku();
    }
    

    WC_Order_Item_Product 데이터에 대한 액세스 및 보호 해제 :

    모든 WC_Order_Item_Product 데이터 메소드를 사용하거나 다음 메소드를 사용하여 WC_Data를 사용하여 데이터의 보호를 해제 할 수 있습니다.

    $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 ){
    
        // Get the common data in an array: 
        $item_product_data_array = $item_product->get_data();
    
        // Get the special meta data in an array: 
        $item_product_meta_data_array = $item_product->get_meta_data();
    
        // Get the specific meta data from a meta_key: 
        $meta_value = $item_product->get_meta( 'custom_meta_key', true );
    
        // get only additional meta data (formatted in an unprotected array)
        $formatted_meta_data = $item->get_formatted_meta_data();
    }
    

    참조 용 :

  2. ==============================

    2.WC_Order_Item_Product는 get_order_id ()가있는 WC_Order_Item에서 상속되므로 주문 ID를 가져올 수 있습니다.

    WC_Order_Item_Product는 get_order_id ()가있는 WC_Order_Item에서 상속되므로 주문 ID를 가져올 수 있습니다.

    $order_item->get_order_id();
    
  3. from https://stackoverflow.com/questions/45706007/get-order-items-and-wc-order-item-product-in-woocommerce-3 by cc-by-sa and MIT license