Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/tutor-pro/gift-course/WooGiftProceeder.php
Keine Baseline-Datei – Diff nur gegen leer.
1
-
1
+
<?php
2
+
/**
3
+
* Concrete class for adding item in to cart when
4
+
* monetization is woocommerce
5
+
*
6
+
* @package Tutor\Ecommerce
7
+
* @author Themeum
8
+
* @link https://themeum.com
9
+
* @since 3.8.0
10
+
*/
11
+
12
+
namespace TutorPro\GiftCourse;
13
+
14
+
use WC_Order;
15
+
use WC_Order_Item;
16
+
use WC_Order_Item_Product;
17
+
18
+
/**
19
+
* Handle proceed to checkout logics
20
+
*/
21
+
class WooGiftProceeder implements GiftProceeder {
22
+
23
+
/**
24
+
* Create gift order
25
+
*
26
+
* @since 3.8.0
27
+
*
28
+
* @throws \Exception If product not found for this course.
29
+
*
30
+
* @param object $gift_data Gift data object.
31
+
*
32
+
* @return string Order checkout url
33
+
*/
34
+
public function proceed_to_checkout( object $gift_data ):string {
35
+
$product_id = tutor_utils()->get_course_product_id( $gift_data->course_id );
36
+
if ( ! $product_id ) {
37
+
throw new \Exception( 'Product not found for this course' );
38
+
}
39
+
40
+
$product = wc_get_product( $product_id );
41
+
if ( ! $product ) {
42
+
throw new \Exception( 'Invalid product ID: ' . $product_id );
43
+
}
44
+
45
+
// Ensure WooCommerce cart is loaded.
46
+
if ( ! WC()->cart ) {
47
+
wc_load_cart();
48
+
}
49
+
50
+
// Add product to cart with gift meta.
51
+
$cart = WC()->cart;
52
+
$items = $cart->get_cart();
53
+
54
+
foreach ( $items as $item ) {
55
+
$cart_product_id = $item['product_id'];
56
+
$is_gift_in_cart = $item[ GiftCourse::GIFT_DATA_META ] ?? null;
57
+
if ( $product_id === $cart_product_id && $is_gift_in_cart ) {
58
+
throw new \Exception( 'Gift course already in cart' );
59
+
}
60
+
}
61
+
62
+
$cart->add_to_cart(
63
+
$product_id,
64
+
1,
65
+
0,
66
+
array(),
67
+
array( GiftCourse::GIFT_DATA_META => $gift_data )
68
+
);
69
+
70
+
return wc_get_checkout_url();
71
+
}
72
+
73
+
/**
74
+
* Check if the item is gift item
75
+
*
76
+
* @since 3.8.0
77
+
*
78
+
* @param object|int $item Order item id or item object.
79
+
*
80
+
* @return object|bool
81
+
*/
82
+
public function is_gift_item( $item ) {
83
+
if ( ! $item ) {
84
+
return false;
85
+
}
86
+
87
+
if ( is_int( $item ) ) {
88
+
$item = new WC_Order_Item( $item );
89
+
}
90
+
91
+
$meta = $item->get_meta( GiftCourse::GIFT_FLAG_META, true );
92
+
if ( $meta ) {
93
+
return $item;
94
+
} else {
95
+
return false;
96
+
}
97
+
}
98
+
99
+
/**
100
+
* Get gift data
101
+
*
102
+
* @since 3.8.0
103
+
*
104
+
* @param int|object $item Order item object or id.
105
+
*
106
+
* @return object|bool
107
+
*/
108
+
public function get_gift_data( $item ) {
109
+
if ( is_int( $item ) ) {
110
+
$item = new WC_Order_Item_Product( $item );
111
+
}
112
+
113
+
if ( ! $item ) {
114
+
return false;
115
+
}
116
+
117
+
if ( ! $this->is_gift_item( $item ) ) {
118
+
return false;
119
+
}
120
+
121
+
return $item->get_meta( GiftCourse::GIFT_DATA_META, true );
122
+
}
123
+
124
+
/**
125
+
* Get gift data
126
+
*
127
+
* @since 3.8.0
128
+
*
129
+
* @param int|object $item Item id or item object.
130
+
* @param string $meta_key Item meta key.
131
+
* @param string $meta_value Item meta value.
132
+
*
133
+
* @return object|bool
134
+
*/
135
+
public function update_item_meta( $item, $meta_key, $meta_value ) {
136
+
if ( ! $item ) {
137
+
return false;
138
+
}
139
+
140
+
if ( is_int( $item ) ) {
141
+
$item = new WC_Order_Item_Product( $item );
142
+
}
143
+
144
+
if ( ! $item ) {
145
+
return false;
146
+
}
147
+
148
+
$item->update_meta_data( $meta_key, $meta_value );
149
+
$item->save();
150
+
151
+
return true;
152
+
}
153
+
154
+
/**
155
+
* Get order
156
+
*
157
+
* @since 3.8.0
158
+
*
159
+
* @param int $order_id Order id.
160
+
*
161
+
* @return object|bool
162
+
*/
163
+
public function get_order( int $order_id ) {
164
+
$order = new WC_Order( $order_id );
165
+
if ( ! $order ) {
166
+
return false;
167
+
}
168
+
169
+
$status = $order->get_status( '' );
170
+
171
+
$order->order_status = $status;
172
+
173
+
return $order;
174
+
}
175
+
176
+
/**
177
+
* Get order items
178
+
*
179
+
* @since 3.8.0
180
+
*
181
+
* @param int $order_id Order id.
182
+
*
183
+
* @return object|bool
184
+
*/
185
+
public function get_order_items( int $order_id ) {
186
+
$order = new WC_Order( $order_id );
187
+
if ( ! $order ) {
188
+
return false;
189
+
}
190
+
191
+
$items = $order->get_items();
192
+
if ( ! $items ) {
193
+
return;
194
+
}
195
+
196
+
return $items;
197
+
}
198
+
199
+
/**
200
+
* Restrict refund for the gift course
201
+
*
202
+
* @since 3.8.0
203
+
*
204
+
* @throws \Exception If user try to refund gift item.
205
+
*
206
+
* @param WC_Order_Refund $refund Refund object.
207
+
* @param array $args Refund args.
208
+
*
209
+
* @return void
210
+
*/
211
+
public function restrict_refund( $refund, $args ) {
212
+
$order_id = $args['order_id'];
213
+
if ( ! $order_id ) {
214
+
return;
215
+
}
216
+
217
+
$order = $this->get_order( $order_id );
218
+
$items = $order->items;
219
+
if ( ! $items ) {
220
+
return;
221
+
}
222
+
223
+
foreach ( $items as $item ) {
224
+
if ( $this->is_gift_item( $item ) && $this->is_gift_received( $item ) ) {
225
+
throw new \Exception( __( 'Gift course cannot be refunded.', 'tutor-pro' ) );
226
+
}
227
+
}
228
+
}
229
+
230
+
/**
231
+
* Is gift received
232
+
*
233
+
* @since 3.8.0
234
+
*
235
+
* @param object $item WC_Order_Item_Product object.
236
+
*
237
+
* @return boolean
238
+
*/
239
+
public function is_gift_received( object $item ): bool {
240
+
$status = $item->get_meta( GiftCourse::GIFT_STATUS_META, true );
241
+
return GiftCourse::GIFT_STATUS_RECEIVED === $status;
242
+
}
243
+
244
+
/**
245
+
* Check if the cart item already added for gift.
246
+
*
247
+
* @since 3.8.0
248
+
*
249
+
* @param array $cart_data Cart data.
250
+
*
251
+
* @return bool
252
+
*/
253
+
public function is_gift_course_in_cart( array $cart_data ) {
254
+
return is_array( $cart_data ) && isset( $cart_data[ GiftCourse::GIFT_DATA_META ] );
255
+
}
256
+
257
+
}
258
+