WordPress at a glance
function is not described

WC_REST_Legacy_Orders_Controller::prepare_item_for_database() protected WC 1.0

Deprecated from version 3.0. It is no longer supported and can be removed in future releases. It is recommended to replace this function with the same one.

Prepare a single order for create.

{} It's a method of the class: WC_REST_Legacy_Orders_Controller{}

Return

WP_Error|WC_Order. $data Object.

Usage

// protected - for code of main (parent) or child class
$result = $this->prepare_item_for_database( $request );
$request(WP_REST_Request) (required)
Request object.

Changelog

Deprecated 3.0

Code of WC_REST_Legacy_Orders_Controller::prepare_item_for_database() WC 5.0.0

<?php
protected function prepare_item_for_database( $request ) {
	$id        = isset( $request['id'] ) ? absint( $request['id'] ) : 0;
	$order     = new WC_Order( $id );
	$schema    = $this->get_item_schema();
	$data_keys = array_keys( array_filter( $schema['properties'], array( $this, 'filter_writable_props' ) ) );

	// Handle all writable props
	foreach ( $data_keys as $key ) {
		$value = $request[ $key ];

		if ( ! is_null( $value ) ) {
			switch ( $key ) {
				case 'billing' :
				case 'shipping' :
					$this->update_address( $order, $value, $key );
					break;
				case 'line_items' :
				case 'shipping_lines' :
				case 'fee_lines' :
				case 'coupon_lines' :
					if ( is_array( $value ) ) {
						foreach ( $value as $item ) {
							if ( is_array( $item ) ) {
								if ( $this->item_is_null( $item ) || ( isset( $item['quantity'] ) && 0 === $item['quantity'] ) ) {
									$order->remove_item( $item['id'] );
								} else {
									$this->set_item( $order, $key, $item );
								}
							}
						}
					}
					break;
				case 'meta_data' :
					if ( is_array( $value ) ) {
						foreach ( $value as $meta ) {
							$order->update_meta_data( $meta['key'], $meta['value'], isset( $meta['id'] ) ? $meta['id'] : '' );
						}
					}
					break;
				default :
					if ( is_callable( array( $order, "set_{$key}" ) ) ) {
						$order->{"set_{$key}"}( $value );
					}
					break;
			}
		}
	}

	/**
	 * Filter the data for the insert.
	 *
	 * The dynamic portion of the hook name, $this->post_type, refers to post_type of the post being
	 * prepared for the response.
	 *
	 * @param WC_Order           $order      The Order object.
	 * @param WP_REST_Request    $request    Request object.
	 */
	return apply_filters( "woocommerce_rest_pre_insert_{$this->post_type}", $order, $request );
}