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