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 |