Advertisement
vapvarun

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

Sep 28th, 2023
1,103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.12 KB | None | 0 0
  1. // Hook to change order status to "completed" for digital or downloadable products
  2. add_action('woocommerce_payment_complete', 'vap_mark_order_complete_for_digital_or_downloadable_products');
  3.  
  4. function vap_mark_order_complete_for_digital_or_downloadable_products($order_id) {
  5.     // Check if the order exists
  6.     if (!$order_id) return;
  7.  
  8.     // Get the order object
  9.     $order = wc_get_order($order_id);
  10.  
  11.     // Check if the order contains at least one digital or downloadable product
  12.     $has_digital_or_downloadable_product = false;
  13.  
  14.     foreach ($order->get_items() as $item) {
  15.         $product_id = $item->get_product_id();
  16.         $product = wc_get_product($product_id);
  17.  
  18.         // Check if the product is digital or downloadable
  19.         if ($product && ($product->is_downloadable() || $product->is_virtual())) {
  20.             $has_digital_or_downloadable_product = true;
  21.             break;
  22.         }
  23.     }
  24.  
  25.     // If the order contains at least one digital or downloadable product, set the order status to "completed"
  26.     if ($has_digital_or_downloadable_product) {
  27.         $order->update_status('completed');
  28.     }
  29. }
  30.  
Tags: woocommerce
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement