cal*_*718 3 php wordpress cancel-button orders woocommerce
这是参考使用 Woo Cancel for Customers 插件回答我的帐户订单列表上的添加取消按钮:
我还尝试将函数添加到 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;
}
Run Code Online (Sandbox Code Playgroud)
因为这个钩子在 Woocommerce 核心代码中使用了 2 次,每个都有不同数量的参数:
- 在一个时间类-WC-形状handler.php (与只有一个参数:
$statuses
)- 另一个时间在wc-account-functions.php (有两个参数:
$statuses
和$order
)处理起来很复杂,但不会造成任何问题......
我发现以下转变(一个非常轻的更新)应该可以解决问题:
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;
}
Run Code Online (Sandbox Code Playgroud)
代码位于活动子主题(或活动主题)的 function.php 文件中。它现在应该可以工作了。
归档时间: |
|
查看次数: |
3150 次 |
最近记录: |