Woocommerce - Allowing Order Again for different s

2019-09-08 12:08发布

问题:

I'm looking to allow the Order Again functionality to all statuses. By default WooCommerce only allows orders with a status of COMPLETED this functionality. It seems to be a two step process as the first step requires the button being shown to the user, this is completed by editing this file:

wc-template-functions.php

With this snippit of code:

function woocommerce_order_again_button( $order ) {
        //if ( ! $order || ! $order->has_status( 'completed' ) || ! is_user_logged_in() ) {
        // Allow 'Order Again' at all times.
        if ( ! $order || ! is_user_logged_in() ) {
            return;
        }

        wc_get_template( 'order/order-again.php', array(
            'order' => $order
        ) );
    }

By commenting out the validation of the $order->has_status() method, I'm able to show the button on the page. However, when trying clicking the Order Again button, it still does a check before adding the items to the cart.

Can anyone tell me where this code is stored to do a preliminary check on the $order->has_status()?

回答1:

For those having the same issue, this plugin was able to accomplish what I wanted:

https://wordpress.org/plugins/one-click-order-reorder/installation/



回答2:

Since the OP's original question, WooCommerce has added a filter to these statuses. The function can be found in includes/wc-template-functions.php

https://docs.woocommerce.com/wp-content/images/wc-apidocs/function-woocommerce_order_again_button.html

/**
 * Display an 'order again' button on the view order page.
 *
 * @param object $order
 * @subpackage  Orders
 */
function woocommerce_order_again_button( $order ) {
    if ( ! $order || ! $order->has_status( apply_filters( 'woocommerce_valid_order_statuses_for_order_again', array( 'completed' ) ) ) || ! is_user_logged_in() ) {
        return;
    }

    wc_get_template( 'order/order-again.php', array(
        'order' => $order,
    ) );
}

So in order to filter the statuses you could do something like this (wc_get_order_statuses() just returns all order statuses in this case; you can set the $statuses variable to an array of an statuses you would like):

add_filter('woocommerce_valid_order_statuses_for_order_again', function( $statuses ){

    $statuses = wc_get_order_statuses();

    return $statuses;

}, 10, 2);


回答3:

You can simply add difference order status to filter woocommerce_valid_order_statuses_for_order_again.

add_filter( 'woocommerce_valid_order_statuses_for_order_again', 'add_order_again_status', 10, 1);

function add_order_again_status($array){
    $array = array_merge($array, array('on-hold', 'processing', 'pending-payment', 'cancelled', 'refunded'));
    return $array;
}