filter woocommerce cancel unpaid order

2019-07-28 04:50发布

i want to ask how to overwrite this woocommerce function (wp_schedule_single_event and post_status) using filter/action Plugin file location : /woocommerce/includes/wc-order-functions.php

Original File :

/**
 * 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' );

I want to change this variable :

$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 ) );

and this

    **wp_schedule_single_event( time() + 3600 ),** 'woocommerce_cancel_unpaid_orders' );

please help..

Thanks Azreal

1条回答
【Aperson】
2楼-- · 2019-07-28 05:28

This is absolutely possible - wc_cancel_unpaid_orders is not called directly, rather it's hooked on to the woocommerce_cancel_unpaid_orders action, at line 488 of that file:

add_action( 'woocommerce_cancel_unpaid_orders', 'wc_cancel_unpaid_orders' );

this means that you can unhook THEIR wc_cancel_unpaid_orders, and assign your own. For example, you could add this to your functions.php file:

<?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' );
}
查看更多
登录 后发表回答