| 1 |
<?php |
| 2 |
|
| 3 |
namespace SpringDevs\Subscription\Illuminate; |
| 4 |
|
| 5 |
/** |
| 6 |
* Class AutoRenewal |
| 7 |
* |
| 8 |
* @package SpringDevs\Subscription\Illuminate |
| 9 |
*/ |
| 10 |
class AutoRenewal { |
| 11 |
|
| 12 |
/** |
| 13 |
* Initialize the class |
| 14 |
*/ |
| 15 |
public function __construct() { |
| 16 |
add_action( 'subscrpt_subscription_expired', array( $this, 'after_subscription_expired' ) ); |
| 17 |
add_filter( |
| 18 |
'subscrpt_renewal_item_meta', |
| 19 |
function ( $item_meta, $product ) { |
| 20 |
if ( 'updated' !== get_option( 'subscrpt_renewal_price', 'subscribed' ) || ! $product ) { |
| 21 |
return $item_meta; |
| 22 |
} |
| 23 |
if ( $product->is_type( 'variable' ) ) { |
| 24 |
$product = wc_get_product( $product->get_meta( '_subscrpt_convertation_default_variation_for_renewal', true ) ); |
| 25 |
if ( ! $product ) { |
| 26 |
return $item_meta; |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
$timing_per = $product->get_meta( '_subscrpt_timing_per' ); |
| 31 |
$timing_option = $product->get_meta( '_subscrpt_timing_option' ); |
| 32 |
|
| 33 |
return array( |
| 34 |
'time' => empty( $timing_per ) ? 1 : $timing_per, |
| 35 |
'type' => $timing_option, |
| 36 |
'trial' => null, |
| 37 |
); |
| 38 |
}, |
| 39 |
10, |
| 40 |
2 |
| 41 |
); |
| 42 |
add_filter( |
| 43 |
'subscrpt_renewal_product_args', |
| 44 |
function ( $product_args, $product, $order_item ) { |
| 45 |
if ( 'updated' !== get_option( 'subscrpt_renewal_price', 'subscribed' ) ) { |
| 46 |
return $product_args; |
| 47 |
} |
| 48 |
|
| 49 |
if ( ! $product ) { |
| 50 |
if ( ! is_admin() ) { |
| 51 |
wc_add_notice( __( 'Subscription early renewal order creation failed due to product deletion !', 'sdevs_subscrpt' ), 'error' ); |
| 52 |
} |
| 53 |
return false; |
| 54 |
} |
| 55 |
|
| 56 |
if ( $product->is_type( 'variable' ) ) { |
| 57 |
$product = wc_get_product( $product->get_meta( '_subscrpt_convertation_default_variation_for_renewal', true ) ); |
| 58 |
if ( ! $product ) { |
| 59 |
if ( ! is_admin() ) { |
| 60 |
wc_add_notice( __( 'Subscription early renewal order creation failed due to product deletion !', 'sdevs_subscrpt' ), 'error' ); |
| 61 |
} |
| 62 |
return false; |
| 63 |
} |
| 64 |
} |
| 65 |
$product_args = array( |
| 66 |
'name' => $product->get_name(), |
| 67 |
'subtotal' => $product->get_price() * $order_item->get_quantity(), |
| 68 |
'total' => $product->get_price() * $order_item->get_quantity(), |
| 69 |
); |
| 70 |
|
| 71 |
return $product_args; |
| 72 |
}, |
| 73 |
10, |
| 74 |
3 |
| 75 |
); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* After Expired Subscription. |
| 80 |
* |
| 81 |
* @param int $subscription_id Subscription ID. |
| 82 |
*/ |
| 83 |
public function after_subscription_expired( $subscription_id ) { |
| 84 |
if ( subscrpt_is_auto_renew_enabled() ) { |
| 85 |
Helper::create_renewal_order( $subscription_id ); |
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
|