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:
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:
- User selects products and decides to pay for them via "Bank account" payment gateway (one of default gateways in Woocommerce)
- User will see bank account and order number to pay (In my case Order number 84)
- User will send money to given bank account, specifying the order number (In Czech this field is called "variable symbol")
- The plugin connects to the bank via API and queries all transactions on given bank account
- 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
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:
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:
- User selects products and decides to pay for them via "Bank account" payment gateway (one of default gateways in Woocommerce)
- User will see bank account and order number to pay (In my case Order number 84)
- User will send money to given bank account, specifying the order number (In Czech this field is called "variable symbol")
- The plugin connects to the bank via API and queries all transactions on given bank account
- 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
Share Improve this question edited Sep 18, 2018 at 12:08 Pavel Janicek asked Sep 18, 2018 at 7:46 Pavel JanicekPavel Janicek 2123 silver badges14 bronze badges 02 Answers
Reset to default 1global $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.
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