WC_Coupon_Data_Store_CPT::read()publicWC 3.0.0

Method to read a coupon.

Method of the class: WC_Coupon_Data_Store_CPT{}

Hooks from the method

Return

null. Nothing (null).

Usage

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

Changelog

Since 3.0.0 Introduced.

WC_Coupon_Data_Store_CPT::read() code WC 9.4.2

public function read( &$coupon ) {
	$coupon->set_defaults();

	$post_object = get_post( $coupon->get_id() );

	if ( ! $coupon->get_id() || ! $post_object || 'shop_coupon' !== $post_object->post_type ) {
		throw new Exception( __( 'Invalid coupon.', 'woocommerce' ) );
	}

	$coupon_id              = $coupon->get_id();
	$limit_usage_to_x_items = get_post_meta( $coupon_id, 'limit_usage_to_x_items', true );
	$coupon->set_props(
		array(
			'code'                        => $post_object->post_title,
			'description'                 => $post_object->post_excerpt,
			'status'                      => $post_object->post_status,
			'date_created'                => $this->string_to_timestamp( $post_object->post_date_gmt ),
			'date_modified'               => $this->string_to_timestamp( $post_object->post_modified_gmt ),
			'date_expires'                => metadata_exists( 'post', $coupon_id, 'date_expires' ) ? get_post_meta( $coupon_id, 'date_expires', true ) : get_post_meta( $coupon_id, 'expiry_date', true ), // @todo: Migrate expiry_date meta to date_expires in upgrade routine.
			'discount_type'               => get_post_meta( $coupon_id, 'discount_type', true ),
			'amount'                      => get_post_meta( $coupon_id, 'coupon_amount', true ),
			'usage_count'                 => get_post_meta( $coupon_id, 'usage_count', true ),
			'individual_use'              => 'yes' === get_post_meta( $coupon_id, 'individual_use', true ),
			'product_ids'                 => $this->get_coupon_meta_as_array( $coupon_id, 'product_ids' ),
			'excluded_product_ids'        => $this->get_coupon_meta_as_array( $coupon_id, 'exclude_product_ids' ),
			'usage_limit'                 => get_post_meta( $coupon_id, 'usage_limit', true ),
			'usage_limit_per_user'        => get_post_meta( $coupon_id, 'usage_limit_per_user', true ),
			'limit_usage_to_x_items'      => $limit_usage_to_x_items > 0 ? $limit_usage_to_x_items : null,
			'free_shipping'               => 'yes' === get_post_meta( $coupon_id, 'free_shipping', true ),
			'product_categories'          => array_filter( (array) get_post_meta( $coupon_id, 'product_categories', true ) ),
			'excluded_product_categories' => array_filter( (array) get_post_meta( $coupon_id, 'exclude_product_categories', true ) ),
			'exclude_sale_items'          => 'yes' === get_post_meta( $coupon_id, 'exclude_sale_items', true ),
			'minimum_amount'              => get_post_meta( $coupon_id, 'minimum_amount', true ),
			'maximum_amount'              => get_post_meta( $coupon_id, 'maximum_amount', true ),
			'email_restrictions'          => array_filter( (array) get_post_meta( $coupon_id, 'customer_email', true ) ),
			'used_by'                     => array_filter( (array) get_post_meta( $coupon_id, '_used_by' ) ),
		)
	);
	$coupon->read_meta_data();
	$coupon->set_object_read( true );
	do_action( 'woocommerce_coupon_loaded', $coupon );
}