编程语言
首页 > 编程语言> > PHP-将自定义订单状态设置为对付款有效

PHP-将自定义订单状态设置为对付款有效

作者:互联网

该函数位于类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

标签:orders,woocommerce,hook-woocommerce,wordpress,php
来源: https://codeday.me/bug/20191026/1935051.html