WC_REST_Orders_V2_Controller::prepare_coupon_lines()protectedWC 1.0

Create or update an order coupon.

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

Хуков нет.

Возвращает

WC_Order_Item_Coupon.

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

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->prepare_coupon_lines( $posted, $action, $item );
$posted(массив) (обязательный)
Item data.
$action(строка)
'create' to add coupon or 'update' to update it.
По умолчанию: 'create'
$item(объект)
Passed when updating an item. Null during creation.
По умолчанию: null

Код WC_REST_Orders_V2_Controller::prepare_coupon_lines() WC 8.7.0

protected function prepare_coupon_lines( $posted, $action = 'create', $item = null ) {
	$item = is_null( $item ) ? new WC_Order_Item_Coupon( ! empty( $posted['id'] ) ? $posted['id'] : '' ) : $item;

	if ( 'create' === $action ) {
		$coupon_code = ArrayUtil::get_value_or_default( $posted, 'code' );
		if ( StringUtil::is_null_or_whitespace( $coupon_code ) ) {
			throw new WC_REST_Exception( 'woocommerce_rest_invalid_coupon_coupon', __( 'Coupon code is required.', 'woocommerce' ), 400 );
		}
	}

	$this->maybe_set_item_props( $item, array( 'code', 'discount' ), $posted );
	$this->maybe_set_item_meta_data( $item, $posted );

	return $item;
}