4

我在 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->update_status( 'cancelled', __( 'Unpaid order cancelled - time limit reached.', 'woocommerce' ) );

有了这个

$order->update_status( '完成', __( '订单完成 - 达到时间限制。', 'woocommerce' ) );

直到尚未测试bcz我想在测试之前确认。

4

0 回答 0