过滤 woocommerce 取消未付订单
Posted
技术标签:
【中文标题】过滤 woocommerce 取消未付订单【英文标题】:filter woocommerce cancel unpaid order 【发布时间】:2017-04-23 11:52:45 【问题描述】:我想问一下如何使用过滤器/操作覆盖这个 woocommerce 功能(wp_schedule_single_event 和 post_status) 插件文件位置:/woocommerce/includes/wc-order-functions.php
原始文件:
/**
* Cancel all unpaid orders after held duration to prevent stock lock for those products.
*
* @access public
*/
function wc_cancel_unpaid_orders()
global $wpdb;
$held_duration = get_option( 'woocommerce_hold_stock_minutes' );
if ( $held_duration < 1 || get_option( 'woocommerce_manage_stock' ) != 'yes' )
return;
$date = date( "Y-m-d H:i:s", strtotime( '-' . absint( $held_duration ) . ' MINUTES', current_time( 'timestamp' ) ) );
$unpaid_orders = $wpdb->get_col( $wpdb->prepare( "
SELECT posts.ID
FROM $wpdb->posts AS posts
WHERE posts.post_type IN ('" . implode( "','", wc_get_order_types() ) . "')
AND posts.post_status = 'wc-pending'
AND posts.post_modified < %s
", $date ) );
if ( $unpaid_orders )
foreach ( $unpaid_orders as $unpaid_order )
$order = wc_get_order( $unpaid_order );
if ( apply_filters( 'woocommerce_cancel_unpaid_order', 'checkout' === get_post_meta( $unpaid_order, '_created_via', true ), $order ) )
$order->update_status( 'cancelled', __( 'Unpaid order cancelled - time limit reached.', 'woocommerce' ) );
wp_clear_scheduled_hook( 'woocommerce_cancel_unpaid_orders' );
wp_schedule_single_event( time() + ( absint( $held_duration ) * 60 ), 'woocommerce_cancel_unpaid_orders' );
add_action( 'woocommerce_cancel_unpaid_orders', 'wc_cancel_unpaid_orders' );
我想改变这个变量:
$unpaid_orders = $wpdb->get_col( $wpdb->prepare( "
SELECT posts.ID
FROM $wpdb->posts AS posts
WHERE posts.post_type IN ('" . implode( "','", wc_get_order_types() ) . "')
AND posts.post_status = '**wc-on-hold**'
AND posts.post_modified < %s
", $date ) );
还有这个
**wp_schedule_single_event( time() + 3600 ),** 'woocommerce_cancel_unpaid_orders' );
请帮忙..
谢谢 阿兹雷尔
【问题讨论】:
关注这个问题:***.com/questions/52385537/… 【参考方案1】:这绝对是可能的 - wc_cancel_unpaid_orders
不是直接调用的,而是在line 488 of that file 处与woocommerce_cancel_unpaid_orders
操作挂钩:
add_action( 'woocommerce_cancel_unpaid_orders', 'wc_cancel_unpaid_orders' );
这意味着您可以解开他们的wc_cancel_unpaid_orders
,并分配您自己的。例如,您可以将其添加到您的 functions.php
文件中:
<?php
remove_action( 'woocommerce_cancel_unpaid_orders', 'wc_cancel_unpaid_orders' );
add_action( 'woocommerce_cancel_unpaid_orders', 'my_custom_wc_cancel_unpaid_orders' );
function my_custom_wc_cancel_unpaid_orders()
global $wpdb;
$held_duration = get_option( 'woocommerce_hold_stock_minutes' );
if ( $held_duration < 1 || get_option( 'woocommerce_manage_stock' ) != 'yes' )
return;
$date = date( "Y-m-d H:i:s", strtotime( '-' . absint( $held_duration ) . ' MINUTES', current_time( 'timestamp' ) ) );
// write your own code here....
$unpaid_orders = $wpdb->get_col( $wpdb->prepare( "
SELECT posts.ID
FROM $wpdb->posts AS posts
WHERE posts.post_type IN ('" . implode( "','", wc_get_order_types() ) . "')
AND posts.post_status = 'wc-pending'
AND posts.post_modified < %s
", $date ) );
if ( $unpaid_orders )
foreach ( $unpaid_orders as $unpaid_order )
$order = wc_get_order( $unpaid_order );
if ( apply_filters( 'woocommerce_cancel_unpaid_order', 'checkout' === get_post_meta( $unpaid_order, '_created_via', true ), $order ) )
$order->update_status( 'cancelled', __( 'Unpaid order cancelled - time limit reached.', 'woocommerce' ) );
wp_clear_scheduled_hook( 'woocommerce_cancel_unpaid_orders' );
// do whatever you want here as well...
wp_schedule_single_event( time() + ( absint( $held_duration ) * 60 ), 'woocommerce_cancel_unpaid_orders' );
【讨论】:
以上是关于过滤 woocommerce 取消未付订单的主要内容,如果未能解决你的问题,请参考以下文章
在 Woocommerce 订单管理列表中显示用户失败并取消订单计数
有条件地从 WooCommerce 我的帐户订单中删除取消按钮
php 取消订单时自动恢复WooCommerce Stock
在 WooCommerce 3+ 中将已付款订单标记为“已完成”,将未付款订单标记为“已取消”[重复]