我在WooCommerce有网上商店,有一个选项:如果达到x持续时间,则将该订单的状态更改为已取消,并在订单提交前设置库存。
如果达到x持续时间,则我想更改它,然后订单状态更改完成,同时项目从库存中减少。
据我所知,这是减少库存的代码并取消订单。
/**
* woocommerce_cancel_unpaid_orders function.
*
* @access public
* @return void
*/
function woocommerce_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
LEFT JOIN {$wpdb->term_relationships} AS rel ON posts.ID=rel.object_ID
LEFT JOIN {$wpdb->term_taxonomy} AS tax USING( term_taxonomy_id )
LEFT JOIN {$wpdb->terms} AS term USING( term_id )
WHERE posts.post_type = 'shop_order'
AND posts.post_status = 'publish'
AND tax.taxonomy = 'shop_order_status'
AND term.slug IN ('pending')
AND posts.post_modified < %s
", $date ) );
if ( $unpaid_orders ) {
foreach ( $unpaid_orders as $unpaid_order ) {
$order = new WC_Order( $unpaid_order );
if ( apply_filters( 'woocommerce_cancel_unpaid_order', 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', 'woocommerce_cancel_unpaid_orders' );
就个人而言,我做了这些改变,它们是否有意义?
/**
* woocommerce_cancel_unpaid_orders function.
*
* @access public
* @return void
*/
function woocommerce_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
LEFT JOIN {$wpdb->term_relationships} AS rel ON posts.ID=rel.object_ID
LEFT JOIN {$wpdb->term_taxonomy} AS tax USING( term_taxonomy_id )
LEFT JOIN {$wpdb->terms} AS term USING( term_id )
WHERE posts.post_type = 'shop_order'
AND posts.post_status = 'publish'
AND tax.taxonomy = 'shop_order_status'
AND term.slug IN ('pending')
AND posts.post_modified < %s
", $date ) );
if ( $unpaid_orders ) {
foreach ( $unpaid_orders as $unpaid_order ) {
$order = new WC_Order( $unpaid_order );
if ( apply_filters( 'woocommerce_payment_complete_order_status', true, $order ) )
$order->update_status( 'completed', __( 'order completed - time limit reached.', 'woocommerce' ) );
}
}
wp_clear_scheduled_hook( 'woocommerce_payment_complete_order_status' );
wp_schedule_single_event( time() + ( absint( $held_duration ) * 60 ), 'woocommerce_payment_complete_order_status' );
}
add_action( 'woocommerce_payment_complete_order_status', 'woocommerce_payment_complete_order_status' );
我用这个woocommerce_payment_complete_order_status替换了这个过滤器'woocommerce_cancel_unpaid_order'
并且打开了这个 $ order-&gt; update_status('已取消',__('已取消未付订单 - 已达到时限。','woocommerce'));
这一个
$ order-&gt; update_status('已完成',__('已完成订单 - 已达到时间限制。','woocommerce'));
直到测试之前我还没有测试过。