我已经托管了带有表order_master
的远程MySQL数据库,并在config.php
中配置了数据库连接,并且该数据库连接已成功连接到远程数据库。在我的本地主机上,我有一个WooCommerce商店,现在无论何时在WooCommerce商店中下订单,我都希望它也保存在远程MySQL数据库order_master
中。
我尝试使用woocommerce_order_status_completed
,但无法正常工作。我在functions.php
文件中添加了以下代码:
add_action('woocommerce_order_status_completed','payment_complete');
function payment_complete($order_id)
{
alert('Function Called..'); //to check, but function not called
global $items;
$order = new WC_Order($order_id);
global $woocommerce;
global $new_wpdb;
$total_amt=WC()->cart->cart_contents_total;
$stmt = "INSERT INTO order_master (payment_amt) VALUES (%d)";
$new_wpdb->query( $stmt,$total_amt);
}
任何人都可以告诉我该怎么做,我对此无能为力。
答案 0 :(得分:2)
使用woocommerce_new_order
挂钩保存收到的任何新订单。试试这种方法,告诉我是否可行。
add_action( 'woocommerce_new_order', 'your_order_details', 1, 1 );
function your_order_details($order_id){
/* Configure your remote DB settings here */
$host = 'localhost';
$user = 'root';
$pass = '';
$db = 'database';
$new_wpdb = mysqli_connect($host, $user, $pass, $db);
if (!$new_wpdb) {
echo mysqli_error($new_wpdb);
}
$order = new WC_Order($order_id);
$order_num = $order_id; // Order ID
$order_amount = get_post_meta($order_id, '_order_total', true); // Amount
$order_master = "INSERT INTO order_master (payment_amt) VALUES ('$order_amount')";
$order_master_exe = mysqli_query($new_wpdb, $order_master);
if ($order_master) {
mysqli_close($new_wpdb);
}