2016-12-08 70 views
1

我想請教如何使用過濾器/行動 插件文件位置,以覆蓋該woocommerce功能(wp_schedule_single_event和post_status):/woocommerce/includes/wc-order-functions.php過濾woocommerce取消未支付的訂單

原始文件:

/** 
* Cancel all unpaid orders after held duration to prevent stock lock for those products. 
* 
* @access public 
*/ 
function wc_cancel_unpaid_orders() { 
    global $wpdb; 

    $held_duration = get_option('woocommerce_hold_stock_minutes'); 

    if ($held_duration < 1 || get_option('woocommerce_manage_stock') != 'yes') 
     return; 

    $date = date("Y-m-d H:i:s", strtotime('-' . absint($held_duration) . ' MINUTES', current_time('timestamp'))); 

    $unpaid_orders = $wpdb->get_col($wpdb->prepare(" 
     SELECT posts.ID 
     FROM {$wpdb->posts} AS posts 
     WHERE posts.post_type IN ('" . implode("','", wc_get_order_types()) . "') 
     AND  posts.post_status = 'wc-pending' 
     AND  posts.post_modified < %s 
    ", $date)); 

    if ($unpaid_orders) { 
     foreach ($unpaid_orders as $unpaid_order) { 
      $order = wc_get_order($unpaid_order); 

      if (apply_filters('woocommerce_cancel_unpaid_order', 'checkout' === get_post_meta($unpaid_order, '_created_via', true), $order)) { 
       $order->update_status('cancelled', __('Unpaid order cancelled - time limit reached.', 'woocommerce')); 
      } 
     } 
    } 

    wp_clear_scheduled_hook('woocommerce_cancel_unpaid_orders'); 
    wp_schedule_single_event(time() + (absint($held_duration) * 60), 'woocommerce_cancel_unpaid_orders'); 
} 
add_action('woocommerce_cancel_unpaid_orders', 'wc_cancel_unpaid_orders'); 

我想改變這個變量:

$unpaid_orders = $wpdb->get_col($wpdb->prepare(" 
    SELECT posts.ID 
    FROM {$wpdb->posts} AS posts 
    WHERE posts.post_type IN ('" . implode("','", wc_get_order_types()) . "') 
    AND  posts.post_status = '**wc-on-hold**' 
    AND  posts.post_modified < %s 
", $date)); 

**wp_schedule_single_event(time() + 3600),** 'woocommerce_cancel_unpaid_orders'); 

請幫助..

感謝 Azreal

回答

0

這是絕對有可能的 - wc_cancel_unpaid_orders不是直接調用,而它的鉤到woocommerce_cancel_unpaid_orders行動,在line 488 of that file

add_action('woocommerce_cancel_unpaid_orders', 'wc_cancel_unpaid_orders'); 

這意味着你可以解開他們的wc_cancel_unpaid_orders,並分配你自己的。例如,您可以將其添加到您的functions.php文件中:

<?php 
remove_action('woocommerce_cancel_unpaid_orders', 'wc_cancel_unpaid_orders'); 
add_action('woocommerce_cancel_unpaid_orders', 'my_custom_wc_cancel_unpaid_orders'); 

function my_custom_wc_cancel_unpaid_orders() { 
    global $wpdb; 

    $held_duration = get_option('woocommerce_hold_stock_minutes'); 

    if ($held_duration < 1 || get_option('woocommerce_manage_stock') != 'yes') 
     return; 

    $date = date("Y-m-d H:i:s", strtotime('-' . absint($held_duration) . ' MINUTES', current_time('timestamp'))); 
    // write your own code here.... 
    $unpaid_orders = $wpdb->get_col($wpdb->prepare(" 
     SELECT posts.ID 
     FROM {$wpdb->posts} AS posts 
     WHERE posts.post_type IN ('" . implode("','", wc_get_order_types()) . "') 
     AND  posts.post_status = 'wc-pending' 
     AND  posts.post_modified < %s 
    ", $date)); 

    if ($unpaid_orders) { 
     foreach ($unpaid_orders as $unpaid_order) { 
      $order = wc_get_order($unpaid_order); 

      if (apply_filters('woocommerce_cancel_unpaid_order', 'checkout' === get_post_meta($unpaid_order, '_created_via', true), $order)) { 
       $order->update_status('cancelled', __('Unpaid order cancelled - time limit reached.', 'woocommerce')); 
      } 
     } 
    } 

    wp_clear_scheduled_hook('woocommerce_cancel_unpaid_orders'); 
    // do whatever you want here as well... 
    wp_schedule_single_event(time() + (absint($held_duration) * 60), 'woocommerce_cancel_unpaid_orders'); 
} 
+0

謝謝,現在工作:) – Azreal