Woocommerce - Getting the order item price and quantity.

前端 未结 3 628
一整个雨季
一整个雨季 2020-12-13 10:13

Using Woocommerce 2.6.8 , I can\'t get the Order Item Data information as described in the docs and here on SO.

All I want is to get the Line Item price and Quantity

相关标签:
3条回答
  • 2020-12-13 10:36

    Item price can get from order object by below code

    $order->get_item_total( $item );
    
    0 讨论(0)
  • 2020-12-13 10:56

    Please see this documentation for woocommerce Line item in order class. Here

    You can call total for get the total order cost. If you want to retrieve the single item cost by taking the product_id

    $_product = wc_get_product( $product_id );
    $Price = $_product->get_price();
    

    Or you can do this.

    $price = get_post_meta( get_the_ID(), '_regular_price', true);
    $price = get_post_meta( get_the_ID(), '_sale_price', true);
    

    this get the product price today, not the price when made the order. so if a order is made with unit price 10 and then the unit price is changed you will get the new price. the order still has the old "original" order price..

    0 讨论(0)
  • 2020-12-13 10:59

    Update (For WooCommerce 3+)

    Now for the code you can use WC_Order_Item_Product (and WC_Product) methods instead, like:

    ## For WooCommerce 3+ ##
    
    // Getting an instance of the WC_Order object from a defined ORDER ID
    $order = wc_get_order( $order_id ); 
    
    // Iterating through each "line" items in the order
    foreach ($order->get_items() as $item_id => $item ) {
    
        // Get an instance of corresponding the WC_Product object
        $product        = $item_data->get_product();
    
        $active_price   = $product->get_price(); // The product active raw price
    
        $regular_price  = $product->get_sale_price(); // The product raw sale price
    
        $sale_price     = $product->get_regular_price(); // The product raw regular price
    
        $product_name   = $item->get_name(); // Get the item name (product name)
    
        $item_quantity  = $item->get_quantity(); // Get the item quantity
    
        $item_subtotal  = $item->get_subtotal(); // Get the item line total non discounted
    
        $item_subto_tax = $item->get_subtotal_tax(); // Get the item line total tax non discounted
    
        $item_total     = $item->get_total(); // Get the item line total discounted
    
        $item_total_tax = $item->get_total_tax(); // Get the item line total  tax discounted
    
        $item_taxes     = $item->get_taxes(); // Get the item taxes array
    
        $item_tax_class = $item->get_tax_class(); // Get the item tax class
    
        $item_tax_status= $item->get_tax_status(); // Get the item tax status
    
        $item_downloads = $item->get_item_downloads(); // Get the item downloads
    
        // Displaying this data (to check)
        echo 'Product name: '.$product_name.' | Quantity: '.$item_quantity.' | Item total: '. number_format( $item_total, 2 );
    }
    

    Update: Also all the following WC_Abstract_Order methods allow to get order items data with various interesting options like:

    // Getting an instance of the WC_Order object from a defined ORDER ID
    $order = wc_get_order( $order_id ); 
    
    // Iterating through each "line" items in the order
    foreach ($order->get_items() as $item_id => $item_data) {
         ## Option: Including or excluding Taxes
         $inc_tax = true; 
    
         ## Option: Round at item level (or not)
         $round   = false; // Not rounded at item level ("true"  for rounding at item level)
    
         $item_cost_excl_disc = $order->get_item_subtotal( $item, $inc_tax, $round ); // Calculate item cost (not discounted) - useful for gateways.
    
         $item_cost_incl_disc = $order->get_item_total( $item, $inc_tax, $round ); // Calculate item cost (discounted) - useful for gateways.
    
         $item_tax_cost       = $order->get_item_tax( $item, $inc_tax, $round ); // Get item tax cost - useful for gateways.
    
          $item_Line_subtotal = $order->get_line_subtotal( $item, $inc_tax, $round ); // Get line subtotal - not discounted.
    
         $item_Line_total     = $order->get_line_total( $item, $inc_tax, $round ); // Get line total - discounted
    
         $item_Line_tax       = $order->get_line_tax( $item ); // Get line tax
    
         $form_line_subtotal  = $order->get_formatted_line_subtotal( $item, $tax_display = '' ) // Gets line subtotal - formatted for display.
    }
    

    Thanks to @Casper for his comment


    Also WC_Data methods can be used to get order item data as an unprotected array or to get a specific nested or custom meta data value from a specific meta key:

    // Getting an instance of the WC_Order object from a defined ORDER ID
    $order = wc_get_order( $order_id ); 
    
    // Iterating through each "line" items in the order
    foreach ($order->get_items() as $item_id => $item ) {
        $order_item_data      = $item->get_data(); // Get WooCommerce order item meta data in an unprotected array
        print_r( $order_item_data ); // display raw data
    
        $item_meta_data = $item->get_meta_data(); // Get order item nested and custom meta data in an unprotected array
        print_r( $item_meta_data ); // display raw data
    
        $item_value     = $item->get_meta('meta_key'); // Get specific order item custom or nested meta data value from a meta_key
        print_r( $item_value ); // display raw data (can be a string or an array)
    }
    

    This code is tested and works.

    Method get_item_meta() is deprecated and has been replaced by wc_get_order_item_meta and it's not anymore a method but a function with some parameters:

    /** Parameters summary
    
     * @param mixed $item_id
     * @param mixed $key
     * @param bool $single (default: true)
     * @return mixed
     */
    
    wc_get_order_item_meta( $item_id, $key, $single = true );
    

    Prior versions of woocommerce (from 2.4 to 2.6.x)

    You can use get_item_meta() WC_Abstract_order method, to get the order metadata (the item quantity and the item price total).

    So your code will be:

    // Getting the order object "$order"
    $order = wc_get_order( $order_id );
    // Getting the items in the order
    $order_items = $order->get_items();
    // Iterating through each item in the order
    foreach ($order_items as $item_id => $item) {
        // Get the product name
        $product_name = $item['name'];
        // Get the item quantity
        $item_quantity = $order->get_item_meta($item_id, '_qty', true);
        // Get the item line total
        $item_total = $order->get_item_meta($item_id, '_line_total', true);
    
        // Displaying this data (to check)
        echo 'Product name: '.$product_name.' | Quantity: '.$item_quantity.' | Item total: '. $item_total;
    }
    

    This code is tested and fully functional.

    Reference: Class WC_Abstract_Order Methods

    0 讨论(0)
提交回复
热议问题