Php Woocommerce中我的帐户订单列表上的条件取消按钮

Php Woocommerce中我的帐户订单列表上的条件取消按钮,php,wordpress,woocommerce,orders,cancel-button,Php,Wordpress,Woocommerce,Orders,Cancel Button,这是关于回答: 我还尝试将函数添加到functions.php中,但也遇到了参数太少的错误: 我执行了LoicTheAztec提供的相同功能: add_filter( 'woocommerce_valid_order_statuses_for_cancel', 'custom_valid_order_statuses_for_cancel', 10, 2 ); function custom_valid_order_statuses_for_cancel( $statuses, $order

这是关于回答:

我还尝试将函数添加到functions.php中,但也遇到了参数太少的错误:

我执行了LoicTheAztec提供的相同功能:

add_filter( 'woocommerce_valid_order_statuses_for_cancel', 'custom_valid_order_statuses_for_cancel', 10, 2 );
function custom_valid_order_statuses_for_cancel( $statuses, $order ){

// Set HERE the order statuses where you want the cancel button to appear
$custom_statuses    = array( 'pending', 'processing', 'on-hold', 'failed' );

// Set HERE the delay (in days)
$duration = 3; // 3 days

// UPDATE: Get the order ID and the WC_Order object
if( isset($_GET['order_id']))
    $order = wc_get_order( absint( $_GET['order_id'] ) );

$delay = $duration*24*60*60; // (duration in seconds)
$date_created_time  = strtotime($order->get_date_created()); // Creation date time stamp
$date_modified_time = strtotime($order->get_date_modified()); // Modified date time stamp
$now = strtotime("now"); // Now  time stamp

// Using Creation date time stamp
if ( ( $date_created_time + $delay ) >= $now ) return $custom_statuses;
else return $statuses;
}
由于此钩子在Woocommerce核心代码中使用了2次,每次使用的参数数量不同:

  • 一次
  • 再来一次
处理起来很复杂,没有什么问题

我发现了以下解决问题的方法(非常简单的更新):

add_filter( 'woocommerce_valid_order_statuses_for_cancel', 'filter_valid_order_statuses_for_cancel', 20, 2 );
function filter_valid_order_statuses_for_cancel( $statuses, $order = '' ){

    // Set HERE the order statuses where you want the cancel button to appear
    $custom_statuses    = array( 'pending', 'processing', 'on-hold', 'failed' );

    // Set HERE the delay (in days)
    $duration = 3; // 3 days

    // UPDATE: Get the order ID and the WC_Order object
    if( ! is_object( $order ) && isset($_GET['order_id']) )
        $order = wc_get_order( absint( $_GET['order_id'] ) );

    $delay = $duration*24*60*60; // (duration in seconds)
    $date_created_time  = strtotime($order->get_date_created()); // Creation date time stamp
    $date_modified_time = strtotime($order->get_date_modified()); // Modified date time stamp
    $now = strtotime("now"); // Now  time stamp

    // Using Creation date time stamp
    if ( ( $date_created_time + $delay ) >= $now ) return $custom_statuses;
    else return $statuses;
}
代码进入活动子主题(或活动主题)的function.php文件。现在应该可以了