'Cannot retrieve order_id from database with SQL query
We are trying to update WC orders based on an external script.
However, the script is unable to acquire the order_id for some reason. I tried to run the SQL query manually within the database and it returns the order ID without issue.
I am pretty sure that I am querying the database properly, but I cannot seem to figure out what part is wrong.
Tried with $order=wc_get_order($order_id) with no luck.
Here is the code:
<?php
/*
*/
/*
*/
define("ConfirmKeyword", "CF ETIN");
define("CancelKeyword", "AN ETIN");
/*
Enable/Disable Debug Output - Make Sure is Disabled (set to false) in Production
*/
define("EnableDebugOutput", enable);
if ((isset($_GET["sender"])) and
(isset($_GET["receiver"])) and
(isset($_GET["message"])) and
(isset($_GET["timestamp"])) and
(isset($_GET["network_id"])) and
(isset($_GET["network_name"]))
)
{
/*
Load WordPress
*/
define( 'WP_USE_THEMES', false );
require_once( $_SERVER[ 'DOCUMENT_ROOT' ] . '/wp-load.php' );
/*
The Sender of the SMS (ie. 07xyzzzzzz)
*/
$sender = $_GET["sender"];
/*
The Shortcode on which the SMS was sent (ie. 17xy, 18xy, 37xy, 38xy etc.)
*/
$receiver = $_GET["receiver"];
/*
The Message sent by the Sender to the Shortcode
*/
$message = $_GET["message"];
/*
The Timestamp in which the Message was received on the Shortcode, in UNIX Timestamp format.
*/
$timestamp = $_GET["timestamp"];
/*
The Network ID in which the Sender is located
*/
$network_id = $_GET["network_id"];
$network_name = $_GET["network_name"];
$sql_query = $wpdb->prepare(
'SELECT post_id
FROM wpdq_postmeta
WHERE ((meta_key = "_billing_phone") OR
(meta_key = "_shipping_phone")
) AND
((meta_value = %s) OR
(meta_value = %s) OR
(meta_value = %s) OR
(meta_value = %s) OR
(meta_value = %s)
)
ORDER BY post_id DESC
LIMIT 1
',
$sender, // Search for 07xyzzzzzz
substr($sender, 1), // Search for 7xyzzzzzz
"4".$sender, // Search for 407xyzzzzzz
"004".$sender, // Search for 00407xyzzzzzz
"+4".$sender // Search for +407xyzzzzzz
);
if (EnableDebugOutput == true)
echo "Debug: SQL Query: ".$sql_query."<br />";
$entry_id = $wpdb->get_results($sql_query);
$order_id = $entry_id[0]->post_id;
if (EnableDebugOutput == true)
echo "Debug: Found Order ID: ".$order_id."<br />";
/*
Load Order Data
*/
if (!is_null($order_id))
{
if ($order_id > 0)
{
$order = new WC_Order($order_id);
/*
*
*
* Client Confirms Order by SMS
*
*
*
*/
if (strtoupper(substr(trim(urldecode($message)), 0, strlen(ConfirmKeyword))) == strtoupper(ConfirmKeyword))
{
if (EnableDebugOutput == true)
echo "Debug: Order Confirmed<br />";
/*
Update Order Status as you wish
*/
$order->update_status('on-hold');
/*
Create an Order Note
*/
$note = __("Confirmed by".$sender." la ".$receiver." on ".date("d-m-Y H:i:s", $timestamp)." from ".urldecode($network_name)." text: '".urldecode($message)."'");
/*
Add the Order Note
*/
$order->add_order_note($note);
}
/*
*
*
* Client Cancels Order by SMS
*
*
*
*/
if (strtoupper(substr(trim(urldecode($message)), 0, strlen(CancelKeyword))) == strtoupper(CancelKeyword))
{
if (EnableDebugOutput == true)
echo "Debug: Order Canceled<br />";
/*
Update Order Status as you wish
*/
$order->update_status('cancelled');
/*
Create an Order Note
*/
$note = __("Cancelled by".$sender." la ".$receiver." in ".date("d-m-Y H:i:s", $timestamp)." from newtork CODE".urldecode($network_name)." with the message: '".urldecode($message)."'");
/*
Add the Order Note
*/
$order->add_order_note($note);
}
}
else
{
if (EnableDebugOutput == true)
echo "Debug: Order Not Found<br />";
}
}
else
{
if (EnableDebugOutput == true)
echo "Debug: Order Not Found<br />";
}
echo "Message Successfuly Received.";
}
else
{
echo "Invalid Request.";
}
?>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|