| 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( 'subscrpt_renewal_item_meta', array( $this, 'filter_renewal_item_meta' ), 10, 2 ); |
| 18 |
add_filter( 'subscrpt_renewal_product_args', array( $this, 'filter_renewal_product_args' ), 10, 3 ); |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* Filter renewal product args. |
| 23 |
* |
| 24 |
* @param array $product_args product args. |
| 25 |
* @param \WC_Product $product Product Object. |
| 26 |
* @param \WC_Order_Item $order_item Order Item Object. |
| 27 |
* |
| 28 |
* @return array |
| 29 |
*/ |
| 30 |
public function filter_renewal_product_args( $product_args, $product, $order_item ) { |
| 31 |
if ( 'updated' !== get_option( 'subscrpt_renewal_price', 'subscribed' ) ) { |
| 32 |
return $product_args; |
| 33 |
} |
| 34 |
|
| 35 |
if ( ! $product ) { |
| 36 |
if ( ! is_admin() ) { |
| 37 |
wc_add_notice( __( 'Subscription early renewal order creation failed due to product deletion !', 'sdevs_subscrpt' ), 'error' ); |
| 38 |
} |
| 39 |
return false; |
| 40 |
} |
| 41 |
|
| 42 |
if ( $product->is_type( 'variable' ) ) { |
| 43 |
$product = wc_get_product( $product->get_meta( '_subscrpt_convertation_default_variation_for_renewal', true ) ); |
| 44 |
if ( ! $product ) { |
| 45 |
if ( ! is_admin() ) { |
| 46 |
wc_add_notice( __( 'Subscription early renewal order creation failed due to product deletion !', 'sdevs_subscrpt' ), 'error' ); |
| 47 |
} |
| 48 |
return false; |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
$product_args = array( |
| 53 |
'name' => $product->get_name(), |
| 54 |
'subtotal' => $product->get_price() * $order_item->get_quantity(), |
| 55 |
'total' => $product->get_price() * $order_item->get_quantity(), |
| 56 |
); |
| 57 |
|
| 58 |
return $product_args; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Filter renewal item's meta. |
| 63 |
* |
| 64 |
* @param array $item_meta Item meta. |
| 65 |
* @param \WC_Product $product Product Object. |
| 66 |
* |
| 67 |
* @return array |
| 68 |
*/ |
| 69 |
public function filter_renewal_item_meta( $item_meta, $product ) { |
| 70 |
if ( 'updated' !== get_option( 'subscrpt_renewal_price', 'subscribed' ) || ! $product ) { |
| 71 |
return $item_meta; |
| 72 |
} |
| 73 |
|
| 74 |
if ( $product->is_type( 'variable' ) ) { |
| 75 |
$product = wc_get_product( $product->get_meta( '_subscrpt_convertation_default_variation_for_renewal', true ) ); |
| 76 |
if ( ! $product ) { |
| 77 |
return $item_meta; |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
$timing_per = $product->get_meta( '_subscrpt_timing_per' ); |
| 82 |
$timing_option = $product->get_meta( '_subscrpt_timing_option' ); |
| 83 |
|
| 84 |
return array( |
| 85 |
'time' => empty( $timing_per ) ? 1 : $timing_per, |
| 86 |
'type' => $timing_option, |
| 87 |
'trial' => null, |
| 88 |
); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* After Expired Subscription. |
| 93 |
* |
| 94 |
* @param int $subscription_id Subscription ID. |
| 95 |
*/ |
| 96 |
public function after_subscription_expired( $subscription_id ) { |
| 97 |
if ( subscrpt_is_auto_renew_enabled() ) { |
| 98 |
Helper::create_renewal_order( $subscription_id ); |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
|