| 1 |
<?php |
| 2 |
/** |
| 3 |
* Defines functions related to order |
| 4 |
* |
| 5 |
* @author ThimPress |
| 6 |
* @package LearnPress/Functions |
| 7 |
* @version 1.0 |
| 8 |
*/ |
| 9 |
|
| 10 |
defined( 'ABSPATH' ) || exit; |
| 11 |
|
| 12 |
/** |
| 13 |
* Generate unique key for an order. |
| 14 |
* |
| 15 |
* @return mixed |
| 16 |
*/ |
| 17 |
function learn_press_generate_order_key() { |
| 18 |
return apply_filters( 'learn-press/order-key', strtoupper( uniqid( 'ORDER' ) ) ); |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* Return default order status when creating new. |
| 23 |
* |
| 24 |
* @param string $prefix . Optional |
| 25 |
* |
| 26 |
* @return string |
| 27 |
*/ |
| 28 |
function learn_press_default_order_status( $prefix = '' ) { |
| 29 |
return apply_filters( 'learn-press/default-order-status', $prefix . 'pending' ); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Create or update an order. |
| 34 |
* If an 'ID' is passed throught $order_data, then update the existing order |
| 35 |
* |
| 36 |
* @param array $order_data { |
| 37 |
* Array of options to add or update an order |
| 38 |
* |
| 39 |
* @type int ID The order ID |
| 40 |
* @type |
| 41 |
* } |
| 42 |
* |
| 43 |
* @return LP_Order|WP_Error |
| 44 |
* @editor tungnx |
| 45 |
* @modify 4.1.4 - comment - not use |
| 46 |
*/ |
| 47 |
/*function learn_press_create_order( $order_data ) { |
| 48 |
|
| 49 |
$order = new LP_Order(); |
| 50 |
$order->save(); |
| 51 |
|
| 52 |
return $order; |
| 53 |
}*/ |
| 54 |
|
| 55 |
/** |
| 56 |
* Create a new order or update an existing |
| 57 |
* |
| 58 |
* @param array $order_data |
| 59 |
* |
| 60 |
* @return LP_Order|WP_Error |
| 61 |
* |
| 62 |
* @throws Exception |
| 63 |
*/ |
| 64 |
|
| 65 |
/*function learn_press_update_order( $order_data ) { |
| 66 |
if ( empty( $order_data['order_id'] ) ) { |
| 67 |
throw new Exception( __( 'Invalid order ID when updating.', 'learnpress' ) ); |
| 68 |
} |
| 69 |
|
| 70 |
return learn_press_create_order( $order_data ); |
| 71 |
}*/ |
| 72 |
|
| 73 |
/**************************/ |
| 74 |
/** |
| 75 |
* Update Order status |
| 76 |
* |
| 77 |
* @param int |
| 78 |
* @param string |
| 79 |
* |
| 80 |
* @return bool |
| 81 |
*/ |
| 82 |
function learn_press_update_order_status( $order_id, $status = '' ) { |
| 83 |
$order = new LP_Order( $order_id ); |
| 84 |
if ( $order ) { |
| 85 |
return $order->update_status( $status ); |
| 86 |
} |
| 87 |
|
| 88 |
return false; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Add an order item into order. |
| 93 |
* |
| 94 |
* @param int $order_id |
| 95 |
* @param mixed $item - Array of item data or ID |
| 96 |
* |
| 97 |
* @return bool |
| 98 |
* @editor tungnx |
| 99 |
* @modify 4.1.4 - comment - not use |
| 100 |
*/ |
| 101 |
/*function learn_press_add_order_item( $order_id, $item ) { |
| 102 |
$item_id = false; |
| 103 |
$order = learn_press_get_order( $order_id ); |
| 104 |
|
| 105 |
if ( $order ) { |
| 106 |
$item_id = $order->add_item( $item ); |
| 107 |
} |
| 108 |
|
| 109 |
return $item_id; |
| 110 |
}*/ |
| 111 |
|
| 112 |
/** |
| 113 |
* Remove an order item by order_item_id. |
| 114 |
* |
| 115 |
* @param int $order_id |
| 116 |
* @param int $item_id |
| 117 |
* |
| 118 |
* @return bool |
| 119 |
*/ |
| 120 |
function learn_press_remove_order_item( $order_id, $item_id ) { |
| 121 |
$order = learn_press_get_order( $order_id ); |
| 122 |
|
| 123 |
if ( $order ) { |
| 124 |
return $order->remove_item( $item_id ); |
| 125 |
} |
| 126 |
|
| 127 |
return false; |
| 128 |
} |
| 129 |
|
| 130 |
function _learn_press_before_delete_order_item( $item_id, $order_id ) { |
| 131 |
global $wpdb; |
| 132 |
$order = learn_press_get_order_by_item_id( $item_id ); |
| 133 |
|
| 134 |
if ( $order ) { |
| 135 |
$course_id = learn_press_get_order_item_meta( $item_id, '_course_id' ); |
| 136 |
learn_press_delete_user_data( $order->user_id, $course_id ); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
// add_action( 'learn-press/before-delete-order-item', '_learn_press_before_delete_order_item', 10, 2 ); |
| 141 |
|
| 142 |
function _learn_press_ajax_add_order_item_meta( $item ) { |
| 143 |
$item_id = $item['id']; |
| 144 |
$order = learn_press_get_order_by_item_id( $item_id ); |
| 145 |
|
| 146 |
if ( $order ) { |
| 147 |
if ( $order->get_status() == 'completed' ) { |
| 148 |
learn_press_auto_enroll_user_to_courses( $order->id ); |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
add_action( 'learn_press_ajax_add_order_item_meta', '_learn_press_ajax_add_order_item_meta' ); |
| 154 |
|
| 155 |
function learn_press_get_order_by_item_id( $item_id ) { |
| 156 |
global $wpdb; |
| 157 |
|
| 158 |
$order_id = $wpdb->get_var( $wpdb->prepare( "SELECT order_id FROM {$wpdb->prefix}learnpress_order_items WHERE order_item_id = %d", $item_id ) ); |
| 159 |
$order = learn_press_get_order( $order_id ); |
| 160 |
|
| 161 |
if ( $order_id && $order ) { |
| 162 |
return $order; |
| 163 |
} |
| 164 |
|
| 165 |
return false; |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Add order item meta data. |
| 170 |
* |
| 171 |
* @param int $item_id |
| 172 |
* @param string $meta_key |
| 173 |
* @param mixed $meta_value |
| 174 |
* @param string $prev_value |
| 175 |
* |
| 176 |
* @return false|int |
| 177 |
*/ |
| 178 |
function learn_press_add_order_item_meta( $item_id, $meta_key, $meta_value, $prev_value = '' ) { |
| 179 |
return add_metadata( 'learnpress_order_item', $item_id, $meta_key, $meta_value, $prev_value ); |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Update order item meta data. |
| 184 |
* |
| 185 |
* @param int $item_id |
| 186 |
* @param string $meta_key |
| 187 |
* @param mixed $meta_value |
| 188 |
* @param string $prev_value |
| 189 |
* |
| 190 |
* @return bool|int |
| 191 |
*/ |
| 192 |
function learn_press_update_order_item_meta( $item_id, $meta_key, $meta_value, $prev_value = '' ) { |
| 193 |
return update_metadata( 'learnpress_order_item', $item_id, $meta_key, $meta_value, $prev_value ); |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Delete order item meta data. |
| 198 |
* |
| 199 |
* @param int $item_id |
| 200 |
* @param string $meta_key |
| 201 |
* @param mixed $meta_value |
| 202 |
* @param bool $delete_all |
| 203 |
* |
| 204 |
* @return bool |
| 205 |
*/ |
| 206 |
function learn_press_delete_order_item_meta( $item_id, $meta_key, $meta_value, $delete_all = false ) { |
| 207 |
return delete_metadata( 'learnpress_order_item', $item_id, $meta_key, $meta_value, $delete_all ); |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* Get order item meta data. |
| 212 |
* |
| 213 |
* @param int $item_id |
| 214 |
* @param string $meta_key |
| 215 |
* @param bool $single |
| 216 |
* |
| 217 |
* @return mixed |
| 218 |
*/ |
| 219 |
function learn_press_get_order_item_meta( $item_id, $meta_key, $single = true ) { |
| 220 |
return get_metadata( 'learnpress_order_item', $item_id, $meta_key, $single ); |
| 221 |
} |
| 222 |
|
| 223 |
/** |
| 224 |
* Get order |
| 225 |
* |
| 226 |
* @param mixed $the_order |
| 227 |
* |
| 228 |
* @return LP_Order|bool object instance |
| 229 |
*/ |
| 230 |
function learn_press_get_order( $the_order = false ) { |
| 231 |
global $post; |
| 232 |
$the_id = 0; |
| 233 |
if ( false === $the_order && is_a( $post, 'WP_Post' ) && LP_ORDER_CPT === get_post_type( $post ) ) { |
| 234 |
$the_id = $post->ID; |
| 235 |
} elseif ( is_numeric( $the_order ) ) { |
| 236 |
$the_id = $the_order; |
| 237 |
} elseif ( $the_order instanceof LP_Order ) { |
| 238 |
$the_id = $the_order->get_id(); |
| 239 |
} elseif ( ! empty( $the_order->ID ) ) { |
| 240 |
$the_id = $the_order->ID; |
| 241 |
} |
| 242 |
|
| 243 |
if ( LP_ORDER_CPT != get_post_type( $the_id ) ) { |
| 244 |
return false; |
| 245 |
} |
| 246 |
|
| 247 |
return new LP_Order( $the_id ); |
| 248 |
} |
| 249 |
|
| 250 |
/** |
| 251 |
* get confirm order URL |
| 252 |
* |
| 253 |
* @param int $order_id |
| 254 |
* |
| 255 |
* @return string |
| 256 |
*/ |
| 257 |
function learn_press_get_order_confirm_url( $order_id = 0 ) { |
| 258 |
$url = ''; |
| 259 |
$confirm_page_id = learn_press_get_page_id( 'taken_course_confirm' ); |
| 260 |
|
| 261 |
if ( $confirm_page_id && get_post( $confirm_page_id ) ) { |
| 262 |
$url = get_permalink( $confirm_page_id ); |
| 263 |
|
| 264 |
if ( $order_id ) { |
| 265 |
$url = join( preg_match( '!\?!', $url ) ? '&' : '?', array( $url, "order_id={$order_id}" ) ); |
| 266 |
} |
| 267 |
} else { |
| 268 |
$order = new LP_Order( $order_id ); |
| 269 |
$items = $order->get_items(); |
| 270 |
|
| 271 |
if ( $items && ! empty( $items->products ) ) { |
| 272 |
$course = reset( $items->products ); |
| 273 |
$url = get_permalink( $course['id'] ); |
| 274 |
} else { |
| 275 |
$url = get_home_url(); |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
return $url; |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
function learn_press_do_transaction( $method, $transaction = false ) { |
| 284 |
LP_Gateways::instance()->get_available_payment_gateways(); |
| 285 |
|
| 286 |
do_action( 'learn_press_do_transaction_' . $method, $transaction ); |
| 287 |
} |
| 288 |
|
| 289 |
function learn_press_set_transient_transaction( $method, $temp_id, $user_id, $transaction ) { |
| 290 |
set_transient( |
| 291 |
$method . '-' . $temp_id, |
| 292 |
array( |
| 293 |
'user_id' => $user_id, |
| 294 |
'transaction_object' => $transaction, |
| 295 |
), |
| 296 |
60 * 60 * 24 |
| 297 |
); |
| 298 |
} |
| 299 |
|
| 300 |
function learn_press_get_transient_transaction( $method, $temp_id ) { |
| 301 |
return get_transient( $method . '-' . $temp_id ); |
| 302 |
} |
| 303 |
|
| 304 |
function learn_press_delete_transient_transaction( $method, $temp_id ) { |
| 305 |
return delete_transient( $method . '-' . $temp_id ); |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
/** |
| 310 |
* Deprecated function |
| 311 |
* |
| 312 |
* @param array $args |
| 313 |
* |
| 314 |
* @return int |
| 315 |
*/ |
| 316 |
function learn_press_add_transaction( $args = null ) { |
| 317 |
// _deprecated_function( 'learn_press_add_transaction', '1.0', 'learn_press_add_order' ); |
| 318 |
return learn_press_add_order( $args ); |
| 319 |
} |
| 320 |
|
| 321 |
/** |
| 322 |
* @param null $args |
| 323 |
* |
| 324 |
* @return mixed |
| 325 |
*/ |
| 326 |
function learn_press_add_order( $args = null ) { |
| 327 |
// $method, $method_id, $status = 'Pending', $customer_id = false, $transaction_object = false, $args = array() |
| 328 |
$default_args = array( |
| 329 |
'method' => '', |
| 330 |
'method_id' => '', |
| 331 |
'status' => '', |
| 332 |
'user_id' => null, |
| 333 |
'order_id' => 0, |
| 334 |
'parent' => 0, |
| 335 |
'transaction_object' => false, |
| 336 |
); |
| 337 |
|
| 338 |
$args = wp_parse_args( $args, $default_args ); |
| 339 |
$order_data = array(); |
| 340 |
|
| 341 |
if ( $args['order_id'] > 0 && get_post( $args['order_id'] ) ) { |
| 342 |
$updating = true; |
| 343 |
$order_data['ID'] = $args['order_id']; |
| 344 |
} else { |
| 345 |
$updating = false; |
| 346 |
$order_data['post_type'] = LP_ORDER_CPT; |
| 347 |
$order_data['post_status'] = ! empty( $args['status'] ) ? 'publish' : 'lpr-draft'; |
| 348 |
$order_data['ping_status'] = 'closed'; |
| 349 |
$order_data['post_author'] = ( $order_owner_id = learn_press_cart_order_instructor() ) ? $order_owner_id : 1; // always is administrator |
| 350 |
$order_data['post_parent'] = absint( $args['parent'] ); |
| 351 |
} |
| 352 |
$order_title = array(); |
| 353 |
if ( $args['method'] ) { |
| 354 |
$order_title[] = $args['method']; |
| 355 |
} |
| 356 |
if ( $args['method_id'] ) { |
| 357 |
$order_title[] = $args['method_id']; |
| 358 |
} |
| 359 |
$order_title[] = date_i18n( 'Y-m-d-H:i:s' ); |
| 360 |
$order_data['post_title'] = join( '-', $order_title ); |
| 361 |
|
| 362 |
if ( empty( $args['user_id'] ) ) { |
| 363 |
$user = learn_press_get_current_user(); |
| 364 |
$args['user_id'] = $user->get_id(); |
| 365 |
} |
| 366 |
|
| 367 |
if ( ! $args['transaction_object'] ) { |
| 368 |
$args['transaction_object'] = learn_press_generate_transaction_object(); |
| 369 |
} |
| 370 |
|
| 371 |
if ( ! $updating ) { |
| 372 |
if ( wp_insert_post( $order_data ) ) { |
| 373 |
$transaction_id = wp_insert_post( $order_data ); |
| 374 |
|
| 375 |
update_post_meta( $transaction_id, '_learn_press_transaction_method', $args['method'] ); |
| 376 |
|
| 377 |
// update_post_meta( $transaction_id, '_learn_press_transaction_status', $status ); |
| 378 |
update_post_meta( $transaction_id, '_learn_press_customer_id', $args['user_id'] ); |
| 379 |
update_post_meta( $transaction_id, '_learn_press_customer_ip', learn_press_get_ip() ); |
| 380 |
update_post_meta( $transaction_id, '_learn_press_order_items', $args['transaction_object'] ); |
| 381 |
|
| 382 |
add_user_meta( $args['user_id'], '_lpr_order_id', $transaction_id ); |
| 383 |
|
| 384 |
} |
| 385 |
} else { |
| 386 |
$transaction_id = wp_update_post( $order_data ); |
| 387 |
} |
| 388 |
|
| 389 |
if ( $transaction_id ) { |
| 390 |
if ( ! empty( $args['status'] ) ) { |
| 391 |
learn_press_update_order_status( $transaction_id, $args['status'] ); |
| 392 |
} |
| 393 |
update_post_meta( $transaction_id, '_learn_press_transaction_method_id', $args['method_id'] ); |
| 394 |
|
| 395 |
if ( $args['transaction_object'] ) { |
| 396 |
update_post_meta( $transaction_id, '_learn_press_order_items', $args['transaction_object'] ); |
| 397 |
} |
| 398 |
|
| 399 |
if ( ! empty( $args['status'] ) ) { |
| 400 |
if ( $updating ) { |
| 401 |
return apply_filters( 'learn_press_update_transaction_success', $transaction_id, $args ); |
| 402 |
} else { |
| 403 |
return apply_filters( 'learn_press_add_transaction_success', $transaction_id, $args ); |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
return $transaction_id; |
| 408 |
} |
| 409 |
|
| 410 |
return false; |
| 411 |
|
| 412 |
// do_action( 'learn_press_add_transaction_fail', $args );// $method, $method_id, $status, $customer_id, $transaction_object, $args ); |
| 413 |
} |
| 414 |
|
| 415 |
function learn_press_payment_method_from_slug( $order_id ) { |
| 416 |
$slug = get_post_meta( $order_id, '_learn_press_transaction_method', true ); |
| 417 |
|
| 418 |
return apply_filters( 'learn_press_payment_method_from_slug_' . $slug, $slug ); |
| 419 |
} |
| 420 |
|
| 421 |
function learn_press_generate_transaction_object() { |
| 422 |
$cart = learn_press_get_cart(); |
| 423 |
$products = $cart->get_items(); |
| 424 |
|
| 425 |
if ( $products ) { |
| 426 |
foreach ( $products as $key => $product ) { |
| 427 |
$products[ $key ]['product_base_price'] = floatval( learn_press_get_course_price( $product['id'] ) ); |
| 428 |
$products[ $key ]['product_subtotal'] = floatval( learn_press_get_course_price( $product['id'] ) * $product['quantity'] ); |
| 429 |
$products[ $key ]['product_name'] = get_the_title( $product['id'] ); |
| 430 |
$products = apply_filters( 'learn_press_generate_transaction_object_products', $products, $key, $product ); |
| 431 |
} |
| 432 |
} |
| 433 |
|
| 434 |
$transaction_object = new stdClass(); |
| 435 |
$transaction_object->cart_id = $cart->get_cart_id(); |
| 436 |
$transaction_object->total = round( $cart->get_total(), 2 ); |
| 437 |
$transaction_object->sub_total = $cart->get_sub_total(); |
| 438 |
$transaction_object->currency = learn_press_get_currency(); |
| 439 |
$transaction_object->description = learn_press_get_cart_description(); |
| 440 |
$transaction_object->products = $products; |
| 441 |
$transaction_object->coupons = ''; |
| 442 |
$transaction_object->coupons_total_discount = ''; |
| 443 |
|
| 444 |
$transaction_object = apply_filters( 'learn_press_generate_transaction_object', $transaction_object ); |
| 445 |
|
| 446 |
return $transaction_object; |
| 447 |
} |
| 448 |
|
| 449 |
/** |
| 450 |
* Get the author ID of course in the cart |
| 451 |
* |
| 452 |
* Currently, it only get the first item in cart |
| 453 |
* |
| 454 |
* @return int |
| 455 |
*/ |
| 456 |
function learn_press_cart_order_instructor() { |
| 457 |
$cart = learn_press_get_cart(); |
| 458 |
$products = $cart->get_items(); |
| 459 |
|
| 460 |
if ( $products ) { |
| 461 |
foreach ( $products as $key => $product ) { |
| 462 |
$post = get_post( $product['id'] ); |
| 463 |
|
| 464 |
if ( $post && ! empty( $post->ID ) ) { |
| 465 |
return $post->post_author; |
| 466 |
} |
| 467 |
} |
| 468 |
} |
| 469 |
|
| 470 |
return 0; |
| 471 |
} |
| 472 |
|
| 473 |
function learn_press_get_orders( $args = array() ) { |
| 474 |
// _deprecated_function( __FUNCTION__, '3.0.0', 'get_posts' ); |
| 475 |
$args['post_type'] = LP_ORDER_CPT; |
| 476 |
$orders = get_posts( $args ); |
| 477 |
|
| 478 |
return apply_filters( 'learn_press_get_orders', $orders, $args ); |
| 479 |
} |
| 480 |
|
| 481 |
/** |
| 482 |
* Count orders by it's status |
| 483 |
* |
| 484 |
* @param array $args |
| 485 |
* |
| 486 |
* @return array |
| 487 |
*/ |
| 488 |
function learn_press_count_orders( $args = array() ) { |
| 489 |
if ( is_string( $args ) ) { |
| 490 |
$args = array( 'status' => $args ); |
| 491 |
} else { |
| 492 |
$args = wp_parse_args( |
| 493 |
$args, |
| 494 |
array( |
| 495 |
'status' => '', |
| 496 |
) |
| 497 |
); |
| 498 |
} |
| 499 |
global $wpdb; |
| 500 |
$statuses = $args['status']; |
| 501 |
|
| 502 |
if ( ! $statuses ) { |
| 503 |
$statuses = learn_press_get_register_order_statuses(); |
| 504 |
$statuses = array_keys( $statuses ); |
| 505 |
} |
| 506 |
|
| 507 |
settype( $statuses, 'array' ); |
| 508 |
$size_of_status = sizeof( $statuses ); |
| 509 |
|
| 510 |
foreach ( $statuses as $k => $status ) { |
| 511 |
$statuses[ $k ] = ! preg_match( '~^lp-~', $status ) ? 'lp-' . $status : $status; |
| 512 |
} |
| 513 |
|
| 514 |
$format = array_fill( 0, $size_of_status, '%s' ); |
| 515 |
$counts = array_fill_keys( $statuses, 0 ); |
| 516 |
$statuses[] = LP_ORDER_CPT; |
| 517 |
$query = $wpdb->prepare( |
| 518 |
" |
| 519 |
SELECT COUNT(ID) AS count, post_status AS status |
| 520 |
FROM {$wpdb->posts} o |
| 521 |
WHERE post_status IN(" . join( ',', $format ) . ') |
| 522 |
AND post_type = %s |
| 523 |
GROUP BY o.post_status |
| 524 |
', |
| 525 |
$statuses |
| 526 |
); |
| 527 |
|
| 528 |
$results = $wpdb->get_results( $query ); |
| 529 |
if ( $results ) { |
| 530 |
foreach ( $results as $result ) { |
| 531 |
if ( array_key_exists( $result->status, $counts ) ) { |
| 532 |
$counts[ $result->status ] = absint( $result->count ); |
| 533 |
} |
| 534 |
} |
| 535 |
} |
| 536 |
|
| 537 |
return $size_of_status > 1 ? $counts : reset( $counts ); |
| 538 |
} |
| 539 |
|
| 540 |
/** |
| 541 |
* Format price with currency and other settings. |
| 542 |
* |
| 543 |
* @param float $price |
| 544 |
* @param string $currency |
| 545 |
* |
| 546 |
* @return string |
| 547 |
*/ |
| 548 |
function learn_press_format_price( $price = 0, $currency = '' ): string { |
| 549 |
if ( ! is_numeric( $price ) ) { |
| 550 |
$price = 0; |
| 551 |
} |
| 552 |
|
| 553 |
$before = $after = ''; |
| 554 |
|
| 555 |
$currency = is_string( $currency ) && '' !== $currency ? $currency : learn_press_get_currency_symbol(); |
| 556 |
$thousands_separator = LP_Settings::get_option( 'thousands_separator', ',' ); |
| 557 |
$number_of_decimals = LP_Settings::get_option( 'number_of_decimals', 2 ); |
| 558 |
$decimals_separator = LP_Settings::get_option( 'decimals_separator', '.' ); |
| 559 |
|
| 560 |
switch ( LP_Settings::get_option( 'currency_pos' ) ) { |
| 561 |
default: |
| 562 |
$before = $currency; |
| 563 |
break; |
| 564 |
case 'left_with_space': |
| 565 |
$before = $currency . ' '; |
| 566 |
break; |
| 567 |
case 'right': |
| 568 |
$after = $currency; |
| 569 |
break; |
| 570 |
case 'right_with_space': |
| 571 |
$after = ' ' . $currency; |
| 572 |
} |
| 573 |
|
| 574 |
return $before . number_format( $price, $number_of_decimals, $decimals_separator, $thousands_separator ) . $after; |
| 575 |
} |
| 576 |
|
| 577 |
/** |
| 578 |
* Update |
| 579 |
* |
| 580 |
* @param $order_id |
| 581 |
* |
| 582 |
* @return array|bool |
| 583 |
*/ |
| 584 |
function learn_press_update_order_items( $order_id ) { |
| 585 |
$order = learn_press_get_order( $order_id ); |
| 586 |
if ( ! $order ) { |
| 587 |
return false; |
| 588 |
} |
| 589 |
|
| 590 |
$subtotal = 0; |
| 591 |
$total = 0; |
| 592 |
$items = $order->get_items(); |
| 593 |
|
| 594 |
if ( $items ) { |
| 595 |
foreach ( $items as $item ) { |
| 596 |
$subtotal += $item['subtotal']; |
| 597 |
$total += $item['total']; |
| 598 |
} |
| 599 |
} |
| 600 |
|
| 601 |
update_post_meta( $order_id, '_order_currency', learn_press_get_currency() ); |
| 602 |
update_post_meta( $order_id, '_prices_include_tax', 'no' ); |
| 603 |
update_post_meta( $order_id, '_order_subtotal', $subtotal ); |
| 604 |
update_post_meta( $order_id, '_order_total', $total ); |
| 605 |
update_post_meta( $order_id, '_order_key', learn_press_generate_order_key() ); |
| 606 |
update_post_meta( $order_id, '_payment_method', '' ); |
| 607 |
update_post_meta( $order_id, '_payment_method_title', '' ); |
| 608 |
update_post_meta( $order_id, '_order_version', '1.0' ); |
| 609 |
|
| 610 |
return array( |
| 611 |
'subtotal' => $subtotal, |
| 612 |
'total' => $total, |
| 613 |
'currency' => learn_press_get_currency(), |
| 614 |
); |
| 615 |
} |
| 616 |
|
| 617 |
/** |
| 618 |
* Format order's ID in ten numbers. Eg: 0000000XXX. |
| 619 |
* |
| 620 |
* @param int $order_number |
| 621 |
* |
| 622 |
* @since 2.0.0 |
| 623 |
* |
| 624 |
* @return string |
| 625 |
*/ |
| 626 |
function learn_press_transaction_order_number( $order_number ) { |
| 627 |
$formatted_number = apply_filters( 'learn_press_get_order_number', '#' . sprintf( "%'.010d", $order_number ), $order_number ); |
| 628 |
|
| 629 |
return apply_filters( 'learn-press/order-number-formatted', $formatted_number, $order_number ); |
| 630 |
} |
| 631 |
|
| 632 |
/** |
| 633 |
* Get label of an order's statues. |
| 634 |
* |
| 635 |
* @param int|string $order_id - Optional. ID of an order or status. |
| 636 |
* |
| 637 |
* @return string|bool |
| 638 |
*/ |
| 639 |
function learn_press_get_order_status_label( $order_id = 0 ) { |
| 640 |
$statuses = learn_press_get_order_statuses(); |
| 641 |
if ( is_numeric( $order_id ) ) { |
| 642 |
$status = get_post_status( $order_id ); |
| 643 |
} else { |
| 644 |
$status = $order_id; |
| 645 |
} |
| 646 |
|
| 647 |
return ! empty( $statuses[ $status ] ) ? $statuses[ $status ] : false; |
| 648 |
} |
| 649 |
|
| 650 |
/** |
| 651 |
* Get list of registered order's statuses and/or labels. |
| 652 |
* |
| 653 |
* @param bool $prefix |
| 654 |
* @param bool $status_only |
| 655 |
* |
| 656 |
* @since 2.1.7 |
| 657 |
* |
| 658 |
* @return array |
| 659 |
*/ |
| 660 |
function learn_press_get_order_statuses( $prefix = true, $status_only = false ) { |
| 661 |
$register_statues = learn_press_get_register_order_statuses(); |
| 662 |
|
| 663 |
if ( ! $prefix ) { |
| 664 |
$order_statuses = array(); |
| 665 |
foreach ( $register_statues as $k => $v ) { |
| 666 |
$k = preg_replace( '~^lp-~', '', $k ); |
| 667 |
$order_statuses[ $k ] = $v; |
| 668 |
} |
| 669 |
} else { |
| 670 |
$order_statuses = $register_statues; |
| 671 |
} |
| 672 |
|
| 673 |
$order_statuses = wp_list_pluck( $order_statuses, 'label' ); |
| 674 |
|
| 675 |
if ( $status_only ) { |
| 676 |
$order_statuses = array_keys( $order_statuses ); |
| 677 |
} |
| 678 |
|
| 679 |
// @deprecated |
| 680 |
$order_statuses = apply_filters( 'learn_press_order_statuses', $order_statuses ); |
| 681 |
|
| 682 |
// @since 3.0.0 |
| 683 |
return apply_filters( 'learn-press/order-statues', $order_statuses ); |
| 684 |
} |
| 685 |
|
| 686 |
/** |
| 687 |
* Get list of registered order's statues for registering with wp post's status. |
| 688 |
* |
| 689 |
* @since 2.0.0 |
| 690 |
* |
| 691 |
* @return array |
| 692 |
*/ |
| 693 |
function learn_press_get_register_order_statuses() { |
| 694 |
$order_statues = array(); |
| 695 |
|
| 696 |
$order_statues['lp-pending'] = array( |
| 697 |
'label' => _x( 'Pending', 'Order status', 'learnpress' ), |
| 698 |
'public' => false, |
| 699 |
'exclude_from_search' => false, |
| 700 |
'show_in_admin_all_list' => true, |
| 701 |
'show_in_admin_status_list' => true, |
| 702 |
'label_count' => _n_noop( 'Pending Payment <span class="count">(%s)</span>', 'Pending Payment <span class="count">(%s)</span>', 'learnpress' ), |
| 703 |
); |
| 704 |
$order_statues['lp-processing'] = array( |
| 705 |
'label' => _x( 'Processing', 'Order status', 'learnpress' ), |
| 706 |
'public' => false, |
| 707 |
'exclude_from_search' => false, |
| 708 |
'show_in_admin_all_list' => true, |
| 709 |
'show_in_admin_status_list' => true, |
| 710 |
'label_count' => _n_noop( 'Processing <span class="count">(%s)</span>', 'Processing <span class="count">(%s)</span>', 'learnpress' ), |
| 711 |
); |
| 712 |
$order_statues['lp-completed'] = array( |
| 713 |
'label' => _x( 'Completed', 'Order status', 'learnpress' ), |
| 714 |
'public' => false, |
| 715 |
'exclude_from_search' => false, |
| 716 |
'show_in_admin_all_list' => true, |
| 717 |
'show_in_admin_status_list' => true, |
| 718 |
'label_count' => _n_noop( 'Completed <span class="count">(%s)</span>', 'Completed <span class="count">(%s)</span>', 'learnpress' ), |
| 719 |
); |
| 720 |
$order_statues['lp-cancelled'] = array( |
| 721 |
'label' => _x( 'Cancelled', 'Order status', 'learnpress' ), |
| 722 |
'public' => false, |
| 723 |
'exclude_from_search' => false, |
| 724 |
'show_in_admin_all_list' => true, |
| 725 |
'show_in_admin_status_list' => true, |
| 726 |
'label_count' => _n_noop( 'Cancelled <span class="count">(%s)</span>', 'Cancelled <span class="count">(%s)</span>', 'learnpress' ), |
| 727 |
); |
| 728 |
$order_statues['lp-failed'] = array( |
| 729 |
'label' => _x( 'Failed', 'Order status', 'learnpress' ), |
| 730 |
'public' => false, |
| 731 |
'exclude_from_search' => false, |
| 732 |
'show_in_admin_all_list' => true, |
| 733 |
'show_in_admin_status_list' => true, |
| 734 |
'label_count' => _n_noop( 'Failed <span class="count">(%s)</span>', 'Failed <span class="count">(%s)</span>', 'learnpress' ), |
| 735 |
); |
| 736 |
|
| 737 |
return $order_statues; |
| 738 |
} |
| 739 |
|
| 740 |
function _learn_press_get_order_status_description( $status ) { |
| 741 |
static $descriptions = null; |
| 742 |
$descriptions = array( |
| 743 |
'pending' => __( 'Order received in case user buy a course but doesn\'t finalise the order.', 'learnpress' ), |
| 744 |
'processing' => __( 'Payment received and the order is awaiting fulfillment.', 'learnpress' ), |
| 745 |
'completed' => __( 'Order fulfilled and complete.', 'learnpress' ), |
| 746 |
'cancelled' => __( 'The order is cancelled by an admin or the customer.', 'learnpress' ), |
| 747 |
); |
| 748 |
|
| 749 |
return apply_filters( 'learn_press_order_status_description', ! empty( $descriptions[ $status ] ) ? $descriptions[ $status ] : '' ); |
| 750 |
} |
| 751 |
|
| 752 |
/** |
| 753 |
* Get status of an order by the ID. |
| 754 |
* |
| 755 |
* @param int $order_id |
| 756 |
* |
| 757 |
* @return bool|string |
| 758 |
*/ |
| 759 |
function learn_press_get_order_status( $order_id ) { |
| 760 |
$order = learn_press_get_order( $order_id ); |
| 761 |
|
| 762 |
if ( $order ) { |
| 763 |
return $order->get_status(); |
| 764 |
} |
| 765 |
|
| 766 |
return false; |
| 767 |
} |
| 768 |
|
| 769 |
if ( ! function_exists( 'learn_press_cancel_order_process' ) ) { |
| 770 |
/** |
| 771 |
* Process action allows user to cancel an order is pending |
| 772 |
* in their profile. |
| 773 |
*/ |
| 774 |
function learn_press_cancel_order_process() { |
| 775 |
if ( empty( $_REQUEST['cancel-order'] ) || empty( $_REQUEST['lp-nonce'] ) || ! wp_verify_nonce( $_REQUEST['lp-nonce'], 'cancel-order' ) || is_admin() ) { |
| 776 |
return; |
| 777 |
} |
| 778 |
|
| 779 |
$order_id = absint( $_REQUEST['cancel-order'] ); |
| 780 |
$order = learn_press_get_order( $order_id ); |
| 781 |
$user = learn_press_get_current_user(); |
| 782 |
|
| 783 |
if ( ! $order ) { |
| 784 |
learn_press_add_message( sprintf( __( 'Order number <strong>%s</strong> not found', 'learnpress' ), $order_id ), 'error' ); |
| 785 |
} elseif ( $order->has_status( 'pending' ) ) { |
| 786 |
$order->update_status( 'cancelled' ); |
| 787 |
$order->add_note( __( 'Order cancelled by customer', 'learnpress' ) ); |
| 788 |
|
| 789 |
// set updated message |
| 790 |
learn_press_add_message( sprintf( __( 'Order number <strong>%s</strong> has been cancelled', 'learnpress' ), $order->get_order_number() ) ); |
| 791 |
$url = $order->get_cancel_order_url( true ); |
| 792 |
} else { |
| 793 |
learn_press_add_message( sprintf( __( 'Order number <strong>%s</strong> can not be cancelled', 'learnpress' ), $order->get_order_number() ), 'error' ); |
| 794 |
} |
| 795 |
if ( ! $url ) { |
| 796 |
$url = learn_press_user_profile_link( $user->get_id(), LP_Settings::instance()->get( 'profile_endpoints.profile-orders', 'orders' ) ); |
| 797 |
} |
| 798 |
wp_safe_redirect( $url ); |
| 799 |
exit(); |
| 800 |
} |
| 801 |
} |
| 802 |
add_action( 'init', 'learn_press_cancel_order_process' ); |
| 803 |
|
| 804 |
|
| 805 |
/** |
| 806 |
* get total price order complete |
| 807 |
* |
| 808 |
*/ |
| 809 |
|
| 810 |
function learn_press_get_total_price_order_complete() { |
| 811 |
global $wpdb; |
| 812 |
|
| 813 |
$query = $wpdb->prepare( |
| 814 |
"SELECT SUM(meta_value) as order_total From `{$wpdb->prefix}postmeta` as mt |
| 815 |
INNER JOIN `{$wpdb->prefix}posts` as p ON p.id = mt.post_id |
| 816 |
WHERE p.post_type = %s AND mt.meta_key = %s |
| 817 |
", |
| 818 |
LP_ORDER_CPT, |
| 819 |
'_order_total' |
| 820 |
); |
| 821 |
|
| 822 |
$total = $wpdb->get_results( $query )[0]->order_total; |
| 823 |
|
| 824 |
return learn_press_format_price( $total, true ); |
| 825 |
|
| 826 |
} |
| 827 |
/** |
| 828 |
* Auto enroll course after user checkout |
| 829 |
* |
| 830 |
* @param $result |
| 831 |
* @param $order_id |
| 832 |
* @editor tungnx - comment - for not use |
| 833 |
*/ |
| 834 |
/*function _learn_press_checkout_auto_enroll_free_course( $result, $order_id ) { |
| 835 |
return; |
| 836 |
$enrolled = false; |
| 837 |
$order = learn_press_get_order( $order_id, true ); |
| 838 |
|
| 839 |
if ( $order_id && $order ) { |
| 840 |
$user = learn_press_get_user( $order->user_id, true ); |
| 841 |
$order_items = $order->get_items(); |
| 842 |
|
| 843 |
if ( $order_items ) { |
| 844 |
foreach ( $order_items as $item ) { |
| 845 |
if ( $user->has_enrolled_course( $item['course_id'] ) ) { |
| 846 |
continue; |
| 847 |
} |
| 848 |
if ( $user->enroll( $item['course_id'] ) ) { |
| 849 |
$enrolled = $item['course_id']; |
| 850 |
} |
| 851 |
} |
| 852 |
if ( ! $enrolled ) { |
| 853 |
$item = reset( $order_items ); |
| 854 |
$enrolled = $item['course_id']; |
| 855 |
} |
| 856 |
} |
| 857 |
} |
| 858 |
if ( $enrolled ) { |
| 859 |
learn_press_add_message( sprintf( __( 'You have enrolled in this course. <a href="%s">Order details</a>', 'learnpress' ), $result['redirect'] ) ); |
| 860 |
$result['redirect'] = get_the_permalink( $enrolled ); |
| 861 |
LearnPress::instance()->cart->empty_cart(); |
| 862 |
} |
| 863 |
|
| 864 |
return $result; |
| 865 |
}*/ |
| 866 |
|
| 867 |
|