0

I am facing a challenge. I need to check if Woocommerce Order Number will equal Post ID. I know that this is true like 90% of times, but I need bulletproof way to check this. See example where this is not true and Order number and Post ID do differ:

woocommerce different order number and post ID

Things to consider:

  • My plugin may be installed at a time where no orders are created in Woocommerce yet
  • I found this occasion on wordpress multisite, but I know that there are also plugins for sequential order numbers. And there is a lot of them, so I cannot just check for existence of one or two plugins.
  • I need to know this because I am developing a plugin to check payments made manually by the customers. So I know that the payment will be identified by Order Number and not by Order ID (which is always equal to Post ID)

Edit: Expected business flow of my plugin

The plugin I have developed is "offline payments" for bank account payment. So, the flow is this:

  1. User selects products and decides to pay for them via "Bank account" payment gateway (one of default gateways in Woocommerce)
  2. User will see bank account and order number to pay (In my case Order number 84)
  3. User will send money to given bank account, specifying the order number (In Czech this field is called "variable symbol")
  4. The plugin connects to the bank via API and queries all transactions on given bank account
  5. What I need to decide = If payment is received with order number 84, it means that Order ID 7257 (see picture above) is paid.

In most of the sites applies that Order ID == Order Number. However here not and I need to have logic how to decide this. So far I am just thinking about adding a checkbox to the plugin...

Thanks a lot for any ideas

0
global $post;

$order = wc_get_order($post->ID);    

if(!empty($order) && $post->ID == $order->get_id()){
//do things
}

This should work. The first check is because the order with the post ID does not have to exists just as you said.

  • This looks cool, thanks! However if there is no order created, I am afraid that this will never become true... And thats the hard challenge I am facing :) So far the site the order ID and order number do differ is just one so I can program specific type of a plugin, but having this solved would be holy grail :) – Pavel Janicek Sep 18 '18 at 11:01
  • You asked for an option to check whether an existing order has a post ID equal the order ID. What else do you want exactly? I'm not sure I understand. – TotalCare Sep 18 '18 at 11:11
  • I wish it was so easy. :) I need to check if there is some sequential order numbers in place, even if there are no orders yet. I think I will solve it by adding one checkbox to the plugin which will decide if it should check order number or order ID. I will add expected flow to the question itself – Pavel Janicek Sep 18 '18 at 12:03
0

I know I had specific question, so I am answering it after several days of search.

Woocommerce has its own query which I decided to reuse:

public function pending_orders(){
        $query = array(
        'status' => 'on-hold',
        'limit'=>-1
        );
        $pending_orders = wc_get_orders( $query );

        $found_variables = array();
        foreach ($pending_orders as $order){
          $foundit = array(
            'id' =>  $order->id,
            'number' => trim( str_replace( '#', '', $order->get_order_number() ) )
           );
           array_push($found_variables,$foundit);
        }
        return $found_variables;
      }

The above will push same number to both 'id' and 'number' column of my $found_variables if it applies that Order Number == Order ID. But I do not care, because it will also solve the problem if Order Number != Order ID.

Later on, the plugin queries the payments:

public function search_haystack(){

        $pending_variables = $this->pending_orders();
        if (empty($pending_variables)){
            return;
        }
        $downloader = new Downloader($options['fio_token']);
        $transactionList = $downloader->downloadSince(new \DateTime('-1 week'));
        $allTransactions = $transactionList->getTransactions();
        foreach ($allTransactions as $transaction) {
            if ($transaction->getAmount()>0){
                if($key = array_search($transaction->getVariableSymbol(), array_column($pending_variables, 'number'))){
                    $this->maybe_complete_payment($pending_variables[$key]['id'],$transaction->getAmount(),$transaction->getCurrency());
                }
            }
        }
      }

And you guessed it, for completing payment, I need Order ID, which I am sending to the function:

public function maybe_complete_payment($variable,$paid_amount,$currency){
        $order = new WC_Order($variable);
        if (($order->get_total() == $paid_amount) and ($currency == $order->get_currency())){
          $order -> payment_complete();
          $order -> add_order_note('Successfully paired via Bank Transfer');
        }
      }

All of the code is not yet tested, but I feel it should be working

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.