WC_REST_Orders_V2_Controller::save_object()protectedWC 3.0.0

Save an object data.

Метод класса: WC_REST_Orders_V2_Controller{}

Хуков нет.

Возвращает

WC_Data|WP_Error.

Использование

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->save_object( $request, $creating );
$request(WP_REST_Request) (обязательный)
Full details about the request.
$creating(true|false)
If is creating a new object.
По умолчанию: false

Список изменений

С версии 3.0.0 Введена.

Код WC_REST_Orders_V2_Controller::save_object() WC 9.3.3

protected function save_object( $request, $creating = false ) {
	try {
		$object = $this->prepare_object_for_database( $request, $creating );

		if ( is_wp_error( $object ) ) {
			return $object;
		}

		// Make sure gateways are loaded so hooks from gateways fire on save/create.
		WC()->payment_gateways();

		if ( ! is_null( $request['customer_id'] ) && 0 !== $request['customer_id'] ) {
			// Make sure customer exists.
			if ( false === get_user_by( 'id', $request['customer_id'] ) ) {
				throw new WC_REST_Exception( 'woocommerce_rest_invalid_customer_id', __( 'Customer ID is invalid.', 'woocommerce' ), 400 );
			}

			// Make sure customer is part of blog.
			if ( is_multisite() && ! is_user_member_of_blog( $request['customer_id'] ) ) {
				add_user_to_blog( get_current_blog_id(), $request['customer_id'], 'customer' );
			}
		}

		if ( $creating ) {
			$object->set_created_via( 'rest-api' );
			$object->set_prices_include_tax( 'yes' === get_option( 'woocommerce_prices_include_tax' ) );
			$object->save();
			$object->calculate_totals();
		} else {
			// If items have changed, recalculate order totals.
			if ( isset( $request['billing'] ) || isset( $request['shipping'] ) || isset( $request['line_items'] ) || isset( $request['shipping_lines'] ) || isset( $request['fee_lines'] ) || isset( $request['coupon_lines'] ) ) {
				$object->calculate_totals( true );
			}
		}

		// Set status.
		if ( ! empty( $request['status'] ) ) {
			$object->set_status( $request['status'] );
		}

		$object->save();

		// Actions for after the order is saved.
		if ( true === $request['set_paid'] ) {
			if ( $creating || $object->needs_payment() ) {
				$object->payment_complete( $request['transaction_id'] );
			}
		}

		return $this->get_object( $object->get_id() );
	} catch ( WC_Data_Exception $e ) {
		return new WP_Error( $e->getErrorCode(), $e->getMessage(), $e->getErrorData() );
	} catch ( WC_REST_Exception $e ) {
		return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
	}
}