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.2 3.5.0 3.6.0 3.6.1 3.6.2 3.6.3 3.6.4 3.7.0 3.7.1 3.7.2 3.7.3 3.7.4 3.8.0 3.8.1 3.8.2 3.8.3 3.9.0 3.9.1 3.9.10 3.9.2 3.9.3 3.9.4 3.9.5 3.9.6 3.9.7 3.9.8 3.9.9
tutor / includes / ecommerce-functions.php
tutor / includes Last commit date
droip 1 week ago kirki 1 week ago theme-compatibility 3 days ago country.php 3 days ago ecommerce-functions.php 3 days ago tinymce_translate.php 3 days ago translate-text.php 3 days ago tutor-general-functions.php 3 days ago tutor-template-functions.php 3 days ago tutor-template-hook.php 4 years ago
ecommerce-functions.php
180 lines
1 <?php
2 /**
3 * Tutor ecommerce functions
4 *
5 * @package TutorFunctions
6 * @author Themeum <support@themeum.com>
7 * @link https://themeum.com
8 * @since 3.5.0
9 */
10
11 defined( 'ABSPATH' ) || exit;
12
13 use Tutor\Ecommerce\Cart\CartFactory;
14 use TutorPro\Ecommerce\GuestCheckout\GuestCheckout;
15
16 if ( ! function_exists( 'tutor_add_to_cart' ) ) {
17 /**
18 * Handle add to cart functionalities
19 *
20 * @since 3.5.0
21 *
22 * @param int $item_id Item id.
23 *
24 * @return object {success, message, data: {cart_url, items, total_count} }
25 */
26 function tutor_add_to_cart( int $item_id ) {
27 $response = new stdClass();
28 $response->success = true;
29 $response->message = __( 'Course added to cart', 'tutor' );
30 $response->data = null;
31
32 $user_id = get_current_user_id();
33 $is_guest_checkout_enabled = tutor_is_guest_checkout_enabled();
34
35 if ( ! $user_id && ! $is_guest_checkout_enabled ) {
36 return array(
37 'success' => false,
38 'message' => __( 'Guest checkout is not enabled', 'tutor' ),
39 'data' => tutor_utils()->tutor_dashboard_url(),
40 'redirect' => true,
41 );
42 }
43
44 try {
45 $cart = tutor_get_cart_object();
46 if ( $cart->add( $item_id ) ) {
47 // Prepare data.
48 $cart_url = $cart->get_cart_url();
49 $items = $cart->get_cart_items();
50 $data = (object) array(
51 'cart_url' => $cart_url,
52 'items' => $items,
53 'total_count' => count( $items ),
54 );
55
56 $response->data = $data;
57 } else {
58 $response->success = false;
59 $response->message = $cart->get_error();
60 }
61 } catch ( \Throwable $th ) {
62 $response->success = false;
63 $response->message = $th->getMessage();
64 }
65
66 return $response;
67 }
68 }
69
70 if ( ! function_exists( 'tutor_get_cart_url' ) ) {
71 /**
72 * Get the cart page URL
73 *
74 * @since 3.5.0
75 *
76 * @return string
77 */
78 function tutor_get_cart_url() {
79 try {
80 $cart = tutor_get_cart_object();
81 return $cart->get_cart_url();
82 } catch ( \Throwable $th ) {
83 return $th->getMessage();
84 }
85 }
86 }
87
88 if ( ! function_exists( 'tutor_get_cart_items' ) ) {
89 /**
90 * Get cart items
91 *
92 * @since 3.5.0
93 *
94 * @return array
95 */
96 function tutor_get_cart_items() {
97 $items = array();
98 try {
99 $cart = tutor_get_cart_object();
100 $items = $cart->get_cart_items();
101 } catch ( \Throwable $th ) {
102 error_log( $th->getMessage() );
103 }
104
105 return $items;
106 }
107 }
108
109 if ( ! function_exists( 'tutor_is_item_in_cart' ) ) {
110 /**
111 * Get cart items
112 *
113 * @since 3.5.0
114 *
115 * @param int $item_id Item id to check.
116 *
117 * @return bool
118 */
119 function tutor_is_item_in_cart( int $item_id ) {
120 try {
121 return tutor_get_cart_object()->is_item_exists( $item_id );
122 } catch ( \Throwable $th ) {
123 return false;
124 }
125 }
126 }
127
128 if ( ! function_exists( 'tutor_remove_cart_item' ) ) {
129 /**
130 * Get cart items
131 *
132 * @since 3.7.2
133 *
134 * @param int $item_id Item id to check.
135 *
136 * @return bool
137 */
138 function tutor_remove_cart_item( int $item_id ) {
139 return tutor_get_cart_object()->remove( $item_id );
140 }
141 }
142
143 if ( ! function_exists( 'tutor_get_cart_object' ) ) {
144 /**
145 * Get cart items
146 *
147 * @since 3.5.0
148 *
149 * @throws \Throwable If cart object creation failed.
150 *
151 * @return object CartInterface
152 */
153 function tutor_get_cart_object() {
154 $monetization = tutor_utils()->get_option( 'monetize_by' );
155 try {
156 return CartFactory::create_cart( $monetization );
157 } catch ( \Throwable $th ) {
158 throw $th;
159 }
160 }
161 }
162
163 if ( ! function_exists( 'tutor_is_guest_checkout_enabled' ) ) {
164 /**
165 * Get cart items
166 *
167 * @since 3.7.2
168 *
169 * @return bool
170 */
171 function tutor_is_guest_checkout_enabled() {
172 $monetization = tutor_utils()->get_option( 'monetize_by' );
173 if ( tutor_utils()->is_monetize_by_tutor() ) {
174 return function_exists( 'tutor_pro' ) && GuestCheckout::is_enable();
175 } elseif ( 'wc' === $monetization ) {
176 return tutor_utils()->get_option( 'enable_guest_course_cart', false );
177 }
178 }
179 }
180