PHP   发布时间:2022-04-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了PHP-将自定义订单状态设置为对付款有效大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

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

/* checks if an order needs payment, based on status and order @R_588_10586@l.
 *
 * @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_@R_588_10586@l() > 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_@R_588_10586@l() > 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

大佬总结

以上是大佬教程为你收集整理的PHP-将自定义订单状态设置为对付款有效全部内容,希望文章能够帮你解决PHP-将自定义订单状态设置为对付款有效所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: