Skip to content

Instantly share code, notes, and snippets.

@mholubowski
Created December 21, 2022 19:13
Show Gist options
  • Save mholubowski/6acd41eb3befbe514b52fb7c5fd11bc5 to your computer and use it in GitHub Desktop.
Save mholubowski/6acd41eb3befbe514b52fb7c5fd11bc5 to your computer and use it in GitHub Desktop.
Solid Affiliate - James CodeWP
<?php
// James wants to limit recurring referrals to 1 or 6 periods depending on the product.
// 'solid_affiliate/should_skip_referral_creation_for_order' is the filter to use.
// The filter is passed a tuple [bool, string[]] where the bool is whether or not to skip the referral creation and the string[] is the reasons why.
// The filter is also passed the OrderDescription object. Using the OrderDescriptions, we need to determine whether or not the order is a recurring order, and if so, how many months it is recurring for.
add_filter(
'solid_affiliate/should_skip_referral_creation_for_order',
/**
* @param array{0: bool, 1: string[]} $tuple
* @param OrderDescription $order_description
*
* @return array{0: bool, 1: string[]}
**/
function ($tuple, $order_description) {
if ($order_description->is_renewal_order) {
$wc_order = wc_get_order($order_description->order_id);
if ($wc_order instanceof \WC_Order) {
// get the subscription object
$subscriptions = wcs_get_subscriptions_for_order($wc_order);
$subscription = array_pop($subscriptions);
if ($subscription instanceof \WC_Subscription) {
// check when the subscription started
$subscription_start_date = $subscription->get_date_created();
$renewal_order_date = $wc_order->get_date_created();
if ($subscription_start_date instanceof \WC_DateTime && $renewal_order_date instanceof \WC_DateTime) {
$months_since_subscription_start = $subscription_start_date->diff($renewal_order_date)->m;
////////////////////////////////////////
// TODO Logic in here. If you determine the referral should be skipped then return:
// return [true, array_merge($tuple[1], ['This Referral was skipped via custom snippet. We limit the amount of recurring referral periods for this product.'])]
}
}
}
};
return $tuple;
},
10,
2
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment