WC_REST_Coupons_V1_Controller::save_coupon()protectedWC 3.0.0

Saves a coupon to the database.

Method of the class: WC_REST_Coupons_V1_Controller{}

No Hooks.

Return

WP_Error|Int.

Usage

// protected - for code of main (parent) or child class
$result = $this->save_coupon( $request );
$request(WP_REST_Request) (required)
Full details about the request.

Changelog

Since 3.0.0 Introduced.

WC_REST_Coupons_V1_Controller::save_coupon() code WC 8.7.0

protected function save_coupon( $request ) {
	try {
		$coupon = $this->prepare_item_for_database( $request );

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

		$coupon->save();
		return $coupon->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() ) );
	}
}