Generate Coupon Code


/*This Function will generate coupon code for woocommerce
# args (array)
*/
function couponcode_gen($vals){

foreach($vals as $code):
$coupon_code = $code; // Code
$amount = ‘19.95’; // Amount
$discount_type = ‘fixed_cart’; // Type: fixed_cart, percent, fixed_product, percent_product
$description = __(‘Oodie Collab’); // description

$coupon = array(
‘post_title’ => $coupon_code,
‘post_content’ => “”,
‘post_status’ => ‘publish’,
‘post_excerpt’ => $description,
‘post_author’ => 1,
‘post_type’ => ‘shop_coupon’);

$new_coupon_id = wp_insert_post( $coupon );

// Add meta
update_post_meta( $new_coupon_id, ‘description’, “Oodie Collab” );
update_post_meta( $new_coupon_id, ‘discount_type’, $discount_type );
update_post_meta( $new_coupon_id, ‘coupon_amount’, $amount );
update_post_meta( $new_coupon_id, ‘individual_use’, ‘no’ );
update_post_meta( $new_coupon_id, ‘product_ids’, ‘1992’ );
update_post_meta( $new_coupon_id, ‘exclude_product_ids’, ” );
update_post_meta( $new_coupon_id, ‘usage_limit’, ‘1’ );
update_post_meta( $new_coupon_id, ‘usage_limit_per_user’, ‘1’ );
update_post_meta( $new_coupon_id, ‘minimum_amount’, ‘24.95’ );
update_post_meta( $new_coupon_id, ‘individual_use’, ‘yes’ );
update_post_meta( $new_coupon_id, ‘expiry_date’, ‘2022-07-31’ );
update_post_meta( $new_coupon_id, ‘apply_before_tax’, ‘yes’ );
update_post_meta( $new_coupon_id, ‘free_shipping’, ‘no’ );

endforeach;
}

#end




Leave a Reply

Your email address will not be published.