WC_REST_Orders_V1_Controller::prepare_line_items
Create or update a line item.
Метод класса: WC_REST_Orders_V1_Controller{}
Хуков нет.
Возвращает
WC_Order_Item_Product.
Использование
// protected - в коде основоного (родительского) или дочернего класса $result = $this->prepare_line_items( $posted, $action );
- $posted(массив) (обязательный)
- Line item data.
- $action(строка)
'create'to add line item or'update'to update it.
По умолчанию:'create'
Код WC_REST_Orders_V1_Controller::prepare_line_items() WC REST Orders V1 Controller::prepare line items WC 11.1.0
protected function prepare_line_items( $posted, $action = 'create' ) {
$item = new WC_Order_Item_Product( ! empty( $posted['id'] ) ? $posted['id'] : '' );
$product = wc_get_product( $this->get_product_id( $posted, $action ) );
$current_product_id = (int) $item->get_product_id( 'edit' );
$current_variation_id = (int) $item->get_variation_id( 'edit' );
// set_product() clears a variation when given its parent. REST partial updates restore it only
// when the posted parent and SKU-resolved product still identify the current item.
// An explicit variation_id of 0 still demotes the item.
$same_product_update = 'update' === $action
&& array_key_exists( 'product_id', $posted )
&& $product instanceof WC_Product
&& (int) $posted['product_id'] === $current_product_id
&& in_array( $product->get_id(), array( $current_product_id, $current_variation_id ), true );
$restore_variation_id = $same_product_update
&& $current_variation_id
&& ( ! array_key_exists( 'variation_id', $posted ) || (int) $posted['variation_id'] === $current_variation_id );
$clear_variation_id = $same_product_update
&& $current_variation_id
&& array_key_exists( 'variation_id', $posted )
&& 0 === (int) $posted['variation_id'];
if ( $clear_variation_id && $product instanceof WC_Product_Variation ) {
$product = wc_get_product( $current_product_id );
}
if ( $product && $product !== $item->get_product() ) {
$item->set_product( $product );
if ( $restore_variation_id ) {
try {
$item->set_variation_id( $current_variation_id );
} catch ( WC_Data_Exception $e ) {
if ( 'order_item_product_invalid_variation_id' !== $e->getErrorCode() ) {
throw $e;
}
// The stored variation ID no longer identifies a variation. Keep set_product()'s parent demotion.
// Unlike v2, no get_post_type() recheck is needed: $item is always a base WC_Order_Item_Product
// (never a woocommerce_get_order_item_classname subclass), whose setter throws this code only
// when the post is not a product_variation.
wc_get_logger()->warning(
sprintf(
'Order item #%d (order #%d) referenced variation #%d, which no longer exists; the item was demoted to its parent product during a REST update.',
$item->get_id(),
$item->get_order_id(),
$current_variation_id
),
array( 'source' => 'rest-api' )
);
}
}
if ( 'create' === $action ) {
$quantity = isset( $posted['quantity'] ) ? $posted['quantity'] : 1;
$total = wc_get_price_excluding_tax( $product, array( 'qty' => $quantity ) );
$item->set_total( $total );
$item->set_subtotal( $total );
}
}
if ( $clear_variation_id ) {
$item->set_variation_id( 0 );
}
$this->maybe_set_item_props( $item, array( 'name', 'quantity', 'total', 'subtotal', 'tax_class' ), $posted );
return $item;
}