Help with creating coupons programmatically
-
Hello,
For a project i am trying to create coupons programmatically with some data ( acquired points)
When i am trying to run it via a shortcode on a random page everything works expect the saving of the coupons.
When i try to save it with add_action(‘admin_init’, ‘userbyidtest’) it works but i don’t want it that way.
I really want it to work as a shortcode when i visit a certain page on this case. Can anyone have a look and help me out?function userbyidtest(){ $users = get_users(); global $wpdb; global $woocommerce; $percentage = 10; foreach($users as $user){ $usermail = $user->user_email; $userdname = $user->display_name; $userID = $user->ID; $totalpoints = $wpdb->get_var( "SELECT points FROM wp_wc_points_rewards_user_points WHERE user_id='$userID'"); //COUPON SETTINGS $expdate = '20 days'; if($totalpoints > 10){ // COUPON SETTINGS 2 $totaldiscount = ($percentage / 100) * $totalpoints; $mintotal = $totaldiscount * 4; $coupon = new WC_Coupon(); $userdname2 = str_replace(' ', '', $userdname); $userdname2 = strtolower($userdname2); $coupon_code = $userdname2 . '-' . $totaldiscount . 'dc'; $coupon->set_code($coupon_code); //the coupon discount type can be 'fixed_cart', 'percent' or 'fixed_product', defaults to 'fixed_cart' $coupon->set_discount_type('fixed_cart'); //the discount amount, defaults to zero $coupon->set_amount($totaldiscount); //the coupon's expiration date defaults to null $coupon->set_date_expires(null); //determines if the coupon can only be used by an individual, defaults to false $coupon->set_individual_use(false); //the individual prodcuts that the disciunt will apply to, default to an empty array $coupon->set_product_ids(array()); //the individual products that are excluded from the discount, default to an empty array $coupon->set_excluded_product_ids(array()); //the times the coupon can be used, defaults to zero $coupon->set_usage_limit(1); //the times the coupon can be used per user, defaults to zero $coupon->set_usage_limit_per_user(0); //whether the coupon awards free shipping, defaults to false $coupon->set_free_shipping(false); //the product categories included in the promotion, defaults to an empty array $coupon->set_product_categories(array()); //the product categories excluded from the promotion, defaults to an empty array $coupon->set_excluded_product_categories(array()); //whether sale items are excluded from the coupon, defaults to false $coupon->set_exclude_sale_items(true); //the minimum amount of spend required to make the coupon active, defaults to an empty string $coupon->set_minimum_amount($mintotal); //the maximum amount of spend required to make the coupon active, defaults to an empty string $coupon->set_maximum_amount(''); //a list of email addresses, the coupon will only be applied if the customer is linked to one of the listed emails, defaults to an empty array $coupon->set_email_restrictions($usermail); //save the coupon $coupon->save(); } } } add_shortcode( 'userbyidtest', 'userbyidtest'); //add_action('admin_init', 'userbyidtest');
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
The topic ‘Help with creating coupons programmatically’ is closed to new replies.