WC_API_Orders::set_line_item( WC_Order $order, array $item, string $action )

Create or update a line item


Description Description


Parameters Parameters

$order

(Required)

$item

(Required) line item data

$action

(Required) 'create' to add line item or 'update' to update it


Top ↑

Source Source

File: includes/legacy/api/v2/class-wc-api-orders.php

860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
protected function set_line_item( $order, $item, $action ) {
    $creating  = ( 'create' === $action );
 
    // product is always required
    if ( ! isset( $item['product_id'] ) && ! isset( $item['sku'] ) ) {
        throw new WC_API_Exception( 'woocommerce_api_invalid_product_id', __( 'Product ID or SKU is required', 'woocommerce' ), 400 );
    }
 
    // when updating, ensure product ID provided matches
    if ( 'update' === $action ) {
 
        $item_product_id   = wc_get_order_item_meta( $item['id'], '_product_id' );
        $item_variation_id = wc_get_order_item_meta( $item['id'], '_variation_id' );
 
        if ( $item['product_id'] != $item_product_id && $item['product_id'] != $item_variation_id ) {
            throw new WC_API_Exception( 'woocommerce_api_invalid_product_id', __( 'Product ID provided does not match this line item', 'woocommerce' ), 400 );
        }
    }
 
    if ( isset( $item['product_id'] ) ) {
        $product_id = $item['product_id'];
    } elseif ( isset( $item['sku'] ) ) {
        $product_id = wc_get_product_id_by_sku( $item['sku'] );
    }
 
    // variations must each have a key & value
    $variation_id = 0;
    if ( isset( $item['variations'] ) && is_array( $item['variations'] ) ) {
        foreach ( $item['variations'] as $key => $value ) {
            if ( ! $key || ! $value ) {
                throw new WC_API_Exception( 'woocommerce_api_invalid_product_variation', __( 'The product variation is invalid', 'woocommerce' ), 400 );
            }
        }
        $variation_id = $this->get_variation_id( wc_get_product( $product_id ), $item['variations'] );
    }
 
    $product = wc_get_product( $variation_id ? $variation_id : $product_id );
 
    // must be a valid WC_Product
    if ( ! is_object( $product ) ) {
        throw new WC_API_Exception( 'woocommerce_api_invalid_product', __( 'Product is invalid.', 'woocommerce' ), 400 );
    }
 
    // quantity must be positive float
    if ( isset( $item['quantity'] ) && floatval( $item['quantity'] ) <= 0 ) {
        throw new WC_API_Exception( 'woocommerce_api_invalid_product_quantity', __( 'Product quantity must be a positive float.', 'woocommerce' ), 400 );
    }
 
    // quantity is required when creating
    if ( $creating && ! isset( $item['quantity'] ) ) {
        throw new WC_API_Exception( 'woocommerce_api_invalid_product_quantity', __( 'Product quantity is required.', 'woocommerce' ), 400 );
    }
 
    if ( $creating ) {
        $line_item = new WC_Order_Item_Product();
    } else {
        $line_item = new WC_Order_Item_Product( $item['id'] );
    }
 
    $line_item->set_product( $product );
    $line_item->set_order_id( $order->get_id() );
 
    if ( isset( $item['quantity'] ) ) {
        $line_item->set_quantity( $item['quantity'] );
    }
    if ( isset( $item['total'] ) ) {
        $line_item->set_total( floatval( $item['total'] ) );
    } elseif ( $creating ) {
        $total = wc_get_price_excluding_tax( $product, array( 'qty' => $line_item->get_quantity() ) );
        $line_item->set_total( $total );
        $line_item->set_subtotal( $total );
    }
    if ( isset( $item['total_tax'] ) ) {
        $line_item->set_total_tax( floatval( $item['total_tax'] ) );
    }
    if ( isset( $item['subtotal'] ) ) {
        $line_item->set_subtotal( floatval( $item['subtotal'] ) );
    }
    if ( isset( $item['subtotal_tax'] ) ) {
        $line_item->set_subtotal_tax( floatval( $item['subtotal_tax'] ) );
    }
    if ( $variation_id ) {
        $line_item->set_variation_id( $variation_id );
        $line_item->set_variation( $item['variations'] );
    }
 
    // Save or add to order.
    if ( $creating ) {
        $order->add_item( $line_item );
    } else {
        $item_id = $line_item->save();
 
        if ( ! $item_id ) {
            throw new WC_API_Exception( 'woocommerce_cannot_create_line_item', __( 'Cannot create line item, try again.', 'woocommerce' ), 500 );
        }
    }
}

Top ↑

Changelog Changelog

Changelog
Version Description
2.2 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.