WC_Coupon_Data_Store_CPT::update_post_meta()privateWC 3.0.0

Helper method that updates all the post meta for a coupon based on it's settings in the WC_Coupon class.

Method of the class: WC_Coupon_Data_Store_CPT{}

Return

null. Nothing (null).

Usage

// private - for code of main (parent) class only
$result = $this->update_post_meta( $coupon );
$coupon(WC_Coupon) (required) (passed by reference — &)
Coupon object.

Changelog

Since 3.0.0 Introduced.

WC_Coupon_Data_Store_CPT::update_post_meta() code WC 9.4.2

private function update_post_meta( &$coupon ) {
	$meta_key_to_props = array(
		'discount_type'              => 'discount_type',
		'coupon_amount'              => 'amount',
		'individual_use'             => 'individual_use',
		'product_ids'                => 'product_ids',
		'exclude_product_ids'        => 'excluded_product_ids',
		'usage_limit'                => 'usage_limit',
		'usage_limit_per_user'       => 'usage_limit_per_user',
		'limit_usage_to_x_items'     => 'limit_usage_to_x_items',
		'usage_count'                => 'usage_count',
		'date_expires'               => 'date_expires',
		'free_shipping'              => 'free_shipping',
		'product_categories'         => 'product_categories',
		'exclude_product_categories' => 'excluded_product_categories',
		'exclude_sale_items'         => 'exclude_sale_items',
		'minimum_amount'             => 'minimum_amount',
		'maximum_amount'             => 'maximum_amount',
		'customer_email'             => 'email_restrictions',
	);

	$props_to_update = $this->get_props_to_update( $coupon, $meta_key_to_props );
	foreach ( $props_to_update as $meta_key => $prop ) {
		$value = $coupon->{"get_$prop"}( 'edit' );
		$value = is_string( $value ) ? wp_slash( $value ) : $value;
		switch ( $prop ) {
			case 'individual_use':
			case 'free_shipping':
			case 'exclude_sale_items':
				$value = wc_bool_to_string( $value );
				break;
			case 'product_ids':
			case 'excluded_product_ids':
				$value = implode( ',', array_filter( array_map( 'intval', $value ) ) );
				break;
			case 'product_categories':
			case 'excluded_product_categories':
				$value = array_filter( array_map( 'intval', $value ) );
				break;
			case 'email_restrictions':
				$value = array_filter( array_map( 'sanitize_email', $value ) );
				break;
			case 'date_expires':
				$value = $value ? $value->getTimestamp() : null;
				break;
		}

		$updated = $this->update_or_delete_post_meta( $coupon, $meta_key, $value );

		if ( $updated ) {
			$this->updated_props[] = $prop;
		}
	}

	do_action( 'woocommerce_coupon_object_updated_props', $coupon, $this->updated_props );
}