| 1 |
<?php |
| 2 |
/** |
| 3 |
* Checkout handlers for subscription plugin. |
| 4 |
* |
| 5 |
* @package wp_subscription |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace SpringDevs\Subscription\Frontend; |
| 9 |
|
| 10 |
use SpringDevs\Subscription\Illuminate\Helper; |
| 11 |
use SpringDevs\Subscription\Illuminate\Subscription\Subscription; |
| 12 |
|
| 13 |
/** |
| 14 |
* Checkout class |
| 15 |
*/ |
| 16 |
class Checkout { |
| 17 |
|
| 18 |
/** |
| 19 |
* Initialize the class |
| 20 |
*/ |
| 21 |
public function __construct() { |
| 22 |
// Subscription upgrade/downgrade order created hook. |
| 23 |
add_action( 'woocommerce_checkout_order_processed', [ $this, 'trigger_subscription_switch_order_created' ] ); |
| 24 |
add_action( 'woocommerce_store_api_checkout_order_processed', [ $this, 'trigger_subscription_switch_order_created_storeapi' ] ); |
| 25 |
|
| 26 |
add_action( 'woocommerce_checkout_order_processed', [ $this, 'create_subscription_after_checkout' ] ); |
| 27 |
add_action( 'woocommerce_store_api_checkout_order_processed', [ $this, 'create_subscription_after_checkout_storeapi' ] ); |
| 28 |
add_action( 'woocommerce_resume_order', [ $this, 'remove_subscriptions' ] ); |
| 29 |
add_action( 'woocommerce_checkout_create_order_line_item', [ $this, 'save_order_item_product_meta' ], 10, 3 ); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Create subscription during checkout on storeAPI. |
| 34 |
* |
| 35 |
* @param \WC_Order $order Order Object. |
| 36 |
*/ |
| 37 |
public function create_subscription_after_checkout_storeapi( $order ) { |
| 38 |
$this->create_subscription_after_checkout( $order->get_id() ); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Create subscription during checkout. |
| 43 |
* |
| 44 |
* @param int $order_id Order ID. |
| 45 |
*/ |
| 46 |
public function create_subscription_after_checkout( $order_id ) { |
| 47 |
$order = wc_get_order( $order_id ); |
| 48 |
|
| 49 |
// Grab the post status based on order status. |
| 50 |
$post_status = 'active'; |
| 51 |
switch ( $order->get_status() ) { |
| 52 |
case 'on-hold': |
| 53 |
case 'pending': |
| 54 |
$post_status = 'pending'; |
| 55 |
break; |
| 56 |
|
| 57 |
case 'failed': |
| 58 |
case 'cancelled': |
| 59 |
$post_status = 'cancelled'; |
| 60 |
break; |
| 61 |
|
| 62 |
default: |
| 63 |
break; |
| 64 |
} |
| 65 |
|
| 66 |
// Create subscription for order items. |
| 67 |
$order_items = $order->get_items(); |
| 68 |
foreach ( $order_items as $order_item ) { |
| 69 |
$product = Subscription::get_subs_product( $order_item['product_id'] ); |
| 70 |
|
| 71 |
if ( $product->is_type( 'simple' ) && ! subscrpt_pro_activated() ) { |
| 72 |
if ( $product->is_enabled() ) { |
| 73 |
$is_renew = isset( $order_item['renew_subscrpt'] ); |
| 74 |
|
| 75 |
$timing_option = $product->get_timing_option(); |
| 76 |
$trial = $product->get_trial(); |
| 77 |
|
| 78 |
wc_update_order_item_meta( |
| 79 |
$order_item->get_id(), |
| 80 |
'_subscrpt_meta', |
| 81 |
array( |
| 82 |
'time' => 1, |
| 83 |
'type' => $timing_option, |
| 84 |
'trial' => $trial, |
| 85 |
) |
| 86 |
); |
| 87 |
|
| 88 |
// Renew subscription if need! |
| 89 |
$renew_subscription_id = Helper::subscription_exists( $product->get_id(), 'expired' ); |
| 90 |
$selected_subscription_id = null; |
| 91 |
if ( $is_renew && $renew_subscription_id && 'cancelled' !== $post_status ) { |
| 92 |
$selected_subscription_id = $renew_subscription_id; |
| 93 |
Helper::process_order_renewal( $selected_subscription_id, $order_id, $order_item->get_id() ); |
| 94 |
} else { |
| 95 |
$selected_subscription_id = Helper::process_new_subscription_order( $order_item, $post_status, $product ); |
| 96 |
} |
| 97 |
|
| 98 |
if ( $selected_subscription_id ) { |
| 99 |
// product related. |
| 100 |
update_post_meta( $selected_subscription_id, '_subscrpt_timing_option', $timing_option ); |
| 101 |
update_post_meta( $selected_subscription_id, '_subscrpt_price', $product->get_price() ); |
| 102 |
update_post_meta( $selected_subscription_id, '_subscrpt_user_cancel', $product->get_meta( '_subscrpt_user_cancel' ) ); |
| 103 |
|
| 104 |
// order related. |
| 105 |
update_post_meta( $selected_subscription_id, '_subscrpt_order_id', $order_id ); |
| 106 |
update_post_meta( $selected_subscription_id, '_subscrpt_order_item_id', $order_item->get_id() ); |
| 107 |
|
| 108 |
// subscription related. |
| 109 |
update_post_meta( $selected_subscription_id, '_subscrpt_trial', $trial ); |
| 110 |
|
| 111 |
do_action( 'subscrpt_order_checkout', $selected_subscription_id, $order_item ); |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
do_action( 'subscrpt_product_checkout', $order_item, $product, $post_status ); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Remove subscriptions for resumed orders. |
| 122 |
* |
| 123 |
* @param int $order_id Order id. |
| 124 |
* |
| 125 |
* @return void |
| 126 |
*/ |
| 127 |
public function remove_subscriptions( $order_id ) { |
| 128 |
global $wpdb; |
| 129 |
// delete subscriptions & order item meta. |
| 130 |
$histories = Helper::get_subscriptions_from_order( $order_id ); |
| 131 |
foreach ( $histories as $history ) { |
| 132 |
$table_name = $wpdb->prefix . 'subscrpt_order_relation'; |
| 133 |
// @phpcs:ignore |
| 134 |
$relation_count = $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM %i WHERE subscription_id=%d', array( $table_name, $history->subscription_id ) ) ); |
| 135 |
if ( 1 === (int) $relation_count ) { |
| 136 |
wp_delete_post( $history->subscription_id, true ); |
| 137 |
} |
| 138 |
wc_delete_order_item_meta( $history->order_item_id, '_subscrpt_meta' ); |
| 139 |
} |
| 140 |
|
| 141 |
// delete order subscription relation. |
| 142 |
global $wpdb; |
| 143 |
$table_name = $wpdb->prefix . 'subscrpt_order_relation'; |
| 144 |
// phpcs:ignore |
| 145 |
$wpdb->delete( $table_name, array( 'order_id' => $order_id ), array( '%d' ) ); |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Save renew meta |
| 150 |
* |
| 151 |
* @param object $item Item. |
| 152 |
* @param string $cart_item_key Cart Item Key. |
| 153 |
* @param array $cart_item Cart Item. |
| 154 |
*/ |
| 155 |
public function save_order_item_product_meta( $item, $cart_item_key, $cart_item ) { |
| 156 |
if ( isset( $cart_item['renew_subscrpt'] ) ) { |
| 157 |
$item->update_meta_data( '_renew_subscrpt', $cart_item['renew_subscrpt'] ); |
| 158 |
} |
| 159 |
|
| 160 |
if ( ! empty( $cart_item['wp_subs_switch'] ?? null ) && ! empty( $cart_item['switch_context'] ?? null ) ) { |
| 161 |
$switch_context = $cart_item['switch_context']; |
| 162 |
|
| 163 |
// Add switch context data to order item meta. |
| 164 |
$item->update_meta_data( '_wp_subs_switch', true, true ); |
| 165 |
$item->update_meta_data( '_wp_subs_switch_context', $switch_context, true ); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Trigger subscription switch order created store API. |
| 171 |
* |
| 172 |
* @param \WC_Order $order Order. |
| 173 |
*/ |
| 174 |
public function trigger_subscription_switch_order_created_storeapi( $order ) { |
| 175 |
$this->trigger_subscription_switch_order_created( $order->get_id() ?? 0 ); |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Trigger subscription switch order created. |
| 180 |
* |
| 181 |
* @param int $order_id Order ID. |
| 182 |
*/ |
| 183 |
public function trigger_subscription_switch_order_created( $order_id ) { |
| 184 |
if ( ! $order_id || ! subscrpt_pro_activated() ) { |
| 185 |
return; |
| 186 |
} |
| 187 |
|
| 188 |
$order = wc_get_order( $order_id ); |
| 189 |
$order_items = $order->get_items(); |
| 190 |
|
| 191 |
foreach ( $order_items as $order_item ) { |
| 192 |
$is_switch = in_array( $order_item->get_meta( '_wp_subs_switch' ), [ true, 1, '1' ], true ); |
| 193 |
$switch_context = $order_item->get_meta( '_wp_subs_switch_context' ); |
| 194 |
|
| 195 |
if ( $is_switch ) { |
| 196 |
$switch_type = $switch_context['switch_type'] ?? 'upgrade'; |
| 197 |
|
| 198 |
unset( $switch_context['nonce'] ); |
| 199 |
unset( $switch_context['redirect_back_url'] ); |
| 200 |
|
| 201 |
/** |
| 202 |
* Action fired when subscription switch order is created. |
| 203 |
* |
| 204 |
* @param string $switch_type Switch type (upgrade/downgrade). |
| 205 |
* @param \WC_Order $order Order object. |
| 206 |
* @param \WC_Order_Item_Product $order_item Order Item object. |
| 207 |
* @param array $switch_context Switch context data. [ 'switch_type', 'subscription_id', 'order_id', 'product_id', 'old_variation_id', 'new_variation_id' ] |
| 208 |
*/ |
| 209 |
do_action( 'subscrpt_switch_order_created', $switch_type, $order, $order_item, $switch_context ); |
| 210 |
} |
| 211 |
} |
| 212 |
} |
| 213 |
} |
| 214 |
|