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

标签 php wordpress woocommerce orders cancel-button

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

我也尝试将函数添加到functions.php中,也得到了参数太少的错误: enter image description here

我做了 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;
}

最佳答案

As this hook is used 2 times in the Woocommerce core code with a different number of arguments for each:

It's complicate to handle, without making some issue…

我发现以下转机(非常简单的更新)应该可以解决问题:

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 文件中。它现在应该可以工作了。

enter image description here

关于php - Woocommerce 中我的帐户订单列表上的条件取消按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50164552/

相关文章:

php - 从 php : headers are interpreted as body? 发送邮件

php - 文件上传 PHP

wordpress - woocommerce 订单放置在 wordpress 数据库中的哪个位置

php - 显示 WooCommerce 购物车中的所有交叉销售商品

wordpress - 获取 Woocommerce 类别缩略图

php - WordPress 尝试激活错误的插件

php - 带有 apache 虚拟主机的 Websocket

javascript - 我的 Jquery 脚本无法在 IE/Safari/Firefox 中运行

mysql - 使用 MySQL 查询以秒为单位增加日期/时间值

css - 元素选择器在 Wordpress 站点的样式表中不起作用