将自定义订单状态设置为对付款有效 [英] Setting custom order statuses as valid for payment

查看:100
本文介绍了将自定义订单状态设置为对付款有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此函数位于WC_Abstract_Order类(核心文件)中

This function is located in the class WC_Abstract_Order (core files)

/* 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的代码以覆盖该函数,就像这样-即,仅具有添加的状态:

I need to add an additional custom order status to the array but can't work out the code for functions.php to override the function, which would be like this - i.e. just with the added status:

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 );
}

任何帮助都感激不尽.

谢谢.

推荐答案

首先,您需要注册自定义状态(如果未完成):

First you need to register your custom status (If is not done):

// 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 过滤器挂钩中,您可以通过以下简单方式将此自定义状态"设置为有效的付款订单状态:

Now in woocommerce_valid_order_statuses_for_payment filter hook, You can set this 'Custom Status' as a valid order status for payment, in this simple way:

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文件中,也可能会出现在任何插件文件中.

它应该能按预期工作...

It should work as expected...

相关答案:在管理控制台"统计信息中添加自定义订单状态小部件

这篇关于将自定义订单状态设置为对付款有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆