palsushobhan

Withdrawal on booking status completed on 1st and 15th

Jun 4th, 2021 (edited)
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.97 KB | None | 0 0
  1. add_filter( 'wcfm_order_status_condition', function($conditions, $table_handler) {
  2. global $WCFMmp;
  3. remove_filter( 'wcfm_order_status_condition', array( $WCFMmp->wcfmmp_vendor, 'wcfmmp_vendor_order_status_condition' ), 10 );
  4. //Get all booking products of the vendor
  5. $product_args = array(
  6. 'limit' => -1,
  7. 'status' => array( 'draft', 'pending', 'publish', 'private', 'scheduled' ),
  8. 'type' => 'booking',
  9. 'author' => $WCFMmp->vendor_id,
  10. 'return' => 'ids',
  11. );
  12. $products = wc_get_products($product_args);
  13. //Get all bookings
  14. $args = array(
  15. 'numberposts' => -1,
  16. 'post_type' => 'wc_booking',
  17. 'post_status' => 'complete',
  18. 'meta_query' => array(
  19. array(
  20. 'key' => '_booking_product_id',
  21. 'value' => $products,
  22. 'compare' => 'IN',
  23. )
  24. ),
  25. 'date_query' => array(
  26. 'after' => date('Y-m-d', strtotime('-15 days'))
  27. ),
  28. 'fields' => 'ids',
  29. );
  30. $booking_ids = get_posts($args);
  31. $order_item_ids = array();
  32. foreach($booking_ids as $booking_id) {
  33. $booking = get_wc_booking($booking_id);
  34. if($booking) {
  35. $order_item_ids[] = $booking->get_order_item_id();
  36. }
  37. }
  38.  
  39. $conditions = array();
  40. if(!empty($order_item_ids)) {
  41. $conditions[] = "{$table_handler}.item_id IN ('" . implode( "','", $order_item_ids ) . "')";
  42. }
  43.  
  44. $wcfmmp_marketplace_options = wcfm_get_option( 'wcfm_marketplace_options', array() );
  45. $order_sync = isset( $wcfmmp_marketplace_options['order_sync'] ) ? $wcfmmp_marketplace_options['order_sync'] : 'no';
  46. $status = get_wcfm_marketplace_active_withdrwal_order_status_in_comma();
  47. if( $order_sync == 'yes' ) {
  48. $conditions[] = "{$table_handler}.order_status IN ({$status})";
  49. } else {
  50. $conditions[] = "{$table_handler}.commission_status IN ({$status})";
  51. }
  52.  
  53. return ' AND ( ' . implode(' OR ', $conditions) . ' )';
  54. }, 9, 2 );
  55.  
  56. add_filter('wcfm_schedule_period_starts_from', function( $period_starts_from, $withdrawal_schedule ) {
  57. if($withdrawal_schedule === 'month') {
  58. $now = strtotime('now');
  59. $mid_of_month = strtotime('first day of this month') + 86400 * 14;
  60. $next_month = strtotime('first day of next month');
  61. if($now > $mid_of_month) {
  62. return $next_month;
  63. } else {
  64. return $mid_of_month;
  65. }
  66. }
  67. return $period_starts_from;
  68. }, 10, 2);
  69.  
  70. add_filter('wcfm_schedule_period_interval', function( $period_interval, $withdrawal_schedule ) {
  71. if($withdrawal_schedule === 'month') {
  72. $now = strtotime('now');
  73. $mid_of_month = strtotime('first day of this month') + 86400 * 14;
  74. $next_month = strtotime('first day of next month');
  75. if($now > $mid_of_month) {
  76. return 14;
  77. } else {
  78. return ( $next_month - $mid_of_month ) / 86400;
  79. }
  80. }
  81. return $period_interval;
  82. }, 10, 2);
  83.  
  84. add_action('action_scheduler_after_execute', function($action_id, $action, $context) {
  85. if($action->get_hook() === 'wcfmmp_withdrawal_periodic_scheduler' ) {
  86. if( !get_option( 'wcfmmp_withdrawal_periodic_scheduler_reset' ) ) {
  87. update_option( 'wcfmmp_withdrawal_periodic_scheduler_reset', 1 );
  88. }
  89. }
  90. }, 10, 3);
  91.  
  92. add_action( 'wcfm_init', function() {
  93. if( get_option( 'wcfmmp_withdrawal_periodic_scheduler_reset' ) ) {
  94. $today = date('j');
  95. if( $today > 1 && $today < 15 || $today > 15 && $today < 29 ) {
  96. $next = WC()->queue()->get_next( 'wcfmmp_withdrawal_periodic_scheduler' );
  97. if($next) {
  98. WC()->queue()->cancel_all( 'wcfmmp_withdrawal_periodic_scheduler' );
  99. }
  100. delete_option( 'wcfmmp_withdrawal_periodic_scheduler_reset');
  101. }
  102. }
  103. }, 20 );
Add Comment
Please, Sign In to add comment