| 1 |
<?php |
| 2 |
|
| 3 |
namespace SpringDevs\Subscription\Illuminate; |
| 4 |
|
| 5 |
/** |
| 6 |
* Class Order |
| 7 |
* |
| 8 |
* @package SpringDevs\Subscription\Illuminate |
| 9 |
*/ |
| 10 |
class Order { |
| 11 |
|
| 12 |
/** |
| 13 |
* Initialize the class. |
| 14 |
*/ |
| 15 |
public function __construct() { |
| 16 |
add_action( 'woocommerce_admin_order_item_headers', array( $this, 'register_custom_column' ) ); |
| 17 |
add_action( 'woocommerce_admin_order_item_values', array( $this, 'add_column_value' ), 10, 2 ); |
| 18 |
add_action( 'woocommerce_before_order_itemmeta', array( $this, 'add_order_item_data' ), 10, 3 ); |
| 19 |
add_action( 'woocommerce_order_status_changed', array( $this, 'order_status_changed' ) ); |
| 20 |
add_action( 'woocommerce_before_delete_order', array( $this, 'delete_the_subscription' ) ); |
| 21 |
add_action( 'subscrpt_subscription_activated', array( $this, 'generate_dates_for_subscription' ) ); |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Generate start, next and trial dates. |
| 26 |
* |
| 27 |
* @param int $subscription_id Subscription Id. |
| 28 |
* |
| 29 |
* @return void |
| 30 |
*/ |
| 31 |
public function generate_dates_for_subscription( $subscription_id ) { |
| 32 |
$order_item_id = get_post_meta( $subscription_id, '_subscrpt_order_item_id', true ); |
| 33 |
$subscription_history = Helper::get_subscription_from_order_item_id( $order_item_id ); |
| 34 |
|
| 35 |
$order_item_meta = wc_get_order_item_meta( $order_item_id, '_subscrpt_meta' ); |
| 36 |
$type = Helper::get_typos( 1, $order_item_meta['type'] ); |
| 37 |
$trial = get_post_meta( $subscription_id, '_subscrpt_trial', true ); |
| 38 |
if ( 'new' === $subscription_history->type ) { |
| 39 |
$start_date = time(); |
| 40 |
$next_date = sdevs_wp_strtotime( 1 . ' ' . $type, $start_date ); |
| 41 |
if ( $trial && ! empty( $trial ) ) { |
| 42 |
$trial_started = get_post_meta( $subscription_id, '_subscrpt_trial_started', true ); |
| 43 |
$trial_ended = get_post_meta( $subscription_id, '_subscrpt_trial_ended', true ); |
| 44 |
if ( empty( $trial_started ) && empty( $trial_ended ) ) { |
| 45 |
$start_date = sdevs_wp_strtotime( $trial ); |
| 46 |
update_post_meta( $subscription_id, '_subscrpt_trial_started', time() ); |
| 47 |
update_post_meta( $subscription_id, '_subscrpt_trial_ended', $start_date ); |
| 48 |
update_post_meta( $subscription_id, '_subscrpt_trial_mode', 'on' ); |
| 49 |
$next_date = $start_date; |
| 50 |
} |
| 51 |
} |
| 52 |
update_post_meta( $subscription_id, '_subscrpt_start_date', $start_date ); |
| 53 |
} elseif ( 'renew' === $subscription_history->type ) { |
| 54 |
if ( $trial ) { |
| 55 |
delete_post_meta( $subscription_id, '_subscrpt_trial' ); |
| 56 |
delete_post_meta( $subscription_id, '_subscrpt_trial_mode' ); |
| 57 |
delete_post_meta( $subscription_id, '_subscrpt_trial_started' ); |
| 58 |
delete_post_meta( $subscription_id, '_subscrpt_trial_ended' ); |
| 59 |
} |
| 60 |
$next_date = sdevs_wp_strtotime( 1 . ' ' . $type, time() ); |
| 61 |
} elseif ( 'early-renew' === $subscription_history->type ) { |
| 62 |
$next_date = sdevs_wp_strtotime( 1 . ' ' . $type, get_post_meta( $subscription_id, '_subscrpt_next_date', true ) ); |
| 63 |
|
| 64 |
if ( $trial ) { |
| 65 |
$trial_mode = get_post_meta( $subscription_id, '_subscrpt_trial_mode', true ); |
| 66 |
if ( 'on' === $trial_mode ) { |
| 67 |
update_post_meta( $subscription_id, '_subscrpt_trial_mode', 'extended' ); |
| 68 |
$next_date = sdevs_wp_strtotime( 1 . ' ' . $type, get_post_meta( $subscription_id, '_subscrpt_trial_ended', true ) ); |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
update_post_meta( $subscription_id, '_subscrpt_next_date', $next_date ); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Add custom column on order item. |
| 77 |
* |
| 78 |
* @return void |
| 79 |
*/ |
| 80 |
public function register_custom_column() { |
| 81 |
?> |
| 82 |
<th class="item_recurring sortable" data-sort="float"><?php esc_html_e( 'Recurring', 'sdevs_subscrpt' ); ?></th> |
| 83 |
<?php |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Display data for custom column. |
| 88 |
* |
| 89 |
* @param \WC_Product $product Product Object. |
| 90 |
* @param \WC_Order_Item $item Order Item. |
| 91 |
* |
| 92 |
* @return void |
| 93 |
*/ |
| 94 |
public function add_column_value( $product, $item ) { |
| 95 |
if ( ! method_exists( $item, 'get_id' ) || ! method_exists( $item, 'get_subtotal' ) ) { |
| 96 |
return; |
| 97 |
} |
| 98 |
|
| 99 |
$subtotal = '-'; |
| 100 |
$item_id = $item->get_id(); |
| 101 |
$subscription_id = Helper::get_subscription_from_order_item_id( $item->get_id() )->subscription_id; |
| 102 |
$price = get_post_meta( $subscription_id, '_subscrpt_price', true ); |
| 103 |
$subtotal = Helper::format_price_with_order_item( $price, $item_id ); |
| 104 |
?> |
| 105 |
<td class="item_recurring" width="15%"> |
| 106 |
<div class="view"> |
| 107 |
<?php echo wp_kses_post( $subtotal ); ?> |
| 108 |
</div> |
| 109 |
</td> |
| 110 |
<?php |
| 111 |
} |
| 112 |
|
| 113 |
public function add_order_item_data( $item_id, $item, $product ) { |
| 114 |
if ( ! $product ) { |
| 115 |
return; |
| 116 |
} |
| 117 |
|
| 118 |
$item_meta = wc_get_order_item_meta( $item_id, '_subscrpt_meta', true ); |
| 119 |
|
| 120 |
if ( ! $item_meta || ! is_array( $item_meta ) ) { |
| 121 |
return false; |
| 122 |
} |
| 123 |
|
| 124 |
$trial = $item_meta['trial']; |
| 125 |
$has_trial = isset( $item_meta['trial'] ) && strlen( $item_meta['trial'] ) > 2; |
| 126 |
|
| 127 |
if ( $has_trial ) { |
| 128 |
echo '<br/><small> + Got ' . $trial . ' free trial!</small>'; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Take some actions based on order status changed. |
| 134 |
* |
| 135 |
* @param int $order_id Order Id. |
| 136 |
*/ |
| 137 |
public function order_status_changed( $order_id ) { |
| 138 |
$order = wc_get_order( $order_id ); |
| 139 |
$post_status = 'active'; |
| 140 |
|
| 141 |
switch ( $order->get_status() ) { |
| 142 |
case 'on-hold': |
| 143 |
case 'pending': |
| 144 |
$post_status = 'pending'; |
| 145 |
break; |
| 146 |
|
| 147 |
case 'refunded': |
| 148 |
case 'failed': |
| 149 |
case 'cancelled': |
| 150 |
$post_status = 'cancelled'; |
| 151 |
break; |
| 152 |
|
| 153 |
default: |
| 154 |
$post_status = 'active'; |
| 155 |
break; |
| 156 |
} |
| 157 |
$post_status = apply_filters( 'subscript_order_status_to_post_status', $post_status, $order ); |
| 158 |
|
| 159 |
global $wpdb; |
| 160 |
$table_name = $wpdb->prefix . 'subscrpt_order_relation'; |
| 161 |
// @phpcs:ignore |
| 162 |
$histories = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM %i WHERE order_id=%d', array( $table_name, $order_id ) ) ); |
| 163 |
|
| 164 |
foreach ( $histories as $history ) { |
| 165 |
if ( 'new' === $history->type || 'renew' === $history->type ) { |
| 166 |
wp_update_post( |
| 167 |
array( |
| 168 |
'ID' => $history->subscription_id, |
| 169 |
'post_status' => $post_status, |
| 170 |
) |
| 171 |
); |
| 172 |
|
| 173 |
Action::write_comment( $post_status, $history->subscription_id ); |
| 174 |
} else { |
| 175 |
do_action( 'subscrpt_order_status_changed', $order, $history ); |
| 176 |
} |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Delete the subscription. |
| 182 |
* |
| 183 |
* @param int $order_id Order ID. |
| 184 |
* |
| 185 |
* @return void |
| 186 |
*/ |
| 187 |
public function delete_the_subscription( $order_id ) { |
| 188 |
global $wpdb; |
| 189 |
$table_name = $wpdb->prefix . 'subscrpt_order_relation'; |
| 190 |
|
| 191 |
$histories = Helper::get_subscriptions_from_order( $order_id ); |
| 192 |
foreach ( (array) $histories as $history ) { |
| 193 |
$subscription_order_id = get_post_meta( $history->subscription_id, '_subscrpt_order_id', true ); |
| 194 |
if ( (int) $subscription_order_id === $order_id ) { |
| 195 |
wp_delete_post( $history->subscription_id, true ); |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
// phpcs:ignore |
| 200 |
$wpdb->delete( $table_name, array( 'order_id' => $order_id ), array( '%d' ) ); |
| 201 |
} |
| 202 |
} |
| 203 |
|