skip to Main Content

I used artificial intelligence for the first time

But the code you told me is not working

To convert veins automatically from treatment to complete after 30 seconds

Only on virtual products

There are two specific payment methods

But it doesn’t work

// Add a 30 second delay for virtual products
add_filter( 'woocommerce_payment_complete_order_status', 'wc_delay_virtual_order', 10, 2 );
function wc_delay_virtual_order( $order_status, $order ) {
    if ( $order->has_downloadable_item() && ! $order->has_status( 'completed' ) ) {
        return 'on-hold'; // change to any other status you wish to use.
    }

    return $order_status;
}
add_action( 'woocommerce_thankyou', 'wc_delay_virtual', 30 );
function wc_delay_virtual( $orderid ) { 

    // Get the order object and check for downloadable items. 
    $order = new WCOrder($orderid); 

    if ( $order->hasDownloadableItem() ) { 

        // Set a 30 second delay for virtual products. 
        sleep(30); 

        // Update the order status to complete. 
        $order->updateStatus('completed');  

    }  
}  
// Add a custom payment gateway for MobileWallet and Reference Code payments. 
add-filter('woocommerce-payment-gateways','wc-add-mobilewallet-referencecode'); 
function wc-add-mobilewallet-referencecode($methods){ 

 $methods[] = 'WCMobileWallet'; // MobileWallet payment gateway class name.  

 $methods[] = 'WCReferenceCode'; // Reference Code payment gateway class name.  

 return $methods;  
}

I tried to add this code inside plugin and inside function.php
I am waiting for your response to solve my problem, thank you

2

Answers


  1. The part of your code posted below has an issue. you cant use - in between function name like wc-add-mobilewallet-referencecode instead it should be wc_add_mobilewallet_referencecode using and underscore "_"

    Your correct code should look so:

    add_filter('woocommerce-payment-gateways','wc_add_mobilewallet_referencecode'); 
    function wc_add_mobilewallet_referencecode($methods){ 
    
     $methods[] = 'WCMobileWallet'; // MobileWallet payment gateway class name.  
    
     $methods[] = 'WCReferenceCode'; // Reference Code payment gateway class name.  
    
     return $methods;  
    }
    

    And since you just need to change order status to complete on downloadable product, just need the code below:

    add_filter( 'woocommerce_payment_complete_order_status', 'wc_delay_virtual_order', 10, 2 );
    function wc_delay_virtual_order( $order_status, $order ) {
        if ( $order->has_downloadable_item() && ! $order->has_status( 'completed' ) ) {
            sleep(30);
            return 'completed'; //Here should be complete.
        }
    
      return $order_status;
    }
    
    Login or Signup to reply.
  2. The requirement doesn’t seem to work for the following reasons:

    1. Payment gateways will have their configuration where after each successful order, the status will be updated based on the configuration.

    2. Making the customer wait 30 seconds on the browser is not recommended.

    3. Even if you forcefully update the order status on the thankyou page hook, what will happen next is if the woocommerce or any third-party plugin or payment plugin has different order statuses, those will be updated as soon as your update is done.

    Nevertheless, there are three possible ways for your requirement.

    1. Search for payment configuration and update the order completion status there. For example, in the case of Novalnet Payment Gateway, I use their configuration, so I need not do anything here.

    2. On the WooCommerce shop backend, you have the settings you can use it. Refer https://woocommerce.com/document/woocommerce-order-status-control/

    3. Based on your business logic, you can set up a cron to update all the virtual orders that aren’t on complete order status. Refer https://developer.wordpress.org/plugins/cron/

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search