WC_REST_Coupons_V2_Controller::prepare_object_for_database
Prepare a single coupon for create or update.
Метод класса: WC_REST_Coupons_V2_Controller{}
Хуки из метода
Возвращает
WP_Error|WC_Data.
Использование
// protected - в коде основоного (родительского) или дочернего класса $result = $this->prepare_object_for_database( $request, $creating );
- $request(WP_REST_Request) (обязательный)
- Request object.
- $creating(true|false)
- If is creating a new object.
По умолчанию:false
Код WC_REST_Coupons_V2_Controller::prepare_object_for_database() WC REST Coupons V2 Controller::prepare object for database WC 11.2.2
protected function prepare_object_for_database( $request, $creating = false ) {
$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 ( $creating && StringUtil::is_null_or_whitespace( $request['code'] ?? null ) ) {
return new WP_Error( 'woocommerce_rest_empty_coupon_code', sprintf( __( 'The coupon code cannot be empty.', 'woocommerce' ), 'code' ), array( 'status' => 400 ) );
}
// Handle setting discount type first because that is needed for validation.
if ( isset( $request['discount_type'] ) ) {
$coupon->set_discount_type( $request['discount_type'] );
}
// When both minimum_amount and maximum_amount are present in the same
// request, pass them together through the coupon's set_props() so the
// pair is validated as a unit. Calling the setters sequentially (as
// the default loop below does) causes the first setter to compare its
// new value against the stale stored value of the other field, which
// spuriously rejects valid simultaneous raises like min=100/max=200 →
// min=250/max=300.
$amount_keys_handled = false;
if ( ! is_null( $request['minimum_amount'] ) && ! is_null( $request['maximum_amount'] ) ) {
$amount_result = $coupon->set_props(
array(
'minimum_amount' => $request['minimum_amount'],
'maximum_amount' => $request['maximum_amount'],
)
);
if ( is_wp_error( $amount_result ) ) {
return $amount_result;
}
$amount_keys_handled = true;
}
// Handle all writable props.
foreach ( $data_keys as $key ) {
// minimum_amount and maximum_amount were already applied atomically above.
if ( $amount_keys_handled && in_array( $key, array( 'minimum_amount', 'maximum_amount' ), true ) ) {
continue;
}
$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':
MetaDataUtil::update( $value, $coupon );
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;
}
}
}
/**
* Filters an object before it is inserted via the REST API.
*
* The dynamic portion of the hook name, `$this->post_type`,
* refers to the object type slug.
*
* @param WC_Data $coupon Object object.
* @param WP_REST_Request $request Request object.
* @param bool $creating If is creating a new object.
*/
return apply_filters( "woocommerce_rest_pre_insert_{$this->post_type}_object", $coupon, $request, $creating );
}