此函数位于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 );
}
任何帮助都感激不尽。
谢谢。
首先,您需要注册自定义状态(如果未完成):
// 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
这很有效,谢谢@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;
}
然后来自@LoicTheAztec的这一点:
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;
}