Created
November 24, 2020 12:56
-
-
Save praveencs87/a5c25d9c403e76473209bbab847a9d22 to your computer and use it in GitHub Desktop.
Create Couponcode Dynamically
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Code Description given in https://webinwordpress.com/uncategorized/how-to-create-and-apply-coupon-code-programmatically-in-woocoomerce/ | |
$coupon_code = 'WEBINWORDPRESS.COM'; // Code - this can be a dynamic string like $UserRole.$order | |
$amount = '20'; // Amount | |
$discount_type = 'percent'; // Types are : fixed_cart, percent, fixed_product, percent_product | |
$coupon = array( | |
'post_title' => $coupon_code, | |
'post_content' => '', | |
'post_status' => 'publish', | |
'post_author' => 10, //user_id dynamic value, usually admin user id | |
'post_type' => 'shop_coupon' | |
); | |
$new_coupon_id = wp_insert_post( $coupon ); | |
// Add post meta | |
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', 'yes' ); | |
update_post_meta( $new_coupon_id, 'product_ids', '' ); | |
update_post_meta( $new_coupon_id, 'exclude_product_ids', '' ); | |
update_post_meta( $new_coupon_id, 'usage_limit', '1' ); | |
update_post_meta( $new_coupon_id, 'expiry_date', '' ); | |
update_post_meta( $new_coupon_id, 'apply_before_tax', 'no' ); | |
update_post_meta( $new_coupon_id, 'free_shipping', 'no' ); | |
update_post_meta( $new_coupon_id, 'exclude_sale_items', 'no' ); | |
update_post_meta( $new_coupon_id, 'free_shipping', 'no' ); | |
update_post_meta( $new_coupon_id, 'product_categories', '' ); | |
update_post_meta( $new_coupon_id, 'exclude_product_categories', '' ); | |
update_post_meta( $new_coupon_id, 'minimum_amount', '' ); | |
update_post_meta( $new_coupon_id, 'customer_email', '' ); | |
// Refer https://woocommerce.github.io/code-reference/classes/WC-Coupon.html | |
// Code Description given in https://webinwordpress.com/uncategorized/how-to-create-and-apply-coupon-code-programmatically-in-woocoomerce/ | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment