Created
January 14, 2024 23:11
-
-
Save goranefbl/1e2711ad7d896bd6d9c3e615897d6225 to your computer and use it in GitHub Desktop.
Add coupon on first renewal.
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 | |
/** | |
* Add Meta box to Order Screen | |
* @author WPGens | |
*/ | |
if (!defined('ABSPATH')) { | |
exit; | |
} | |
class WPGENS_RAF_Order | |
{ | |
/** | |
* Hook in order meta boxes and save order meta | |
* | |
* @since 2.0.0 | |
*/ | |
public function __construct() | |
{ | |
add_action('woocommerce_order_status_' . apply_filters('wpgens_raf_order_status', 'completed'), array($this, 'gens_maybe_create_send_coupon'), 10); | |
add_action('woocommerce_subscription_renewal_payment_complete', array($this, 'handle_subscription_renewal'), 10, 2); | |
} | |
public function handle_subscription_renewal($subscription, $last_order) | |
{ | |
// Check if it's the first renewal | |
if ($subscription->get_payment_count() == 1) { | |
// Get the parent order | |
$parent_order_id = $subscription->get_parent_id(); | |
// Proceed with your existing logic | |
$this->gens_maybe_create_send_coupon($parent_order_id); | |
} | |
} | |
/** | |
* Before version 3, settings field were saved as "yes", after new react admin its "1". | |
*/ | |
public function settings_field_active($field) | |
{ | |
$value = get_option($field); | |
if ($value === "yes" || $value === "1") { | |
return true; | |
} | |
return false; | |
} | |
public function gens_maybe_create_send_coupon($order_id) | |
{ | |
$order = wc_get_order($order_id); | |
$raf_meta = $order->get_meta('_raf_meta', true); | |
// Already generated? Dont generate again | |
// if($order->get_meta( '_raf_coupon_generated', true)) { | |
// return false; | |
// } | |
// No referral ID? Exit. | |
if (!$order->get_meta('_raf_id', true)) { | |
return false; | |
} | |
// Wrong referral ID? Exit | |
$referral_id = $this->get_referral_id($order_id); | |
if (!$referral_id) { | |
return false; | |
} | |
// Advocate Coupon (we need to check this way since ppl who update plugin dont have this field yet) | |
if (get_option('gens_raf_generate_for_order') !== '') { | |
$coupon_object = new WPGens_RAF_Coupons('referrer', $referral_id, $order_id); | |
$coupon_code = $coupon_object->get_coupon(); // returns coupon code | |
// Email Coupon Code | |
if ($coupon_code && !$this->settings_field_active('gens_raf_disable_emails')) { | |
$email = new WPGens_RAF_Email($coupon_object->coupon_mail, $coupon_code, $order_id); | |
$email->send_email(); | |
} | |
if ($coupon_code) { | |
$order = wc_get_order($order_id); | |
$order->update_meta_data('_raf_coupon_generated', true); | |
$order->save(); | |
} | |
} | |
// Friend Coupon | |
if ($this->settings_field_active('gens_raf_friend_enable')) { | |
$friend_coupon_object = new WPGens_RAF_Coupons('friend', $referral_id, $order_id); | |
$friend_coupon_code = $friend_coupon_object->get_coupon(); // returns coupon code | |
} | |
// Email Friend Coupon Code | |
if ($this->settings_field_active('gens_raf_friend_enable') && $friend_coupon_code && !$this->settings_field_active('gens_raf_disable_emails')) { | |
$email = new WPGens_RAF_Email($friend_coupon_object->coupon_mail, $friend_coupon_code, $order_id, 'friend'); | |
$email->send_email(); | |
} | |
// Increase referrals | |
if (isset($raf_meta['increase_referrals']) && $raf_meta['increase_referrals'] == "true") { | |
WPGens_RAF_User::set_number_of_referrals($referral_id); | |
} | |
// Mozda vrati coupon pa snimi neku meta. | |
} | |
public function get_referral_id($order_id) | |
{ | |
$order = wc_get_order($order_id); | |
$rafID = esc_attr($order->get_meta('_raf_id', true)); | |
// Is guest referring? | |
if (filter_var($rafID, FILTER_VALIDATE_EMAIL)) { | |
return $rafID; | |
} | |
$gens_users = get_users(array( | |
"meta_key" => "gens_referral_id", | |
"meta_value" => $rafID, | |
"number" => 1, | |
"fields" => "ID" | |
)); | |
if (is_array($gens_users) && !empty($gens_users)) { | |
return $gens_users[0]; | |
} else { | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment