过滤woocommerce取消未付订单 [英] filter woocommerce cancel unpaid order

查看:78
本文介绍了过滤woocommerce取消未付订单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问一下如何使用过滤器/操作覆盖此woocommerce函数(wp_schedule_single_event和post_status) 插件文件位置:/woocommerce/includes/wc-order-functions.php

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

原始文件:

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

请帮助.

谢谢 Azreal

推荐答案

这是绝对可能的-wc_cancel_unpaid_orders不会直接调用,而是挂接到woocommerce_cancel_unpaid_orders操作上,位于

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

这意味着您可以解除其wc_cancel_unpaid_orders的钩,然后分配自己的.例如,您可以将其添加到您的functions.php文件中:

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' );
}

这篇关于过滤woocommerce取消未付订单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆