vapvarun icon

Hook to change order status to "completed" for digital or downloadable products

vapvarun | PRO | 09/28/23 11:31:17 AM UTC | 0 ⭐ | 1544 👁️ | Never ⏰ | [woocommerce]
PHP |

1.12 KB

|

None

|

0 👍

/

0 👎

// Hook to change order status to "completed" for digital or downloadable products
add_action('woocommerce_payment_complete', 'vap_mark_order_complete_for_digital_or_downloadable_products');
 
function vap_mark_order_complete_for_digital_or_downloadable_products($order_id) {
    // Check if the order exists
    if (!$order_id) return;
 
    // Get the order object
    $order = wc_get_order($order_id);
 
    // Check if the order contains at least one digital or downloadable product
    $has_digital_or_downloadable_product = false;
 
    foreach ($order->get_items() as $item) {
        $product_id = $item->get_product_id();
        $product = wc_get_product($product_id);
 
        // Check if the product is digital or downloadable
        if ($product && ($product->is_downloadable() || $product->is_virtual())) {
            $has_digital_or_downloadable_product = true;
            break;
        }
    }
 
    // If the order contains at least one digital or downloadable product, set the order status to "completed"
    if ($has_digital_or_downloadable_product) {
        $order->update_status('completed');
    }
}
 

Comments