-2

I am setting up a WooCommerce payment plugin. I have created a payment field that should display the order id before payments are received.

I have seen this answer Get the order ID in checkout page before payment process however I do not know how to use the custom function.

public function payment_fields(){
global $woocommerce;

$amount = floatval( preg_replace( '#[^\d.]#', '', $woocommerce->cart->get_cart_total() ) );

///This works if the order has been already placed
$order = new WC_Order($post->ID);
$order_id = $order->get_id();

$shortcode = $this->shortcode;

$steps="Go to Safaricom Menu on your phone<br>
Select M-PESA<br>
Select Lipa na MPESA<br>
Select Pay Bill<br>
Enter Business No: $shortcode<br>
Enter Account No:$order_id<br>
Enter Amount: $amount <br>
Enter the transaction code you received from MPESA in the form below<br>";
echo wpautop( wptexturize( $steps) );

//This add the form field for Pay bill customers 
 woocommerce_form_field( 'mpesaid', array(
                'title'     => __( 'MPESA Reference', 'cwoa-authorizenet-aim' ),
                'type'      => 'text',
                'label'       => 'M-PESA Reference',
                'required'    => true,
                'maxlength'    => '10'
             )
            );
    }

`

7uc1f3r
  • 28,449
  • 17
  • 32
  • 50
hawlast
  • 31
  • 3

1 Answers1

-1

An order is only created when you trigger payment, you can not get order ID before that. Thus, you'll not be able to populate order ID on a checkout field prior to submission.

The process is as follow:

The code below is copied from the class-wc-checkout.php

  $order_id = $this->create_order( $posted_data );
  $order    = wc_get_order( $order_id );
  if ( is_wp_error( $order_id ) ) {
    throw new Exception( $order_id->get_error_message() );
  }
  if ( ! $order ) {
    throw new Exception( __( 'Unable to create order.', 'woocommerce' ) );
  }
  do_action( 'woocommerce_checkout_order_processed', $order_id, $posted_data, $order );
  if ( WC()->cart->needs_payment() ) {
    $this->process_order_payment( $order_id, $posted_data['payment_method'] );
  } else {
    $this->process_order_without_payment( $order_id );
  }

So, basically you can't get an order ID before all of this happens, and as you see, even the approach you're suggesting only happens as a part of process_checkout after order is created.

A suggestion would be to alter the process and trigger order creation once on checkout visit, but this is not tested and I haven't checked all aspects, so you might run into multiple problems.

Ali_k
  • 1,642
  • 1
  • 11
  • 20
  • I have read that from the checkout, I can process the payments on the order-pay page. Now I have // Response handled for payment gateway public function process_payment( $order_id ) { global $woocommerce; $order = new WC_Order( $order_id ); $environment_url = $this->checkurl; $order->update_status('Pending payment', __( 'Awaiting payment', 'woocommerce' )); $woocommerce->cart->empty_cart(); return array('result' => 'success', 'redirect' => $order->get_checkout_payment_url()); } ' – hawlast Aug 14 '19 at 14:49
  • This redirects me to site.com/checkout/order-pay/46/ buy now I can not process the payments...and a user cannot use the same gateway to make payments :) – hawlast Aug 14 '19 at 14:57