WC_REST_Legacy_Coupons_Controller::prepare_item_for_database()protectedWC 1.0

Устарела с версии 3.0.0. Больше не поддерживается и может быть удалена. Рекомендуется заменить эту функцию на аналог.

Prepare a single coupon for create or update.

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

Хуки из метода

Возвращает

WP_Error|stdClass. $data Post object.

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

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->prepare_item_for_database( $request );
$request(WP_REST_Request) (обязательный)
Request object.

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

Устарела с 3.0.0

Код WC_REST_Legacy_Coupons_Controller::prepare_item_for_database() WC 8.7.0

protected function prepare_item_for_database( $request ) {
	global $wpdb;

	$id        = isset( $request['id'] ) ? absint( $request['id'] ) : 0;
	$coupon    = new WC_Coupon( $id );
	$schema    = $this->get_item_schema();
	$data_keys = array_keys( array_filter( $schema['properties'], array( $this, 'filter_writable_props' ) ) );

	// Validate required POST fields.
	if ( 'POST' === $request->get_method() && 0 === $coupon->get_id() ) {
		if ( empty( $request['code'] ) ) {
			return new WP_Error( 'woocommerce_rest_empty_coupon_code', sprintf( __( 'The coupon code cannot be empty.', 'woocommerce' ), 'code' ), array( 'status' => 400 ) );
		}
	}

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

		if ( ! is_null( $value ) ) {
			switch ( $key ) {
				case 'code' :
					$coupon_code = wc_format_coupon_code( $value );
					$id          = $coupon->get_id() ? $coupon->get_id() : 0;
					$id_from_code = wc_get_coupon_id_by_code( $coupon_code, $id );

					if ( $id_from_code ) {
						return new WP_Error( 'woocommerce_rest_coupon_code_already_exists', __( 'The coupon code already exists', 'woocommerce' ), array( 'status' => 400 ) );
					}

					$coupon->set_code( $coupon_code );
					break;
				case 'meta_data' :
					if ( is_array( $value ) ) {
						foreach ( $value as $meta ) {
							$coupon->update_meta_data( $meta['key'], $meta['value'], isset( $meta['id'] ) ? $meta['id'] : '' );
						}
					}
					break;
				case 'description' :
					$coupon->set_description( wp_filter_post_kses( $value ) );
					break;
				default :
					if ( is_callable( array( $coupon, "set_{$key}" ) ) ) {
						$coupon->{"set_{$key}"}( $value );
					}
					break;
			}
		}
	}

	/**
	 * Filter the query_vars used in `get_items` for the constructed query.
	 *
	 * The dynamic portion of the hook name, $this->post_type, refers to post_type of the post being
	 * prepared for insertion.
	 *
	 * @param WC_Coupon       $coupon        The coupon object.
	 * @param WP_REST_Request $request       Request object.
	 */
	return apply_filters( "woocommerce_rest_pre_insert_{$this->post_type}", $coupon, $request );
}