2017-02-13 110 views
3

該功能位於班WC_Abstract_Order(核心文件)設置自定義訂單狀態爲有效的付款

/* Checks if an order needs payment, based on status and order total. 
* 
* @return bool 
*/ 
public function needs_payment() { 

    $valid_order_statuses = apply_filters('woocommerce_valid_order_statuses_for_payment', array('pending', 'failed'), $this); 

    if ($this->has_status($valid_order_statuses) && $this->get_total() > 0) { 
     $needs_payment = true; 
    } else { 
     $needs_payment = false; 
    } 

    return apply_filters('woocommerce_order_needs_payment', $needs_payment, $this, $valid_order_statuses); 
} 

我需要一個額外的定製訂單狀態添加到陣列中,但不能制定出碼爲functions.php覆蓋函數,這將是這樣 - 即只是與添加的狀態:

public function needs_payment() { 

    $valid_order_statuses = apply_filters('woocommerce_valid_order_statuses_for_payment', array('pending', 'failed','neworderstatus'), $this); 

    if ($this->has_status($valid_order_statuses) && $this->get_total() > 0) { 
     $needs_payment = true; 
    } else { 
     $needs_payment = false; 
    } 

    return apply_filters('woocommerce_order_needs_payment', $needs_payment, $this, $valid_order_statuses); 
} 

任何幫助感激地接受。

謝謝。

回答

0

這個作品,感謝@ LoicTheAztec

// New order status AFTER woo 2.2 
    add_action('init', 'register_my_new_order_statuses'); 

    function register_my_new_order_statuses() { 
register_post_status('wc-custom-status', array(
    'label'      => _x('Custom Status', 'Order status', 'woocommerce'), 
    'public'     => true, 
    'exclude_from_search'  => false, 
    'show_in_admin_all_list' => true, 
    'show_in_admin_status_list' => true, 
    'label_count'    => _n_noop('Custom Status <span class="count">(%s)</span>', 'Custom Status<span class="count">(%s)</span>', 'woocommerce') 
)); 
} 

    add_filter('wc_order_statuses', 'my_new_wc_order_statuses'); 

    // Register in wc_order_statuses. 
    function my_new_wc_order_statuses($order_statuses) { 
$order_statuses['wc-custom-status'] = _x('Custom Status', 'Order status', 'woocommerce'); 

return $order_statuses; 
} 

then this b它來自Loic -

add_filter('woocommerce_valid_order_statuses_for_payment', 'custom_status_valid_for_payment', 10, 2); 
    function custom_status_valid_for_payment($statuses, $order) { 

// Registering the custom status as valid for payment 
$statuses[] = 'wc-custom-status'; 

return $statuses; 
} 
4

首先,您需要註冊您的自定義狀態(如果不這樣做):

// Register new status 
add_action('init', 'register_custom_order_statuses'); 
function register_custom_order_statuses() { 
    register_post_status('wc-custom-status', array(
     'label' => 'Custom Status', 
     'public' => true, 
     'exclude_from_search' => false, 
     'show_in_admin_all_list' => true, 
     'show_in_admin_status_list' => true, 
     'label_count' => _n_noop('Custom Status <span class="count">(%s)</span>', 'Custom Status <span class="count">(%s)</span>') 
    )); 
} 

// Add to list of WC Order statuses 
add_filter('wc_order_statuses', 'add_custom_order_statuses'); 
function add_custom_order_statuses($order_statuses) { 
    $new_order_statuses = array(); 

    // add new order status after processing for example 
    foreach ($order_statuses as $key => $status) { 
     $new_order_statuses[$key] = $status; 
     if ('wc-processing' === $key) { 
      $new_order_statuses['wc-custom-status'] = 'Custom Status'; 
     } 
    } 
    return $new_order_statuses; 
} 

現在woocommerce_valid_order_statuses_for_payment過濾鉤子,你可以設置這個「自定義狀態」作爲支付有效訂單狀態,在這個簡單的方法:

add_filter('woocommerce_valid_order_statuses_for_payment', 'custom_status_valid_for_payment', 10, 2); 
function custom_status_valid_for_payment($statuses, $order) { 

    // Registering the custom status as valid for payment 
    $statuses[] = 'wc-custom-status'; 

    return $statuses; 
} 

代碼放在您的活動子主題(或主題)的function.php文件或也以任何插件文件。

我還沒有真正測試這最後的代碼片斷,但在邏輯上預期它應該工作...


相關答案:Adding custom order statuses in Admin Dashboard Stats Widget

+0

感謝您的幫助。我無法將其解決,因爲我無法使用我們的設置,但我沒有理由相信您的代碼不正確,應該爲其他人工作。 –

+0

感謝您的幫助,最終我決定取消插件並使用與您的函數類似的代碼。 - 我必須爲第一位使用稍微不同的代碼,因爲它使用我正在使用的插件時發生錯誤,請參閱其他代碼中的其他答案 - 不確定其中的差異是什麼,但我想它是... –