Skip to content

Instantly share code, notes, and snippets.

@goranefbl
Created August 13, 2024 08:47
Show Gist options
  • Save goranefbl/d2f7cacdf7f18331cec3bf6a2444036e to your computer and use it in GitHub Desktop.
Save goranefbl/d2f7cacdf7f18331cec3bf6a2444036e to your computer and use it in GitHub Desktop.
Generate coupon once
<?php
add_action('init', 'generate_coupon_manually');
function generate_coupon_manually() {
if (isset($_GET['generate_once']) && $_GET['generate_once'] == 'true') {
// Check if admin
if (!current_user_can('manage_options')) {
wp_die('You do not have permission to access this page.');
}
// Hardcode the order ID here
$order_id = 123; // Replace with your actual order ID
// Check if coupon has already been generated
$order = wc_get_order($order_id);
if ($order->get_meta('_raf_coupon_generated', true)) {
wp_die('Coupon has already been generated for this order.');
}
// Generate coupon
$referral_id = WPGENS_RAF_Order::get_referral_id($order_id);
if (!$referral_id) {
wp_die('Invalid referral ID.');
}
$coupon_object = new WPGens_RAF_Coupons('referrer', $referral_id, $order_id);
$coupon_code = $coupon_object->get_coupon();
if ($coupon_code) {
// Email Coupon Code
if (!WPGENS_RAF_Order::settings_field_active('gens_raf_disable_emails')) {
$email = new WPGens_RAF_Email($coupon_object->coupon_mail, $coupon_code, $order_id);
$email->send_email();
}
// Mark as generated
$order->update_meta_data('_raf_coupon_generated', true);
$order->save();
echo "Coupon generated successfully: " . $coupon_code;
} else {
echo "Failed to generate coupon.";
}
exit;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment