Home / eCommerce / WWPAY – Change Order status (On Hold → Processing)
Duplicate Snippet

Embed Snippet on Your Site

WWPAY – Change Order status (On Hold → Processing)

Code Preview
php
<?php
/**
 * Plugin Name: Wholesale Payments — send $0-today plan orders to Processing
 * Description: Temporary workaround. Moves a Wholesale Payments order from On Hold
 *              to Processing when the hold was caused only by "no payment is due
 *              today". Holds caused by an overdue balance or an exceeded credit
 *              limit are left alone. The workaround turns itself off in
 *              Wholesale Payments 1.0.8, which adds a built-in setting.
 */
defined( 'ABSPATH' ) || exit;
function wpay_day_zero_workaround_is_needed() {
    if ( false !== get_option( 'wpay_day_zero_order_status', false ) ) {
        return false;
    }
    $version = is_multisite()
        ? get_site_option( 'wpay_installed_version', '' )
        : get_option( 'wpay_installed_version', '' );
    if ( $version && version_compare( $version, '1.0.8', '>=' ) ) {
        return false;
    }
    return true;
}
add_action(
    'woocommerce_order_status_changed',
    function ( $order_id, $from, $to, $order ) {
        if ( 'on-hold' !== $to ) {
            return;
        }
        if ( ! wpay_day_zero_workaround_is_needed() ) {
            return;
        }
        if ( ! in_array( $from, array( '', 'pending', 'failed' ), true ) ) {
            return;
        }
        if ( 'wc_wholesale_payments' !== $order->get_payment_method() ) {
            return;
        }
        if ( ! class_exists( '\RymeraWebCo\WPay\Helpers\WPAY_Invoices' ) ) {
            return;
        }
        $user_id = $order->get_customer_id();
        // Leave the overdue-balance hold in place.
        if ( 'yes' === get_option( 'wpay_overdue_restriction_enabled', 'no' ) && $user_id ) {
            $threshold = absint( get_option( 'wpay_overdue_days_threshold', 0 ) );
            if ( \RymeraWebCo\WPay\Helpers\WPAY_Invoices::customer_has_overdue_invoices( $user_id, $threshold ) ) {
                return;
            }
        }
        // Leave the credit-limit hold in place.
        if ( 'yes' === get_option( 'wpay_credit_limit_enabled', 'no' ) && $user_id ) {
            if ( \RymeraWebCo\WPay\Helpers\WPAY_Invoices::customer_exceeds_credit_limit( $user_id ) ) {
                return;
            }
        }
        add_action(
            'shutdown',
            function () use ( $order_id ) {
                $order = wc_get_order( $order_id );
                if ( ! $order || ! $order->has_status( 'on-hold' ) ) {
                    return;
                }
                $order->update_status(
                    'processing',
                    __( 'Moved to Processing by site workaround: the payment plan balance is still outstanding.', 'woocommerce-wholesale-payments' )
                );
            }
        );
    },
    10,
    4
);
function wpay_day_zero_workaround_notice() {
    if ( wpay_day_zero_workaround_is_needed() || ! current_user_can( 'manage_woocommerce' ) ) {
        return;
    }
    printf(
        '<div class="notice notice-info is-dismissible"><p>%s</p></div>',
        esc_html__(
            'Wholesale Payments now includes an order status setting, so the "$0-today plan orders to Processing" workaround is inactive. Set the status on the Wholesale Payments settings page, then remove the workaround file.',
            'woocommerce-wholesale-payments'
        )
    );
}
add_action( 'admin_notices', 'wpay_day_zero_workaround_notice' );

Comments

Add a Comment