• Resolved aequinokte

    (@aequinokte)


    Hello everyone

    A customer wants us to enable access to downloads as soon as the order goes “on-hold” since all of the downloads are free. Usually if the order only contains free products the download links are available instantly. Whereas if there’s even a single product that costs let’s say 1$ – you’ll have to wait for the links until someone sets the order status to “processing”.

    Is there a way to get around this? Even if it means simply automatically setting every order to “processing”/”complete”?

    I have rather good knowledge in PHP and the WordPress environment – yet have to get used to how WooCommerce handles certain things.

    Thank you very much for your help in advance! If I need to specify anything more – let me know! ??

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Support Shameem R. a11n

    (@shameemreza)

    Hi @aequinokte

    Is there a way to get around this? Even if it means simply automatically setting every order to “processing”/”complete”?

    You can use the code snippet shared here (or here) or a plugin like WooCommerce Order Status Control, or Autocomplete WooCommerce Orders.

    However, please note that automatically completing orders could potentially lead to issues if you’re selling physical products that need to be shipped, as you might miss the “processing” step.

    I hope this helps! If you have any other questions, feel free to ask.

    Thread Starter aequinokte

    (@aequinokte)

    Hi @shameemreza

    Oh that’s relatively simple! Thank you – yeah I’d set the orders to “processing” since that also can triggers a download-link mail after updating the right download settings.

    Thank you very much!

    Thread Starter aequinokte

    (@aequinokte)

    Hello @shameemreza

    Uhm I’ve realized (after testing) that the shared code snippet hooks on ‘woocommerce_payment_complete_order_status’ which means it updates the order status once payment is complete. But what I would prefer is to make downloadable products available as soon as possible – even before payment since all the downloadables are FREE. Sadly if there is something that costs in the cart it doesn’t make the downloads available until everything is paid.

    Is there a hook for the order status once the order is being placed?

    Plugin Support Shameem R. a11n

    (@shameemreza)

    Hi @aequinokte

    In that case, you may consider using the woocommerce_new_order hook. This hook is triggered as soon as an order is created, which should allow you to set the order status to processing or complete immediately after the order is placed, regardless of whether or not payment has been made.

    Let me give you a clearer example:

    add_action( 'woocommerce_new_order', 'auto_complete_free_virtual_orders', 10, 1 );
    function auto_complete_free_virtual_orders( $order_id ) {
    if ( ! $order_id ) {
    return;
    }
    $order = wc_get_order( $order_id );

    if ( count( $order->get_items() ) > 0 ) {
    foreach ( $order->get_items() as $item ) {
    if ( $item['product_id'] == YOUR_FREE_PRODUCT_ID ) { //replace with your free product ID
    $order->update_status( 'completed' );
    break;
    }
    }
    }
    }

    I hope this helped you in the right direction!

    Thread Starter aequinokte

    (@aequinokte)

    Hey @shameemreza

    Thank you for the article on the lifecycle! That helped a lot to understand on how it actually works. I still find issues in changing the status of the order – but luckily sending completed mails works now. I do not have the time to dissect everything sadly. I hoped to provide more info as to why things work / don’t work. Here’s the solution to my problem though:

    add_action( ‘woocommerce_checkout_order_processed’, ‘auto_complete_free_virtual_orders’, 10, 1 );
    function auto_complete_free_virtual_orders( $order_id ) {
    $order = wc_get_order( $order_id );

    add_action( 'woocommerce_checkout_order_processed', 'mail_free_downloadable_orders', 10, 1 );
    function mail_free_downloadable_orders( $order_id ) {
    if ( ! $order_id ) {
    return;
    }
    $order = wc_get_order( $order_id );

    foreach ( $order->get_items() as $item ) {
    $product = $item->get_product();
    if ( $product->is_downloadable() && $product->get_price() == 0 ) {
    $order->update_status( 'completed' );
    $mailer = WC()->mailer()->get_emails();
    $mailer['WC_Email_Customer_Completed_Order']->trigger( $order_id );
    break;
    }
    }
    }

    The $order->update_status( 'completed' ); probably gets triggered but some other event might prevent that from happening. Either way! Thank you very much for your help!

    Have a nice day!

    Hey, @aequinokte!

    Thank you for sharing the solution to your problem here and contributing to the WooCommerce Community. This might help other users in the future.

    If you need further help with the code, you can visit the WooCommerce Facebook group or the #developers channel of the WooCommerce Community Slack. We’re lucky to have a great community of open-source developers for WooCommerce, and many of our developers hang out there too.

    Please let us know if there’s anything else we can do to help or if you have any questions.

    Have a wonderful day!

Viewing 6 replies - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.