Skip to content

Instantly share code, notes, and snippets.

@Acephalia
Last active January 22, 2025 21:41
Show Gist options
  • Select an option

  • Save Acephalia/873f1a6c13e842a8c62aa73a1b976231 to your computer and use it in GitHub Desktop.

Select an option

Save Acephalia/873f1a6c13e842a8c62aa73a1b976231 to your computer and use it in GitHub Desktop.

Revisions

  1. Acephalia revised this gist Jan 10, 2024. 1 changed file with 10 additions and 9 deletions.
    19 changes: 10 additions & 9 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -1,27 +1,28 @@
    /**
    * Auto-complete virtual orders.
    */
    // Auto-complete virtual orders if payment is completed by u/acephaliax
    function auto_complete_virtual_orders($order_id) {
    if (!$order_id) {
    return;
    }

    // Get the order object
    $order = wc_get_order($order_id);

    // Check if the order contains virtual products
    $contains_virtual = false;

    foreach ($order->get_items() as $item) {
    $product = $item->get_product();
    if ($product && $product->is_virtual()) {
    $contains_virtual = true;
    break;
    }
    }

    // If the order contains virtual products, auto-complete it
    if ($contains_virtual) {

    // Check if the payment is completed
    $payment_completed = $order->is_paid();

    // If the order contains virtual products and the payment is completed, auto-complete it
    if ($contains_virtual && $payment_completed) {
    $order->update_status('completed');
    }
    }
  2. Acephalia revised this gist Aug 23, 2023. 1 changed file with 25 additions and 37 deletions.
    62 changes: 25 additions & 37 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -1,40 +1,28 @@
    add_action('woocommerce_thankyou', 'virtual_orders_that_cleanup_after_themselves', 10, 1 );
    function virtual_orders_that_cleanup_after_themselves( $order_id ) {

    if( ! $order_id ) return;

    // Get order
    $order = wc_get_order( $order_id );

    // get order items = each product in the order
    $items = $order->get_items();

    // Set variable
    $only_virtual = true;

    foreach ( $items as $item ) {

    // Get product id
    $product = wc_get_product( $item['product_id'] );

    // Is virtual
    $is_virtual = $product->is_virtual();

    // Is_downloadable
    $is_downloadable = $product->is_downloadable();

    if ( ! $is_virtual && ! $is_downloadable ) {

    $only_virtual = false;

    /**
    * Auto-complete virtual orders.
    */
    function auto_complete_virtual_orders($order_id) {
    if (!$order_id) {
    return;
    }

    // Get the order object
    $order = wc_get_order($order_id);

    // Check if the order contains virtual products
    $contains_virtual = false;

    foreach ($order->get_items() as $item) {
    $product = $item->get_product();
    if ($product && $product->is_virtual()) {
    $contains_virtual = true;
    break;
    }

    }

    // true
    if ( $only_virtual ) {

    $order->update_status( 'completed' );


    // If the order contains virtual products, auto-complete it
    if ($contains_virtual) {
    $order->update_status('completed');
    }
    }
    }
    add_action('woocommerce_order_status_changed', 'auto_complete_virtual_orders', 10, 3);
  3. Acephalia created this gist Mar 17, 2023.
    40 changes: 40 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    add_action('woocommerce_thankyou', 'virtual_orders_that_cleanup_after_themselves', 10, 1 );
    function virtual_orders_that_cleanup_after_themselves( $order_id ) {

    if( ! $order_id ) return;

    // Get order
    $order = wc_get_order( $order_id );

    // get order items = each product in the order
    $items = $order->get_items();

    // Set variable
    $only_virtual = true;

    foreach ( $items as $item ) {

    // Get product id
    $product = wc_get_product( $item['product_id'] );

    // Is virtual
    $is_virtual = $product->is_virtual();

    // Is_downloadable
    $is_downloadable = $product->is_downloadable();

    if ( ! $is_virtual && ! $is_downloadable ) {

    $only_virtual = false;

    }

    }

    // true
    if ( $only_virtual ) {

    $order->update_status( 'completed' );

    }
    }