WC_Coupon_Data_Store_CPT::create()publicWC 3.0.0

Method to create a new coupon in the database.

Method of the class: WC_Coupon_Data_Store_CPT{}

Return

null. Nothing (null).

Usage

$WC_Coupon_Data_Store_CPT = new WC_Coupon_Data_Store_CPT();
$WC_Coupon_Data_Store_CPT->create( $coupon );
$coupon(WC_Coupon) (required) (passed by reference — &)
Coupon object.

Changelog

Since 3.0.0 Introduced.

WC_Coupon_Data_Store_CPT::create() code WC 9.4.2

public function create( &$coupon ) {
	if ( ! $coupon->get_date_created( 'edit' ) ) {
		$coupon->set_date_created( time() );
	}

	$coupon_id = wp_insert_post(
		apply_filters(
			'woocommerce_new_coupon_data',
			array(
				'post_type'     => 'shop_coupon',
				'post_status'   => 'publish',
				'post_author'   => get_current_user_id(),
				'post_title'    => $coupon->get_code( 'edit' ),
				'post_content'  => '',
				'post_excerpt'  => $coupon->get_description( 'edit' ),
				'post_date'     => gmdate( 'Y-m-d H:i:s', $coupon->get_date_created()->getOffsetTimestamp() ),
				'post_date_gmt' => gmdate( 'Y-m-d H:i:s', $coupon->get_date_created()->getTimestamp() ),
			)
		),
		true
	);

	if ( $coupon_id ) {
		$coupon->set_id( $coupon_id );
		$this->update_post_meta( $coupon );
		$coupon->save_meta_data();
		$coupon->apply_changes();
		delete_transient( 'rest_api_coupons_type_count' );
		do_action( 'woocommerce_new_coupon', $coupon_id, $coupon );
	}
}