WC_Order::payment_complete PHP Method

payment_complete() public method

Most of the time this should mark an order as 'processing' so that admin can process/post the items. If the cart contains only downloadable items then the order is 'completed' since the admin needs to take no action. Stock levels are reduced at this point. Sales are also recorded for products. Finally, record the date of payment. Order must exist.
public payment_complete ( string $transaction_id = '' ) : boolean
$transaction_id string Optional transaction id to store in post meta.
return boolean success
    public function payment_complete($transaction_id = '')
    {
        try {
            if (!$this->get_id()) {
                return false;
            }
            do_action('woocommerce_pre_payment_complete', $this->get_id());
            if (!empty(WC()->session)) {
                WC()->session->set('order_awaiting_payment', false);
            }
            if ($this->has_status(apply_filters('woocommerce_valid_order_statuses_for_payment_complete', array('on-hold', 'pending', 'failed', 'cancelled'), $this))) {
                $order_needs_processing = false;
                if (sizeof($this->get_items()) > 0) {
                    foreach ($this->get_items() as $item) {
                        if ($item->is_type('line_item') && ($product = $item->get_product())) {
                            $virtual_downloadable_item = $product->is_downloadable() && $product->is_virtual();
                            if (apply_filters('woocommerce_order_item_needs_processing', !$virtual_downloadable_item, $product, $this->get_id())) {
                                $order_needs_processing = true;
                                break;
                            }
                        }
                    }
                }
                if (!empty($transaction_id)) {
                    $this->set_transaction_id($transaction_id);
                }
                $this->set_status(apply_filters('woocommerce_payment_complete_order_status', $order_needs_processing ? 'processing' : 'completed', $this->get_id()));
                $this->set_date_paid(current_time('timestamp'));
                $this->save();
                do_action('woocommerce_payment_complete', $this->get_id());
            } else {
                do_action('woocommerce_payment_complete_order_status_' . $this->get_status(), $this->get_id());
            }
        } catch (Exception $e) {
            return false;
        }
        return true;
    }

Usage Example

 public function process_payment($order_id)
 {
     global $woocommerce;
     $me = wp_get_current_user();
     $order = new WC_Order($order_id);
     if ($me->ID == 0) {
         $woocommerce->add_error(__('Payment error:', 'woothemes') . __('You must be logged in to use this payment method', 'wc_account_funds'));
         return;
     }
     $funds = get_user_meta($me->ID, 'account_funds', true);
     if (!$funds) {
         $funds = 0;
     }
     if ($funds < $order->order_total) {
         $woocommerce->add_error(__('Payment error:', 'woothemes') . __('Insufficient account balance', 'wc_account_funds'));
         return;
     }
     // Payment complete
     $order->payment_complete();
     // deduct amount from account funds
     $new_funds = $funds - $order->order_total;
     update_user_meta($me->ID, 'account_funds', $new_funds);
     // Remove cart
     $woocommerce->cart->empty_cart();
     // Return thank you page redirect
     if (method_exists($order, 'get_checkout_order_received_url')) {
         return array('result' => 'success', 'redirect' => $order->get_checkout_order_received_url());
     } else {
         return array('result' => 'success', 'redirect' => add_query_arg('key', $order->order_key, add_query_arg('order', $order_id, get_permalink(get_option('woocommerce_thanks_page_id')))));
     }
 }
All Usage Examples Of WC_Order::payment_complete
WC_Order