Created
April 5, 2023 10:10
-
-
Save goranefbl/c5e1163bacbaad195560e15a5e8a1a10 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Hook to Woo Tools to add buttons for missing rafs. | |
| * @author WPGens | |
| */ | |
| if ( ! defined( 'ABSPATH' ) ) { | |
| exit; | |
| } | |
| class LOYAL_GENIE_Import { | |
| public function __construct() { | |
| add_action( 'wp_ajax_import_customers', array($this, 'import_customers') ); | |
| add_action( 'wp_ajax_import_orders', array($this, 'import_orders') ); | |
| } | |
| /** | |
| * Callback function for Woocommerce tools button to add missing referrals. | |
| * | |
| * @since 2.0.0 | |
| */ | |
| function import_customers() { | |
| $nonce = $_POST['postNonce']; | |
| $loop = $_POST['loop']; | |
| $total = false; | |
| if ( ! wp_verify_nonce( $nonce, 'import-nonce' ) ) { | |
| die ( 'Busted!'); | |
| } | |
| if($loop == '0') { | |
| $gens_user_query = new WP_User_Query(array( 'meta_key' => 'loyal_genie_created', 'meta_compare' => 'NOT EXISTS', 'number' => -1 )); | |
| $total = $gens_user_query->get_total(); // int, total number of users (not just the first page) | |
| } | |
| $gens_user_query = new WP_User_Query(array( 'meta_key' => 'loyal_genie_created', 'meta_compare' => 'NOT EXISTS', 'number' => 15 )); | |
| $users = $gens_user_query->get_results(); | |
| $formatted_users = []; | |
| foreach ($users as $user) { | |
| $referral_id = get_user_meta($user->ID, "gens_referral_id", true); | |
| $formatted_users[] = [ | |
| 'email' => $user->user_email, | |
| 'firstName' => $user->user_firstname, | |
| 'lastName' => $user->user_lastname, | |
| 'externalUserId' => $user->ID, | |
| 'referralId' => $referral_id ? $referral_id : null | |
| ]; | |
| } | |
| if(count($formatted_users) > 0) { | |
| $api = new Loyal_Genie_API(); | |
| $response = $api->post('import/customers', ['users' => $formatted_users]); | |
| if($response->data === "success") { | |
| foreach($formatted_users as $user) { | |
| update_user_meta( $user['externalUserId'], 'loyal_genie_created', true ); | |
| } | |
| wp_send_json_success( | |
| array('current' => $loop * 15, 'count' => $total ) | |
| ); | |
| } | |
| } else { | |
| wp_send_json_success( | |
| array('current' => $loop * 15, 'count' => $total ) | |
| ); | |
| } | |
| wp_send_json_success( array('error' => 'Something went wrong... no customers have been imported.' )); | |
| } | |
| /** | |
| * Callback function for Woocommerce tools button to add missing referrals. | |
| * | |
| * @since 2.0.0 | |
| */ | |
| function import_orders() { | |
| $nonce = $_POST['postNonce']; | |
| $loop = $_POST['loop']; | |
| $date_created = $_POST['dateCreated']; | |
| $total = false; | |
| if ( ! wp_verify_nonce( $nonce, 'import-nonce' ) ) { | |
| die ( 'Busted!'); | |
| } | |
| if($loop == '0') { | |
| // Get first raf order in the system | |
| $query = new WP_Query( | |
| array( | |
| 'post_type' => 'shop_order', | |
| 'post_status' => array_keys( wc_get_order_statuses() ), | |
| 'meta_query' => array( | |
| 'query_one' => array( | |
| 'key' => '_wpgens_raf_id', | |
| 'compare' => 'EXISTS', // Optional | |
| ), | |
| ), | |
| 'posts_per_page' => 1, | |
| 'orderby' => 'date', | |
| 'order' => 'ASC' | |
| ) | |
| ); | |
| $list_orders = $query->get_posts(); | |
| if ( count($list_orders) === 0 ) { | |
| wp_send_json_success( array('error' => 'No orders to import.' )); | |
| } | |
| $firstOrder = wc_get_order($list_orders[0]->ID); | |
| $firstOrderData = $firstOrder->get_data(); | |
| $date_created = $firstOrderData['date_created']->date('Y-m-d'); | |
| // Get total number of orders after first date: | |
| $args = array( | |
| 'post_type' => 'shop_order', | |
| 'post_status' => array_keys( wc_get_order_statuses() ), | |
| 'meta_query' => array( | |
| 'query' => array( | |
| 'key' => 'loyal_genie_created', | |
| 'compare' => 'NOT EXISTS', // Optional | |
| ), | |
| ), | |
| 'date_query' => array( | |
| array( | |
| 'after' => $date_created, | |
| 'inclusive' => true | |
| ), | |
| ), | |
| 'posts_per_page' => -1, | |
| 'orderby' => 'date', | |
| 'order' => 'ASC' | |
| ); | |
| $totalQuery = new WP_Query( $args ); | |
| $total = count($totalQuery->get_posts()); | |
| if ( $total === 0 ) { | |
| wp_send_json_success( array('error' => 'No orders to import.' )); | |
| } | |
| } | |
| // Get orders created after first raf order | |
| $args = array( | |
| 'post_type' => 'shop_order', | |
| 'post_status' => array_keys( wc_get_order_statuses() ), | |
| 'meta_query' => array( | |
| 'query' => array( | |
| 'key' => 'loyal_genie_created', | |
| 'compare' => 'NOT EXISTS', // Optional | |
| ), | |
| ), | |
| 'date_query' => array( | |
| array( | |
| 'after' => $date_created, | |
| 'inclusive' => true | |
| ), | |
| ), | |
| 'posts_per_page' => 10, | |
| 'orderby' => 'date', | |
| 'order' => 'ASC' | |
| ); | |
| $query = new WP_Query( $args ); | |
| $orders = $query->get_posts(); | |
| $formatted_orders = []; | |
| foreach ($orders as $key => $order) { | |
| $orderObject = wc_get_order($order->ID); | |
| $order_data = $orderObject->get_data(); | |
| $formatted_orders[$key] = [ | |
| 'email' => $order_data['billing']['email'], | |
| 'firstName' => $order_data['billing']['first_name'], | |
| 'lastName' => $order_data['billing']['last_name'], | |
| 'orderTotal' => $order_data['total'], | |
| 'currency' => $order_data['currency'], | |
| 'orderDate' => $order_data['date_created']->date('Y-m-d H:i:s'), | |
| 'externalOrderNumber' => (integer) $order_data['id'], | |
| 'ipAddress' => $order_data['customer_ip_address'], | |
| ]; | |
| if($orderObject->get_user_id() > 0) { | |
| $user_info = get_userdata($orderObject->get_user_id()); | |
| $email = $user_info->user_email; | |
| $formatted_orders[$key]['referralId'] = get_user_meta($orderObject->get_user_id(), "gens_referral_id", true); | |
| $formatted_orders[$key]['externalUserId'] = $orderObject->get_user_id(); | |
| $formatted_orders[$key]['email'] = $email; | |
| } | |
| // only add referrer_id if present | |
| $wpgens_raf_id = $orderObject->get_meta('_wpgens_raf_id', true, 'view'); | |
| if ($wpgens_raf_id != null) { | |
| $wpgens_raf_meta = $orderObject->get_meta('_wpgens_raf_meta', true, 'view'); | |
| $formatted_orders[$key]['referredBy'] = $wpgens_raf_id; | |
| $formatted_orders[$key]['referralCorrect'] = $wpgens_raf_meta['generate'] == "true"; | |
| } | |
| } | |
| if(count($formatted_orders) > 0) { | |
| $api = new Loyal_Genie_API(); | |
| $response = $api->post('import/orders', ['orders' => $formatted_orders]); | |
| if($response->data === "success") { | |
| foreach($formatted_orders as $order) { | |
| update_post_meta( $order['externalOrderNumber'], 'loyal_genie_created', true ); | |
| } | |
| wp_send_json_success( | |
| array('current' => $loop * 10, 'count' => $total, 'dateCreated' => $date_created ) | |
| ); | |
| } | |
| } else { | |
| wp_send_json_success( | |
| array('current' => $loop * 10, 'count' => $total, 'dateCreated' => $date_created ) | |
| ); | |
| } | |
| wp_send_json_success( array('error' => 'Something went wrong... no orders have been imported.' )); | |
| } | |
| } | |
| new LOYAL_GENIE_Import(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment