PluginProbe
Subscriptions for WooCommerce with Stripe Recurring Payments / 1.4.1
Subscriptions for WooCommerce with Stripe Recurring Payments v1.4.1
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.4.1, at includes/Frontend/Checkout.php

156 lines 4.8 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 = sdevs_get_subscription_product( $order_item['product_id'] );
60
61 if ( $product->is_type( 'simple' ) && ! subscrpt_pro_activated() ) {
62
63 if ( $product->is_enabled() ) {
64 $is_renew = isset( $order_item['renew_subscrpt'] );
65
66 $timing_option = $product->get_timing_option();
67 $trial = $product->get_trial();
68
69 wc_update_order_item_meta(
70 $order_item->get_id(),
71 '_subscrpt_meta',
72 array(
73 'time' => 1,
74 'type' => $timing_option,
75 'trial' => $trial,
76 )
77 );
78
79 // Renew subscription if need!
80 $renew_subscription_id = Helper::subscription_exists( $product->get_id(), 'expired' );
81 $selected_subscription_id = null;
82 if ( $is_renew && $renew_subscription_id && 'cancelled' !== $post_status ) {
83 $selected_subscription_id = $renew_subscription_id;
84 Helper::process_order_renewal(
85 $selected_subscription_id,
86 $order_id,
87 $order_item->get_id()
88 );
89 } else {
90 $selected_subscription_id = Helper::process_new_subscription_order( $order_item, $post_status, $product );
91 }
92
93 if ( $selected_subscription_id ) {
94 // product related.
95 update_post_meta( $selected_subscription_id, '_subscrpt_timing_option', $timing_option );
96 update_post_meta( $selected_subscription_id, '_subscrpt_price', $product->get_price() * $order_item['quantity'] );
97 update_post_meta( $selected_subscription_id, '_subscrpt_user_cancel', $product->get_meta( '_subscrpt_user_cancel' ) );
98
99 // order related.
100 update_post_meta( $selected_subscription_id, '_subscrpt_order_id', $order_id );
101 update_post_meta( $selected_subscription_id, '_subscrpt_order_item_id', $order_item->get_id() );
102
103 // subscription related.
104 update_post_meta( $selected_subscription_id, '_subscrpt_trial', $trial );
105
106 do_action( 'subscrpt_order_checkout', $selected_subscription_id, $order_item );
107 }
108 }
109 }
110
111 do_action( 'subscrpt_product_checkout', $order_item, $product, $post_status );
112 }
113 }
114
115 /**
116 * Remove subscriptions for resumed orders.
117 *
118 * @param int $order_id Order id.
119 *
120 * @return void
121 */
122 public function remove_subscriptions( $order_id ) {
123 global $wpdb;
124 // delete subscriptions & order item meta.
125 $histories = Helper::get_subscriptions_from_order( $order_id );
126 foreach ( $histories as $history ) {
127 $table_name = $wpdb->prefix . 'subscrpt_order_relation';
128 // @phpcs:ignore
129 $relation_count = $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM %i WHERE subscription_id=%d', array( $table_name, $history->subscription_id ) ) );
130 if ( 1 === (int) $relation_count ) {
131 wp_delete_post( $history->subscription_id, true );
132 }
133 wc_delete_order_item_meta( $history->order_item_id, '_subscrpt_meta' );
134 }
135
136 // delete order subscription relation.
137 global $wpdb;
138 $table_name = $wpdb->prefix . 'subscrpt_order_relation';
139 // phpcs:ignore
140 $wpdb->delete( $table_name, array( 'order_id' => $order_id ), array( '%d' ) );
141 }
142
143 /**
144 * Save renew meta
145 *
146 * @param object $item Item.
147 * @param string $cart_item_key Cart Item Key.
148 * @param array $cart_item Cart Item.
149 */
150 public function save_order_item_product_meta( $item, $cart_item_key, $cart_item ) {
151 if ( isset( $cart_item['renew_subscrpt'] ) ) {
152 $item->update_meta_data( '_renew_subscrpt', $cart_item['renew_subscrpt'] );
153 }
154 }
155 }
156