【问题标题】:Conditional Cancel Button on my account orders list in WoocommerceWoocommerce 中我的帐户订单列表上的条件取消按钮
【发布时间】:2018-10-14 07:53:43
【问题描述】:

这是参考Add Cancel button on My account Orders list using Woo Cancel for Customers Plugin答案:

我也尝试将函数添加到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;
}

【问题讨论】:

    标签: php wordpress woocommerce orders cancel-button


    【解决方案1】:

    因为这个钩子在 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 文件中。它现在应该可以工作了。

    【讨论】:

      猜你喜欢
      • 2019-10-04
      • 2021-06-08
      • 1970-01-01
      • 1970-01-01
      • 2019-08-08
      • 2021-06-05
      • 2016-12-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多