【问题标题】:Get Order items and WC_Order_Item_Product in WooCommerce 3在 WooCommerce 3 中获取订单商品和 WC_Order_Item_Product
【发布时间】:2018-01-24 03:37:24
【问题描述】:

阅读 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

也许我在这里忽略了一些东西?

【问题讨论】:

    标签: php wordpress woocommerce product orders


    【解决方案1】:

    如果您使用 get_id() 方法,您会在代码中获得您的商品 ID,即 15

    获取产品 ID:
    获取 Product Id 的正确 WC_Order_Item_Product 方法是:get_product_id()

    获取变体 ID
    正确的 WC_Order_Item_Product 获取变体 ID 的方法是:get_variation_id()

    获取订单 ID
    获取 Order 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()

    从订单商品 ID 中获取 WC_Product 对象:

    $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)。取消保护WC_Order_Item_Product 数据 和自定义元数据:

    您可以使用所有WC_Order_Item_Product data 方法,也可以使用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 ){
    
        // 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 ……
    }
    

    作为参考:

    【讨论】:

    • 我不确定这是在做什么。我的电话是get_id(),我真的不知道那是产品ID还是其他对象ID。如果有区别,你能详细说明一下吗?
    • 好吧,您似乎要返回产品 id,但我相信我需要返回对象,而不是 id...get_id() 只是检查它是否有 id,但对象是被退回的不是产品 ID。基本上,我不确定get_id() 是否可以在这种情况下使用。也许这很好,我不知道,但我收到一个 PHP 通知,上面写着 wc_deprecated_function
    • 很好的答案。将 $item 变量命名为 $product 可能是理想的选择。
    【解决方案2】:

    WC_Order_Item_Product 继承自 WC_Order_Item,WC_Order_Item 有 get_order_id(),所以你可以通过

    $order_item->get_order_id();
    

    【讨论】:

    • get_order_id() 返回的结果与get_id() 相同。哦,谢谢,由于某种原因,我忽略了继承的类。 get_id() 来自WC_Data,我认为这与来自WC_Order_Item 类的get_order_id() 不同......
    • 对,我看错了你的问题。答案几乎是一样的,就像你看到的,get_id() 方法也继承自 WC_Data。你遇到了什么错误?
    猜你喜欢
    • 1970-01-01
    • 2019-02-28
    • 2021-04-02
    • 2017-09-17
    • 2019-05-23
    • 2019-01-26
    • 2019-02-23
    • 2018-10-14
    • 2019-03-24
    相关资源
    最近更新 更多