yogibook_aury_new/public/hookwordpress.txt
2025-09-01 15:06:58 +02:00

231 lines
7.1 KiB
Plaintext

<?php
add_action('woocommerce_thankyou', 'copy_order_to_tbl', 10, 1);
function copy_order_to_tbl($order_id){
global $wpdb;
if ( ! $order_id )
return;
global $wpdb;
// Define the database credentials for the other database
$other_db_name = 'ideezeur_yogibook';
$other_db_user = 'ideezeur_wp781';
$other_db_password = 'Sk4@Rb]pFQ4(!9)E';
$other_db_host = 'localhost';
$other_db = new wpdb($other_db_user, $other_db_password, $other_db_name, $other_db_host);
$conn = new mysqli($other_db_host, $other_db_user, $other_db_password, $other_db_name);
$tablename = 'copyorder';
$Itemstablename = 'copyorder_items';
$yogibooktableorder = 'orderbook';
$query = $wpdb->prepare("SELECT * FROM $tablename WHERE order_id=%s;", $order_id );
$row = $wpdb->get_row( $query );
if($row)
return;
$order = wc_get_order( $order_id );
$order_data = $order->get_data();
$copyOrder = [];
$copyOrder['order_id'] = $order_data['id'];
$copyOrder['order_parent_id'] = $order_data['parent_id'];
$copyOrder['order_status'] = $order_data['status'];
//$copyOrder['order_currency'] = $order_data['currency'];
//$copyOrder['order_version'] = $order_data['version'];
//$copyOrder['order_payment_method'] = $order_data['payment_method'];
//$copyOrder['order_payment_method_title'] = $order_data['payment_method_title'];
$copyOrder['order_date_created'] = $order_data['date_created']->date('Y-m-d H:i:s');
$copyOrder['order_date_modified'] = $order_data['date_modified']->date('Y-m-d H:i:s');
//$copyOrder['order_discount_total'] = $order_data['discount_total'];
//$copyOrder['order_discount_tax'] = $order_data['discount_tax'];
//$copyOrder['order_shipping_total'] = $order_data['shipping_total'];
//$copyOrder['order_shipping_tax'] = $order_data['shipping_tax'];
$copyOrder['order_total'] = $order_data['total'];
//$copyOrder['order_total_tax'] = $order_data['total_tax'];
$copyOrder['order_customer_id'] = $order_data['customer_id'];
$copyOrder['order_billing_first_name'] = $order_data['billing']['first_name'];
$copyOrder['order_billing_last_name'] = $order_data['billing']['last_name'];
//$copyOrder['order_billing_company'] = $order_data['billing']['company'];
//$copyOrder['order_billing_address_1'] = $order_data['billing']['address_1'];
//$copyOrder['order_billing_address_2'] = $order_data['billing']['address_2'];
//$copyOrder['order_billing_city'] = $order_data['billing']['city'];
//$copyOrder['order_billing_state'] = $order_data['billing']['state'];
//$copyOrder['order_billing_postcode'] = $order_data['billing']['postcode'];
//$copyOrder['order_billing_country'] = $order_data['billing']['country'];
$copyOrder['order_billing_email'] = $order_data['billing']['email'];
//$copyOrder['order_billing_phone'] = $order_data['billing']['phone'];
//$copyOrder['order_shipping_first_name'] = $order_data['shipping']['first_name'];
//$copyOrder['order_shipping_last_name'] = $order_data['shipping']['last_name'];
//$copyOrder['order_shipping_company'] = $order_data['shipping']['company'];
//$copyOrder['order_shipping_address_1'] = $order_data['shipping']['address_1'];
//$copyOrder['order_shipping_address_2'] = $order_data['shipping']['address_2'];
//$copyOrder['order_shipping_city'] = $order_data['shipping']['city'];
//$copyOrder['order_shipping_state'] = $order_data['shipping']['state'];
//$copyOrder['order_shipping_postcode'] = $order_data['shipping']['postcode'];
//$copyOrder['order_shipping_country'] = $order_data['shipping']['country'];
$other_db->insert( $tablename, $copyOrder);
/*
// Check if the email exists in auth_users
$emailcheck = $order_data['billing']['email'];
$first_name = $order_data['billing']['first_name'];
$last_name = $order_data['billing']['last_name'];
$auth_user_query = $conn->prepare("SELECT * FROM auth_users WHERE email = ?;");
$auth_user_query->bind_param("s", $emailcheck);
$auth_user_query->execute();
$auth_user_result = $auth_user_query->get_result();
if ($auth_user_result->num_rows === 0) {
// If email doesn't exist, add it to auth_users
$user_data = array(
'email' => $emailcheck,
'password' => '!Yogibook159',
'first_name' => $first_name,
'last_name' => $last_name,
'role_id' => '2',
'status' => 'Active'
);
$insert_query = "INSERT INTO auth_users (email, password, first_name, last_name, role_id, status, email_verified_at, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, NOW(), NOW(), NOW())";
$insert_stmt = $conn->prepare($insert_query);
$insert_stmt->bind_param("ssssss", $user_data['email'], $user_data['password'], $user_data['first_name'], $user_data['last_name'], $user_data['role_id'], $user_data['status']);
$insert_stmt->execute();
}
*/
/*
$email = $order_data['billing']['email'];
$find_id_query = $conn->prepare("SELECT id FROM auth_users WHERE email = ?;");
$find_id_query->bind_param("s", $emailcheck);
$find_id_query->execute();
$find_id_query->store_result();
if ($find_id_query->num_rows > 0) {
$find_id_query->bind_result($idusercheck);
$find_id_query->fetch();
} else {
$idusercheck = 0;
}
*/
foreach ($order->get_items() as $item_key => $item ):
$copyOrderItem = [];
$item_data = $item->get_data();
//$item_data = $item->get_data();
$copyOrderItemyogi['order_id'] = $order_data['id'];
$copyOrderItemyogi['order_date_created'] = $order_data['date_created']->date('Y-m-d H:i:s');
$copyOrderItemyogi['order_total'] = $order_data['total'];
$copyOrderItemyogi['order_billing_email'] = $order_data['billing']['email'];
$copyOrderItemyogi['product_name'] = $item_data['name'];
$copyOrderItemyogi['product_id'] = $item_data['product_id'];
$copyOrderItemyogi['variation_id'] = $item_data['variation_id'];
$copyOrderItemyogi['quantity'] = $item_data['quantity'];
$copyOrderItemyogi['line_total'] = $item_data['total'];
$copyOrderItemyogi['iduser'] = $idusercheck;
// Check if product has variation.
if ($item_data['variation_id']) {
$product = wc_get_product($item_data['variation_id']);
} else {
$product = wc_get_product($item_data['product_id']);
}
// query to find idservice
$productvar= $item_data['variation_id'];
$checkservice = "SELECT idservice
FROM service
WHERE FIND_IN_SET(?, wpcatalognumber) > 0";
$result = $conn->prepare($checkservice);
$result->bind_param("s", $productvar);
$result->execute();
$result->store_result();
if ($result->num_rows > 0) {
$result->bind_result($idservice);
$result->fetch();
$copyOrderItemyogi['idservice'] = $idservice;
} else {
$copyOrderItemyogi['idservice'] = "0";
}
//end query
$copyOrderItemyogi['cod'] = $product->get_sku();
// Dividi la stringa utilizzando il carattere '_'
$product_sku = $product->get_sku();
$parts = explode('_', $product_sku);
// Assegna i valori alle variabili richieste
$classshort = $parts[0]; // aerial
$quantityclass = $parts[1]; // 4
$dayclass = $parts[2]; // Mar
$copyOrderItemyogi['classshort'] = $classshort;
$copyOrderItemyogi['quantityclass'] = $quantityclass;
$copyOrderItemyogi['nticket'] = $quantityclass;
$copyOrderItemyogi['dayclass'] = $dayclass;
$copyOrderItemyogi['status'] = "pending";
$other_db->insert( $yogibooktableorder, $copyOrderItemyogi);
endforeach;
}
?>