Chirpsuite Community

Find answers, ask questions, and connect with our
community around the world.

Home Community Websites & Blogs How to autocomplete woocommerce virtual orders

Tagged: ,

  • How to autocomplete woocommerce virtual orders

     Chirpsuite updated 1 month ago 1 Member · 1 Post
  • Chirpsuite

    Administrator
    October 23, 2021 at 7:47 pm

    You can achieve this by either using a plugin or adding code snippet to the theme’s function.php file.

    1. Using a plugin

    Install the AutoComplete Orders plugin by going to your WP Admin Dashboard > Plugins > Add New. Then, use the search bar on the top right to search for AutoComplete WooCommerce Orders. Click Install on the plugin and then press Activate to finish the installation. Follow prompts to set virtual orders on autocomplete status.

    2. Using code snippet

    Add the following code to the theme functions.php file. You should use a child theme.

    // Add woocommerce autocomplete for virtual orders
    
    add_filter( 'woocommerce_payment_complete_order_status', 'auto_complete_virtual_orders', 10, 3 );
    
    function auto_complete_virtual_orders( $payment_complete_status, $order_id, $order ) {
    $current_status = $order->get_status();
    // We only want to update the status to 'completed' if it's coming from one of the following statuses:
    $allowed_current_statuses = array( 'on-hold', 'pending', 'failed' );
    
    if ( 'processing' === $payment_complete_status && in_array( $current_status, $allowed_current_statuses ) ) {
    
    $order_items = $order->get_items();
    
    // Create an array of products in the order
    $order_products = array_filter( array_map( function( $item ) {
    // Get associated product for each line item
    return $item->get_product();
    }, $order_items ), function( $product ) {
    // Remove non-products
    return !! $product;
    } );
    
    if ( count( $order_products > 0 ) ) {
    // Check if each product is 'virtual'
    $is_virtual_order = array_reduce( $order_products, function( $virtual_order_so_far, $product ) {
    return $virtual_order_so_far && $product->is_virtual();
    }, true );
    
    if ( $is_virtual_order ) {
    $payment_complete_status = 'completed';
    }
    }
    }
    return $payment_complete_status;
    }
Viewing 1 of 1 replies

Original Post
0 of 0 posts June 2018
Now
error: