PluginProbe
Subscriptions for WooCommerce with Stripe Recurring Payments / 1.3.0
Subscriptions for WooCommerce with Stripe Recurring Payments v1.3.0
2.0.0 1.11.2 1.11.1 1.11.0 1.10.9 1.10.8 1.10.7 1.10.6 1.10.5 1.10.4 1.10.3 1.10.2 1.10.1 1.10.0 1.9.6 1.9.5 trunk 1.3.0 1.3.1 1.3.2 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 All 61 releases
subscription / includes / Frontend / Checkout.php

Checkout.php in Subscriptions for WooCommerce with Stripe Recurring Payments 1.3.0, at includes/Frontend/Checkout.php

164 lines 5.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace SpringDevs\Subscription\Frontend;
4
5 use SpringDevs\Subscription\Illuminate\Helper;
6
7 /**
8 * Checkout class
9 */
10 class Checkout {
11
12 /**
13 * Initialize the class
14 */
15 public function __construct() {
16 add_action( 'woocommerce_checkout_order_processed', array( $this, 'create_subscription_after_checkout' ) );
17 add_action( 'woocommerce_store_api_checkout_order_processed', array( $this, 'create_subscription_after_checkout_storeapi' ) );
18 add_action( 'woocommerce_resume_order', array( $this, 'remove_subscriptions' ) );
19 add_action( 'woocommerce_checkout_create_order_line_item', array( $this, 'save_order_item_product_meta' ), 10, 3 );
20 }
21
22 /**
23 * Create subscription during checkout on storeAPI.
24 *
25 * @param \WC_Order $order Order Object.
26 */
27 public function create_subscription_after_checkout_storeapi( $order ) {
28 $this->create_subscription_after_checkout( $order->get_id() );
29 }
30
31 /**
32 * Create subscription during checkout.
33 *
34 * @param int $order_id Order ID.
35 */
36 public function create_subscription_after_checkout( $order_id ) {
37 $order = wc_get_order( $order_id );
38
39 // Grab the post status based on order status.
40 $post_status = 'active';
41 switch ( $order->get_status() ) {
42 case 'on-hold':
43 case 'pending':
44 $post_status = 'pending';
45 break;
46
47 case 'failed':
48 case 'cancelled':
49 $post_status = 'cancelled';
50 break;
51
52 default:
53 break;
54 }
55
56 // Create subscription for order items.
57 $order_items = $order->get_items();
58 foreach ( $order_items as $order_item ) {
59 $product = wc_get_product( $order_item['product_id'] );
60
61 if ( $product->is_type( 'simple' ) && ! subscrpt_pro_activated() ) {
62 $enabled = $product->get_meta( '_subscrpt_enabled' );
63
64 if ( $enabled ) {
65 $is_renew = isset( $order_item['renew_subscrpt'] );
66
67 $timing_option = $product->get_meta( '_subscrpt_timing_option' );
68 $trial = null;
69 $has_trial = Helper::check_trial( $product->get_id() );
70
71 $trial_per = $product->get_meta( '_subscrpt_trial_timing_per' );
72 $trial_option = $product->get_meta( '_subscrpt_trial_timing_option' );
73 if ( ! empty( $trial_per ) && $trial_per > 0 && ! $is_renew && $has_trial ) {
74 $trial = $trial_per . ' ' . Helper::get_typos( $trial_per, $trial_option );
75 }
76
77 wc_update_order_item_meta(
78 $order_item->get_id(),
79 '_subscrpt_meta',
80 array(
81 'time' => 1,
82 'type' => $timing_option,
83 'trial' => $trial,
84 )
85 );
86
87 // Renew subscription if need!
88 $renew_subscription_id = Helper::subscription_exists( $product->get_id(), 'expired' );
89 $selected_subscription_id = null;
90 if ( $is_renew && $renew_subscription_id && 'cancelled' !== $post_status ) {
91 $selected_subscription_id = $renew_subscription_id;
92 Helper::process_order_renewal(
93 $selected_subscription_id,
94 $order_id,
95 $order_item->get_id()
96 );
97 } else {
98 $selected_subscription_id = Helper::process_new_subscription_order( $order_item, $post_status, $product );
99 }
100
101 if ( $selected_subscription_id ) {
102 // product related.
103 update_post_meta( $selected_subscription_id, '_subscrpt_timing_option', $timing_option );
104 update_post_meta( $selected_subscription_id, '_subscrpt_price', $product->get_price() * $order_item['quantity'] );
105 update_post_meta( $selected_subscription_id, '_subscrpt_user_cancel', $product->get_meta( '_subscrpt_user_cancel' ) );
106
107 // order related.
108 update_post_meta( $selected_subscription_id, '_subscrpt_order_id', $order_id );
109 update_post_meta( $selected_subscription_id, '_subscrpt_order_item_id', $order_item->get_id() );
110
111 // subscription related.
112 update_post_meta( $selected_subscription_id, '_subscrpt_trial', $trial );
113
114 do_action( 'subscrpt_order_checkout', $selected_subscription_id, $order_item );
115 }
116 }
117 }
118
119 do_action( 'subscrpt_product_checkout', $order_item, $product, $post_status );
120 }
121 }
122
123 /**
124 * Remove subscriptions for resumed orders.
125 *
126 * @param int $order_id Order id.
127 *
128 * @return void
129 */
130 public function remove_subscriptions( $order_id ) {
131 global $wpdb;
132 // delete subscriptions & order item meta.
133 $histories = Helper::get_subscriptions_from_order( $order_id );
134 foreach ( $histories as $history ) {
135 $table_name = $wpdb->prefix . 'subscrpt_order_relation';
136 // @phpcs:ignore
137 $relation_count = $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM %i WHERE subscription_id=%d', array( $table_name, $history->subscription_id ) ) );
138 if ( 1 === (int) $relation_count ) {
139 wp_delete_post( $history->subscription_id, true );
140 }
141 wc_delete_order_item_meta( $history->order_item_id, '_subscrpt_meta' );
142 }
143
144 // delete order subscription relation.
145 global $wpdb;
146 $table_name = $wpdb->prefix . 'subscrpt_order_relation';
147 // phpcs:ignore
148 $wpdb->delete( $table_name, array( 'order_id' => $order_id ), array( '%d' ) );
149 }
150
151 /**
152 * Save renew meta
153 *
154 * @param Object $item Item.
155 * @param String $cart_item_key Cart Item Key.
156 * @param Array $cart_item Cart Item.
157 */
158 public function save_order_item_product_meta( $item, $cart_item_key, $cart_item ) {
159 if ( isset( $cart_item['renew_subscrpt'] ) ) {
160 $item->update_meta_data( '_renew_subscrpt', $cart_item['renew_subscrpt'] );
161 }
162 }
163 }
164