WPITECH
WooCommerce

How to Change WooCommerce Payment Status for Prepaid Orders

How to Change WooCommerce Payment Status

WooCommerce payment status management is an important online store operation that could take a lot of time to process. That’s why it is necessary that all the orders should be updated automatically to show the most recent status.

Cash-on-Delivery is the most demanding option that no WooCommerce store owners can overlook. There are several scenarios where customers want to pre-pay for their online purchases to speed up the order fulfillment process. In some cases, the order has been fulfilled and delivered to the customer but the payment status for the order not updated.

How to Update WooCommerce Payment Status Automatically

You can simply automate the whole process of payment status update for orders that are pre-paid. The orders updated to “Complete” automatically when the WooCommerce payment status will be updated.

add_action('woocommerce_order_payment_status_changed', 'woocommerce_payment_complete_order_status',10,3);
function woocommerce_payment_complete_order_status($order_id)
{
    if ( ! $order_id ) {
        return;
    }
    $order = wc_get_order( $order_id );
    if ($order->data['status'] == 'wc-processing') {
        $payment_method=$order->get_payment_method();
        if ($payment_method != "cod")
        {
            $order->update_status( 'wc-completed' );
        }
    }
}

The above code uses the woocommerce_order_payment_status_changed hook to call in the function when the WooCommerce order status gets changed. Once the order status is “Processing” and the payment method is not Cash-on-Delivery, the status changed to “Complete

update_status() sets the order payment status that is passed to the woocommerce_order_payment_status_changed method. Here is a detailed list of the WooCommerce payment status you can pass to this method:

<option value=”wc-on-hold”>On hold</option>

<option value=”wc-completed”>Completed</option>

<option value=”wc-pending”>Pending payment</option>

<option value=”wc-processing”>Processing</option>

<option value=”wc-cancelled”>Cancelled</option>

<option value=”wc-refunded”>Refunded</option>

<option value=”wc-failed”>Failed</option>

wc_get_order() is a wrapper function to get the data about a specific order and $order gets the order information as an object through wc_get_order() function. You can also use the following code to get the total order.

$order->get_id();

$order->get_formatted_order_total( );

If you have selected “Cash on delivery” payment method then the checkout page shown as:

Cash on delivery WooCommerce payment status

After the order processed you will see

WooCommerce complte order payment status

Display WooCommerce Order Product Information

Adding the below code snippet will display the products in an order. You have to place this code in the template file where you want to display the information.

$filters = array(
'post_status' => 'any',
'post_type' => 'shop_order',
'orderby' => 'modified',
'order' => 'ASC'
);
$loop = new WP_Query($filters);
while ($loop->have_posts()) {
$loop->the_post();
$order = new WC_Order($loop->post->ID);
foreach ($order->get_items() as $key => $lineItem) {
echo '<br>' . 'Product Name : ' . $lineItem['name'];
echo 'Product ID : ' . $lineItem['product_id'];
if ($lineItem['variation_id']) {
echo 'Product Type : Variable Product';
} else {
echo 'Product Type : Simple Product';
}
}
}

If you want to change the WooCommerce order status based on the order status then there is a short tutorial on WooCommerce order status.

Related posts

How to Add WooCommerce Registration Form Fields

WPITECH

How to Setup WooCommerce Deposits for Store Product

WPITECH

Shortcodes of WooCommerce

WPITECH