Get Order Number Before Checkout Process Woocomerce

By default order number maked after user checkout but some case maybe you need to create order number for some reason, for example you want add refrences to your payment like sg paynow plugins or other needs, then maybe you need to certain costume.

In this case I have two pages for costume, on checkout url page and chekout/order-pay/number_to_get

First I have idea to get order number from before with query args, I just add to my function like bellow. 


function getOrder_number()
{
		$args = array(
			'status' => array('pending', 'on-hold', 'processing'),
			'limit'  => 1, // limit by one
			'orderby' => 'date',
			'order'   => 'DESC', //sort by new
		);

		$orders = wc_get_orders($args);

		if (!empty($orders)) {
			$order = $orders[0]; // get order first
			$order_number = $order->get_order_number()+1; // get order number and plus one
			echo 'Order Number test: ' . $order_number . '<br>';
		} else {
			$order_number = 'error';
		}
	return $order_number;
	
}

But in this case the method above is not enough, so I must place the conditional for order-pay pages

function getOrder_number2() {
    // Dapatkan URL saat ini
    $url_path = $_SERVER['REQUEST_URI'];

    // Pisahkan path berdasarkan '/'
    $path_parts = explode('/', trim($url_path, '/'));

    // Cari 'order-pay' di URL dan ambil nomor setelahnya
    $order_number = null;
    $key = array_search('order-pay', $path_parts);
    if ($key !== false && isset($path_parts[$key + 1])) {
        $order_number = absint($path_parts[$key + 1]); // Mendapatkan nomor order
    }

    // Jika tidak ditemukan, atau bukan di halaman 'order-pay', gunakan logika default
    if (!$order_number) {
        $args = array(
            'status' => array('pending', 'on-hold', 'processing'),
            'limit'  => 1, // limit by one
            'orderby' => 'date',
            'order'   => 'DESC', //sort by new
        );

        $orders = wc_get_orders($args);

        if (!empty($orders)) {
            $order = $orders[0]; // get order first
            $order_number = $order->get_order_number()+1; // get order number
        } else {
            $order_number = 'error';
        }
    }

    return $order_number;
}

If user on the chekout page, I added plus one but in order-pay page it is not


0 Response to "Get Order Number Before Checkout Process Woocomerce"

Post a Comment

Komentar yang Anda kirim akan terlebih dahulu di moderasi oleh Admin