/* Plugin Name: Dynamic Add Product Plugin URI: https://66loop.com/ Description: Integration with woocommerce Author: 66loop Version: 1.0 */ ini_set('max_execution_time', 0); define('NA_BASE_PATH2', plugin_dir_path(__FILE__)); define('NA_BASE_URL2', plugin_dir_url( __FILE__ )); // require NA_BASE_PATH2 . 'inc/Menu-Controller.php'; // Load WordPress core // require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php' ); // Manually include WooCommerce if not loaded if ( ! class_exists( 'WooCommerce' ) ) { include_once WP_PLUGIN_DIR . '/woocommerce/woocommerce.php'; } if (! wp_next_scheduled ( 'XS2_ScheduleEvent' )) { wp_schedule_event( strtotime('10:00:00'), 'twicedaily', 'XS2_ScheduleEvent'); } add_action( 'XS2_ScheduleEvent', 'xs2_CronCallbackHandler' ); function xs2_CronCallbackHandler(){ // $taxonomy = "product_cat"; // // === 1. Handle Parent Category === // $parent_term = get_term_by('slug', 'formula1',$taxonomy); // xs2_plugin_log2("response cron: ".var_export($parent_term,true)); // $response = xs2_handleStartCron(); // plugin_log2("response cron: ".var_export($response,true)); // if($response['next_request'] !== '') { // CronCallbackHandler($response['totalRecords'], $response['next_request'],$response['perPageFetch']); // } else{ // exit; // } } if(isset($_GET['debug'])){ if($_GET['debug']=="check"){ // xs2_handleStartCron (); // global $wpdb; // $attachments=$wpdb->get_results( $wpdb->prepare("SELECT * FROM $wpdb->posts p WHERE p.post_date LIKE '2022-02%' ")); // plugin_log2("ALL result of one post: ".var_export($attachments[0],true)); // foreach($attachments as $single){ // echo $single->ID; // // plugin_log2("ID ".$single->ID); // wp_delete_attachment( $single->ID); // } } } // add_action('wp_ajax_awin_sync_start', 'handleStart',20); function xs2_handleStartCron() { xs2_plugin_log2("Start."); global $wpdb; $count = 0; $page = 1; $event_ids_from_api = array(); $event_id_count = 0; while(1){ $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => 'https://testapi.xs2event.com/v1/events?page_size=50&page='.$page, CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => '', CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => 'GET', CURLOPT_HTTPHEADER => array( 'Accept: application/json', 'X-Api-Key: b8183752ca85486d9eab76755f77f241' ), )); $response = curl_exec($curl); curl_close($curl); $response = json_decode($response,true); xs2_plugin_log2("Event API Total Records: ".$response['pagination']['total_size']." API Page: ".$page); $page++; // xs2_plugin_log2("API Running: ".var_export($response,true)); for($i = 0;$i 'https://testapi.xs2event.com/v1/tickets?ticket_status=available&stock=gt:0&event_id='.$response['events'][$i]['event_id'].'&page_size=50&page='.$ticketpage, CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => '', CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => 'GET', CURLOPT_HTTPHEADER => array( 'Accept: application/json', 'X-Api-Key: b8183752ca85486d9eab76755f77f241' ), )); $ticket_response = curl_exec($curl); curl_close($curl); $ticket_response = json_decode($ticket_response,true); // xs2_plugin_log2("API Running1: ".$i."=====>".$j."=======>".var_export($ticket_response,true)); for($j = 0;$j 1000){ xs2_plugin_log2("handleStartCron".$count); break; } } $event_id_from_DB = $wpdb->get_col(" SELECT meta_value FROM {$wpdb->postmeta} WHERE meta_key = 'xs2_event_id' "); for($i = 0;$i 'product', // Or your specific post type 'meta_key' => 'xs2_event_id', 'meta_value' => $event_id_from_DB[$i], 'fields' => 'ids', // Only get post IDs ]); wp_delete_post($posts[0], true); xs2_plugin_log2("Delete Post: ".$event_id_from_DB[$i]); }else{ xs2_plugin_log2("Product found!"); } } exit; } function xs2_deleteRecord($totalRecord){ $productAlldata = []; $g=0; $openFile = fopen("http://ultimatemerch4all.66loop.com/Awindatafeed0033new.csv", "r"); while($g < $totalRecord) { $productAlldata[$g]=fgetcsv($openFile); $g++; } fclose($open); $Product= getAllProduct(); if(isset($Product) && $Product){ foreach($Product as $single){ if (array_search($single->SKU, array_column($productAlldata, '11'),true)) { // update the product..... or do nothing.... plugin_log2("found ProductID".$single->SKU); }else{ plugin_log2("Delete ProductID".$single->SKU); delete_all_attached_media($single->post_id); wp_delete_post($single->post_id, true); } } } } function getCountryName($code) { $countries = [ "ARE" => "United Arab Emirates", "AUS" => "Australia", "AUT" => "Austria", "AZE" => "Azerbaijan", "BEL" => "Belgium", "BHR" => "Bahrain", "BRA" => "Brazil", "CAN" => "Canada", "CHN" => "China", "DEU" => "Germany", "DNK" => "Denmark", "ESP" => "Spain", "FRA" => "France", "GBR" => "United Kingdom", "HUN" => "Hungary", "IRL" => "Ireland", "ITA" => "Italy", "JPN" => "Japan", "MCO" => "Monaco", "MEX" => "Mexico", "NLD" => "Netherlands", "PRT" => "Portugal", "QAT" => "Qatar", "SGP" => "Singapore", "USA" => "United States", "SWE" => "Sweden" ]; $code = strtoupper(trim($code)); return $countries[$code] ?? $code; } function xs2_delete_all_attached_media( $post_id ) { plugin_log2("ID ".$post_id); $attachments = get_attached_media( '', $post_id ); foreach ($attachments as $attachment) { wp_delete_attachment( $attachment->ID, 'true' ); } } function xs2_add_products($data,$i,$ticket_response,$j){ if (!function_exists('wp_insert_post')) { require_once ABSPATH . 'wp-admin/includes/post.php'; } if (!function_exists('is_user_logged_in')) { require_once ABSPATH . 'wp-includes/pluggable.php'; } // $event_id = $data['events'][$i]['event_id']; $event_id = $ticket_response['tickets'][$j]['ticket_id']; // $event_name = $data['events'][$i]['event_name']." - ".$ticket_response['tickets'][$j]['ticket_title']; $event_name = $data['events'][$i]['event_name']; $date_start = $data['events'][$i]['date_start']; $date_stop = $data['events'][$i]['date_stop']; $event_status = $data['events'][$i]['event_status']; $tournament_id = $data['events'][$i]['tournament_id']; $tournament_name = $data['events'][$i]['tournament_name']; $venue_id = $data['events'][$i]['venue_id']; $venue_name = $data['events'][$i]['venue_name']; $location_id = $data['events'][$i]['location_id']; $city = $data['events'][$i]['city']; $iso_country = $data['events'][$i]['iso_country']; $latitude = $data['events'][$i]['latitude']; $longitude = $data['events'][$i]['longitude']; $sport_type = $data['events'][$i]['sport_type']; $season = $data['events'][$i]['season']; $tournament_type = $data['events'][$i]['tournament_type']; $date_confirmed = $data['events'][$i]['date_confirmed']; $hometeam_id = $data['events'][$i]['hometeam_id']; $hometeam_name = $data['events'][$i]['hometeam_name']; $visiting_id = $data['events'][$i]['visiting_id']; $visiting_name = $data['events'][$i]['visiting_name']; $created = $data['events'][$i]['created']; $updated = $data['events'][$i]['updated']; $event_description = $data['events'][$i]['event_description']; $min_ticket_price_eur = $data['events'][$i]['min_ticket_price_eur']; $max_ticket_price_eur = $data['events'][$i]['max_ticket_price_eur']; $slug = $data['events'][$i]['slug']; $number_of_tickets = $data['events'][$i]['number_of_tickets']; $meta_content = ''; $address = $venue_name." ".$city." ".$iso_country; $Map = ""; $args = array( 'post_type' => 'product', // Or your custom post type 'meta_key' => 'xs2_event_id', 'meta_value' => $event_id, 'posts_per_page' => 1, ); $posts = get_posts($args); if (!empty($posts)) { // xs2_plugin_log2("Update Product".$posts[0]->ID); // $result = call_openai_api($event_name,$date_start,$venue_name); // xs2_plugin_log2("API Done Event2211: ".var_export($result,true)); // $meta_content = call_openai_api_meta($event_name,$date_start,$venue_name); // $result = call_openai_api($event_name,$date_start,$venue_name); $post_id = $posts[0]->ID; $product_array = array( 'ID' => $post_id, // Required for updating 'post_title' => $event_name, // 'post_content' => $result, 'post_status' => 'publish', 'post_type' => 'product', ); wp_update_post($product_array); }else{ xs2_plugin_log2("Add Product"); $result = call_openai_api($event_name,$date_start,$venue_name); xs2_plugin_log2("API Done Event2211: ".var_export($result,true)); $meta_content = call_openai_api_meta($event_name,$date_start,$venue_name); $product_array=array( 'post_title' => $event_name, 'post_content' => $result, 'post_status' => 'publish', 'post_type' => 'product', ); $post_id = wp_insert_post( $product_array); } update_post_meta( $post_id, 'event_status', $event_status ); if (empty($posts)) { update_post_meta( $post_id, '_yoast_wpseo_metadesc', $meta_content ); } update_post_meta( $post_id, 'date_stop', $date_stop); update_post_meta( $post_id, 'date_start', $date_start); update_post_meta( $post_id, 'city', $city ); update_post_meta( $post_id, 'venue_name', $venue_name ); update_post_meta( $post_id, 'tournament_name', $tournament_name ); update_post_meta( $post_id, 'season', $season ); update_post_meta( $post_id, 'tournament_type', $tournament_type ); update_post_meta( $post_id, 'iso_country', $iso_country); update_post_meta($post_id,'event_location', $Map); update_post_meta( $post_id, 'max_ticket_price_eur', $max_ticket_price_eur ); update_post_meta( $post_id, 'min_ticket_price_eur', $min_ticket_price_eur ); update_post_meta( $post_id, 'visiting_name', $visiting_name ); update_post_meta( $post_id, 'hometeam_name', $hometeam_name ); update_post_meta( $post_id, 'xs2_event_id', $event_id ); update_post_meta( $post_id, 'added_on', date('Y-m-d')); // wp_set_object_terms( $post_id, $ticket_response['tickets'][$j]['category_name'], 'product_cat', $append = false ); $parent_cat_name = (string)$data['events'][$i]['sport_type']; if($parent_cat_name == "soccer"){ $parent_cat_name = "Football"; } update_post_meta( $post_id, 'sport_type', $parent_cat_name ); $hometeam_name = $data['events'][$i]['hometeam_name']; // add_category_and_assign_to_product($post_id, $parent_cat_name, $iso_country, $hometeam_name); if ( class_exists('WooCommerce') && taxonomy_exists('product_cat') ) { xs2_plugin_log2("WooCommerce loaded"); // add_category_and_assign_to_product(808456, 'Clothing1', 'T-Shirts1', 'Graphic Tees1'); $country_name = getCountryName($iso_country); $country_namee = $country_name.'-'.$parent_cat_name; add_category_and_assign_to_product($post_id, $parent_cat_name, $country_namee, $hometeam_name,$tournament_name); add_category_and_assign_to_product_for_other($post_id, $tournament_name, $taxonomy="tournaments"); add_category_and_assign_to_product_for_other($post_id, $hometeam_name, $taxonomy="team"); add_category_and_assign_to_product_for_other($post_id, $country_name, $taxonomy="country-categoryy"); } else { xs2_plugin_log2("WooCommerce not loaded yet"); } update_post_meta( $post_id, 'ticketstatus', $ticket_response['tickets'][$j]['ticket_status'] ); update_post_meta( $post_id, 'category_type', $ticket_response['tickets'][$j]['category_type'] ); update_post_meta( $post_id, 'event_id', $event_id ); update_post_meta( $post_id, 'ticket_title', $ticket_response['tickets'][$j]['ticket_title'] ); update_post_meta( $post_id, 'external_ticket_id', $ticket_response['tickets'][$j]['external_ticket_id'] ); update_post_meta( $post_id, 'currency_code', $ticket_response['tickets'][$j]['currency_code'] ); update_post_meta( $post_id, 'category_name', $ticket_response['tickets'][$j]['category_name'] ); update_post_meta( $post_id, '_stock', $ticket_response['tickets'][$j]['stock'] ); if($ticket_response['tickets'][$j]['stock'] > 0){ update_post_meta( $post_id, '_stock_status', "instock" ); }else{ update_post_meta( $post_id, '_stock_status', "outofstock" ); } update_post_meta( $post_id, '_manage_stock',"yes"); $net_rate = $ticket_response['tickets'][$j]['net_rate']; $five_percent = $net_rate * 0.15; update_post_meta( $post_id, 'original_price',(int)$net_rate/100 ); $net_rate = $ticket_response['tickets'][$j]['net_rate'] + $five_percent; update_post_meta( $post_id, '_regular_price', (int)$net_rate/100 ); update_post_meta( $post_id, '_price', (int)$net_rate/100 ); // wp_set_object_terms( $post_id, $data1[$y][63], 'product_cat', $append = false ); // update_post_meta( $post_id, '_product_image_gallery', $attach_id ); // if($data1[$y][24]!=''){ // Generate_Featured_Image($data1[$y][24] , $post_id); // $Ids[$index] = add_Gallery_Image($data1[$y][24] , $post_id); // $index++; // } // if($data1[$y][58]!=''){ // $Ids[$index] = add_Gallery_Image($data1[$y][58] , $post_id); // $index++; // } // if($data1[$y][67]!=''){ // Generate_Featured_Image($data1[$y][67] , $post_id); // $Ids[$index] = add_Gallery_Image($data1[$y][67] , $post_id); // $index++; // } // if($data1[$y][68]!=''){ // $Ids[$index] = add_Gallery_Image($data1[$y][68] , $post_id); // $index++; // } // if($data1[$y][70]!=''){ // $Ids[$index] = add_Gallery_Image($data1[$y][70] , $post_id); // } // $tags = implode(', ', $Ids); // // plugin_log2("IDS tags:".$tags); // update_post_meta( $post_id, '_product_image_gallery', $tags ); } function xs2_Update_products($product_id,$data1,$result,$y){ $Ids = []; $index = 0; $short_description = 'BUY NOW'; $short_description .= $data1[$y][61]; $product_array=array( 'ID' => $product_id, 'post_title' => $data1[$y][17], 'post_content' => $data1[$y][18], 'post_excerpt' => $short_description, 'post_type' => "product", ); $post_id = wp_update_post( $product_array); wp_set_object_terms( $post_id, 'simple', 'product_type' ); update_post_meta( $post_id, '_price', $data1[$y][30] ); update_post_meta( $post_id, '_stock', $data1[$y][35] ); wp_set_object_terms( $post_id, $data1[$y][63], 'product_cat', $append = false ); if($data1[$y][24]!=''){ $Ids[$index] = add_Gallery_Image($data1[$y][24] , $post_id); $index++; } Generate_Featured_Image($data1[$y][5] , $post_id); if($data1[$y][58]!=''){ $Ids[$index] = add_Gallery_Image($data1[$y][58] , $post_id); $index++; } if($data1[$y][67]!=''){ Generate_Featured_Image($data1[$y][67] , $post_id); $Ids[$index] = add_Gallery_Image($data1[$y][67] , $post_id); $index++; } if($data1[$y][68]!=''){ $Ids[$index] = add_Gallery_Image($data1[$y][68] , $post_id); $index++; } if($data1[$y][70]!=''){ $Ids[$index] = add_Gallery_Image($data1[$y][70] , $post_id); } $tags = implode(', ', $Ids); update_post_meta( $post_id, '_product_image_gallery', $tags ); } function xs2_Generate_Featured_Image( $image_url, $post_id ){ // plugin_log2("Generate_Featured_Image"); $upload_dir = wp_upload_dir(); $image_data = file_get_contents($image_url); $filename = basename($image_url); if(wp_mkdir_p($upload_dir['path'])) $file = $upload_dir['path'] . '/' . $filename; else $file = $upload_dir['basedir'] . '/' . $filename; file_put_contents($file, $image_data); $wp_filetype = wp_check_filetype($filename, null ); $attachment = array( 'post_mime_type' => $wp_filetype['type'], 'post_title' => sanitize_file_name($filename), 'post_content' => '', 'post_status' => 'inherit' ); $attach_id = wp_insert_attachment( $attachment, $file, $post_id ); require_once(ABSPATH . 'wp-admin/includes/image.php'); $attach_data = wp_generate_attachment_metadata( $attach_id, $file ); $res1= wp_update_attachment_metadata( $attach_id, $attach_data ); $res2= set_post_thumbnail( $post_id, $attach_id ); // plugin_log2("Post Id: ".$post_id."Attachment Id: ".$attach_id); } function xs2_add_Gallery_Image( $image_url, $post_id ){ $upload_dir = wp_upload_dir(); $image_data = file_get_contents($image_url); $url = strtok($image_url, '?'); $filename = basename($url); if(wp_mkdir_p($upload_dir['path'])) $file = $upload_dir['path'] . '/' . $filename; else $file = $upload_dir['basedir'] . '/' . $filename; file_put_contents($file, $image_data); $wp_filetype = wp_check_filetype($filename, null ); if($wp_filetype['type'] == false){ $attachment = array( 'post_mime_type' => 'image/jpeg', 'post_title' => sanitize_file_name($filename), 'post_content' => '', 'post_status' => 'inherit' ); } $attachment = array( 'post_mime_type' => $wp_filetype['type'], 'post_title' => sanitize_file_name($filename), 'post_content' => '', 'post_status' => 'inherit' ); $attach_id = wp_insert_attachment( $attachment, $file, $post_id ); require_once(ABSPATH . 'wp-admin/includes/image.php'); $attach_data = wp_generate_attachment_metadata( $attach_id, $file ); $res1= wp_update_attachment_metadata( $attach_id, $attach_data ); // plugin_log2("Attachment Id: ".$attach_id); return $attach_id; } function xs2_term_exist($name){ $id=term_exists( $name, $taxonomy = '', $parent = null ); return $id; } // if ( ! function_xs2_exists( 'plugin_log2' ) ) { function xs2_plugin_log2( $entry, $mode = 'a', $file = 'plugin2' ) { // Get WordPress uploads directory. $upload_dir = wp_upload_dir(); $upload_dir = $upload_dir['basedir']; // If the entry is array, json_encode. if ( is_array( $entry ) ) { $entry = json_encode( $entry ); } // Write the log file. $file = $upload_dir . '/' . $file . '.log'; $file = fopen( $file, $mode ); $bytes = fwrite( $file, current_time( 'mysql' ) . "::" . $entry . "\n" ); fclose( $file ); return $bytes; } // } add_action('woocommerce_payment_complete', 'custom_payment_complete_hook'); function custom_payment_complete_hook($order_id) { $order = wc_get_order($order_id); $billing_email = $order->get_billing_email(); // Example: Log payment completion // error_log("Payment completed for order ID: " . $order_id); xs2_plugin_log2("Payment completed for order ID: " . $order_id); // Loop through each line item (i.e. each product in the order) foreach ( $order->get_items() as $item_id => $item ) { /** @var WC_Order_Item_Product $item **/ // Get the WC_Product object $product = $item->get_product(); $quantity = $item->get_quantity(); $product_id = $product->get_id(); // Get the single xs2_event_id value (returns string or empty) $xs2_event_id = get_post_meta( $product_id, 'xs2_event_id', true ); // If you expect multiple values (array), use false instead of true: // $xs2_event_id_array = get_post_meta( $product_id, 'xs2_event_id', false ); if ( $xs2_event_id ) { // do something with it xs2_plugin_log2( "Product #{$product_id} has event ID: {$xs2_event_id}" ); $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => 'https://testapi.xs2event.com/v1/tickets/'.$xs2_event_id.'?sorting=&page_size=50&page=1', CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => '', CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => 'GET', CURLOPT_HTTPHEADER => array( 'Accept: application/json', 'X-Api-Key: b8183752ca85486d9eab76755f77f241' ), )); $ticket_response = curl_exec($curl); curl_close($curl); $ticket_response = json_decode($ticket_response,true); // $ticket_response['ticket_id']; // $ticket_response['event_id']; // $ticket_response['net_rate']; // $ticket_response['net_rate']; // $ticket_response['net_rate']; // $ticket_response['net_rate']; // $ticket_response['net_rate']; // $booking_email = $billing_email; // $booking_reference = $billing_email; // $ticket_id = $ticket_response['ticket_id']; // $quantity = $quantity; // $net_rate = $ticket_response['net_rate']; // $currency_code = $ticket_response['currency_code']; // $event_id = $ticket_response['event_id']; // $face_value = $ticket_response['face_value']; // $local_currency = $ticket_response['currency_code']; // $payment_method = "invoice"; // your source variables $booking_email = $billing_email; $booking_reference = $billing_email; $invoice_reference = ''; // set as needed $reservation_id = ''; // set as needed $ticket_id = $ticket_response['ticket_id']; $quantity = (string) $quantity; $net_rate = (string) $ticket_response['net_rate']; $currency_code = $ticket_response['currency_code']; $event_id = $ticket_response['event_id']; $face_value = (string) $ticket_response['face_value']; $local_currency = "EUR"; //$ticket_response['currency_code']; $payment_method = 'invoice'; $street_name = $order->get_billing_address_1(); // “street_name” $zip = $order->get_billing_postcode(); // “zip” $last_name = $order->get_billing_last_name(); // “last_name” $first_name = $order->get_billing_first_name(); // “first_name” $city = $order->get_billing_city(); // “city” $contact_email = $order->get_billing_email(); // “contact_email” $contact_phone = $order->get_billing_phone(); // “contact_phone” $country_of_residence = $order->get_billing_country(); // “country_of_residence” $payload = [ 'booking_email' => $billing_email, 'items' => [ [ 'currency_code' => $currency_code, 'net_rate' => $net_rate, 'quantity' => $quantity, 'ticket_id' => $ticket_id, ], ], 'local_currency' => $local_currency, 'notify_client' => true, ]; // encode to JSON $body = json_encode($payload, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => 'https://testapi.xs2event.com/v1/reservations', CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => '', CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_POSTFIELDS =>$body, CURLOPT_HTTPHEADER => array( 'Content-Type: application/json', 'Accept: application/json', 'X-Api-Key: b8183752ca85486d9eab76755f77f241' ), )); $response = curl_exec($curl); $response = json_decode($response,true); curl_close($curl); xs2_plugin_log2( 'Reservations On XS2:: ' . print_r( $response, true ) ); $guest_requirements = xs2_handle_guestdata($event_id); $reservation_id = $response['reservation_id']; if($guest_requirements != 'No Requirements'){ xs2_add_guestdata_to_reservations($reservation_id,$order,$guest_requirements,$quantity,$ticket_id); } if($reservation_id == ''){ exit(); } // build the payload array $payload = [ 'booking_email' => $booking_email, 'booking_reference' => $booking_reference, 'invoice_reference' => $invoice_reference, "guests" => [ [ // "additional_street_name" => "", "city" => $city, "contact_email" => $contact_email, "contact_phone" => $contact_phone, "country_of_residence" => $country_of_residence, // "date_of_birth" => "", "first_name" => $first_name, // "gender" => "", "last_name" => $last_name, // "lead_guest" => "", // "passport_number" => "", // "province" => "", "street_name" => $street_name, "zip" => $zip ], ], 'items' => [ [ 'ticket_id' => $ticket_id, 'quantity' => $quantity, 'net_rate' => $net_rate, 'currency_code' => $currency_code, 'event_id' => $event_id, 'face_value' => $face_value, ], ], 'local_currency' => $local_currency, 'payment_method' => $payment_method, 'reservation_id' => $reservation_id, ]; // Log the full array // xs2_plugin_log2( 'Billing data: ' . print_r( $payload, true ) ); // convert to JSON $jsonPayload = json_encode( $payload, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ); // send it off (e.g. via cURL or your HTTP client) echo $jsonPayload; $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => 'https://testapi.xs2event.com/v1/bookings', CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => '', CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_POSTFIELDS =>$jsonPayload, CURLOPT_HTTPHEADER => array( 'Content-Type: application/json', 'Accept: application/json', 'X-Api-Key: b8183752ca85486d9eab76755f77f241' ), )); $response = curl_exec($curl); $response = json_decode($response,true); curl_close($curl); $qty = $ticket_response['stock'] - $quantity; update_post_meta( $post_id, '_stock', $qty); if($qty > 0){ update_post_meta( $post_id, '_stock_status', "instock" ); }else{ update_post_meta( $post_id, '_stock_status', "outofstock" ); } xs2_plugin_log2( "Booking On XS2:: ".var_export($response,true) ); } else { // no meta found xs2_plugin_log2( "Product #{$product_id} has no xs2_event_id meta." ); } } // Example: Send a custom email // $to = 'irfan1481999@gmail.com'; // $subject = 'Payment Successful'; // $message = 'Thank you for your purchase. Your order ID is ' . $order_id; // wp_mail($to, $subject, $message); // Example: Update custom order meta // $order->update_meta_data('custom_payment_status', 'paid'); // $order->save(); } function add_category_and_assign_to_product($product_id, $parent_cat_name, $sub_cat_name = null, $sub_sub_cat_name = null,$tournament_name = null) { // $taxonomy = "product_cat"; $taxonomy = "product_cat"; $term_ids = []; // === 1. Handle Parent Category === $parent_term = get_term_by('slug', $parent_cat_name, $taxonomy); if (!$parent_term) { $inserted = wp_insert_term($parent_cat_name, $taxonomy, ['slug' => $parent_cat_name]); if (is_wp_error($inserted)) { xs2_plugin_log2("Error inserting parent category: " . $inserted->get_error_message()); return; } $parent_term_id = $inserted['term_id']; } else { $parent_term_id = $parent_term->term_id; } $term_ids[] = $parent_term_id; xs2_plugin_log2("Parent Term ID: " . $parent_term_id); // // === 2. Handle Sub Category === if (!empty($sub_cat_name)) { // $sub_term = get_term_by('slug', $sub_cat_name, $taxonomy); $sub_term = term_exists( $sub_cat_name,$taxonomy, $parent_term_id); if (!$sub_term) { $inserted = wp_insert_term($sub_cat_name, $taxonomy, [ 'slug' => $sub_cat_name, 'parent' => $parent_term_id ]); if (is_wp_error($inserted)) { xs2_plugin_log2("Error inserting sub category: " . $inserted->get_error_message()); return; } $sub_term_id = $inserted['term_id']; } else { $sub_term_id = $sub_term['term_id']; } $term_ids[] = $sub_term_id; xs2_plugin_log2("Sub Term ID: " . $sub_term_id); } if (!empty($sub_sub_cat_name)) { // Use sub_term_id if set, otherwise fall back to parent_term_id $sub_sub_parent_id = isset($sub_term_id) ? $sub_term_id : $parent_term_id; // $sub_sub_term = get_term_by('slug', "teams", $taxonomy); $sub_sub_term = term_exists( "teams",$taxonomy, $sub_sub_parent_id); xs2_plugin_log2("Sub-Sub Term IDdddd: " . "teams"." ==" .$sub_sub_parent_id); if (!$sub_sub_term) { $inserted = wp_insert_term("Teams", $taxonomy, [ 'slug' => "teams", 'parent' => $sub_sub_parent_id ]); if (is_wp_error($inserted)) { xs2_plugin_log2("Error inserting sub-sub category: " . $inserted->get_error_message()); return; } $sub_sub_term_id = $inserted['term_id']; } else { $sub_sub_term_id = $sub_sub_term['term_id']; } $term_ids[] = $sub_sub_term_id; xs2_plugin_log2("Sub-Sub Term ID: " . $sub_sub_term_id); } // === 3. Handle Sub-Sub Category === if (!empty($sub_sub_cat_name)) { // Use sub_term_id if set, otherwise fall back to parent_term_id $sub_sub_parent_id = isset($sub_sub_term_id) ? $sub_sub_term_id : $sub_term_id; // $sub_sub_term = get_term_by('slug', $sub_sub_cat_name, $taxonomy); $sub_sub_term = term_exists( $sub_sub_cat_name,$taxonomy, $sub_sub_parent_id); xs2_plugin_log2("Sub-Sub Term IDdddd: " . $sub_sub_cat_name." ==" .$sub_sub_parent_id); if (!$sub_sub_term) { $inserted = wp_insert_term($sub_sub_cat_name, $taxonomy, [ 'slug' => $sub_sub_cat_name, 'parent' => $sub_sub_parent_id ]); if (is_wp_error($inserted)) { xs2_plugin_log2("Error inserting sub-sub category: " . $inserted->get_error_message()); return; } $sub_sub_term_id = $inserted['term_id']; } else { $sub_sub_term_id = $sub_sub_term['term_id']; } $term_ids[] = $sub_sub_term_id; xs2_plugin_log2("Sub-Sub Term ID: " . $sub_sub_term_id); } if (!empty($tournament_name)) { // Use sub_term_id if set, otherwise fall back to parent_term_id $sub_sub_parent_id = isset($sub_term_id) ? $sub_term_id : $parent_term_id; // $sub_sub_term = get_term_by('slug', "tournaments", $taxonomy); $sub_sub_term = term_exists( "tournaments",$taxonomy, $sub_sub_parent_id); xs2_plugin_log2("Sub-Sub Term IDdddd: " . "Tournaments"." ==" .$sub_sub_parent_id); if (!$sub_sub_term) { $inserted = wp_insert_term("Tournaments", $taxonomy, [ 'slug' => "tournaments", 'parent' => $sub_sub_parent_id ]); if (is_wp_error($inserted)) { xs2_plugin_log2("Error inserting sub-sub category: " . $inserted->get_error_message()); return; } $sub_sub_term_id = $inserted['term_id']; } else { $sub_sub_term_id = $sub_sub_term['term_id']; } $term_ids[] = $sub_sub_term_id; xs2_plugin_log2("Sub-Sub Term ID: " . $sub_sub_term_id); } if (!empty($tournament_name)) { // Use sub_term_id if set, otherwise fall back to parent_term_id $sub_sub_parent_id = isset($sub_sub_term_id) ? $sub_sub_term_id : $sub_sub_term_id; // $sub_sub_term = get_term_by('slug', $tournament_name, $taxonomy); $sub_sub_term = term_exists( $tournament_name,$taxonomy, $sub_sub_parent_id); if (!$sub_sub_term) { $inserted = wp_insert_term($tournament_name, $taxonomy, [ 'slug' => $tournament_name, 'parent' => $sub_sub_parent_id ]); if (is_wp_error($inserted)) { xs2_plugin_log2("Error inserting sub-sub category: " . $inserted->get_error_message()); return; } $sub_sub_term_id = $inserted['term_id']; } else { $sub_sub_term_id = $sub_sub_term['term_id']; } $term_ids[] = $sub_sub_term_id; xs2_plugin_log2("Sub-Sub Term ID: " . $sub_sub_term_id); } // === 4. Assign terms to the product === xs2_plugin_log2("Assigning categories to product ID $product_id: " . var_export($term_ids,true)); $raw_ids = $term_ids; $term_ids = []; foreach ( $raw_ids as $id ) { $term = get_term( (int)$id, 'product_cat' ); if ( $term && !is_wp_error($term) ) { $term_ids[] = (int)$id; } } wp_set_object_terms($product_id, $term_ids, $taxonomy, false); } function add_category_and_assign_to_product_for_other($product_id, $parent_cat_name, $taxonomy) { $taxonomy = $taxonomy; $term_ids = []; // === 1. Handle Parent Category === $parent_term = get_term_by('slug', $parent_cat_name, $taxonomy); if (!$parent_term) { $inserted = wp_insert_term($parent_cat_name, $taxonomy, ['slug' => $parent_cat_name]); if (is_wp_error($inserted)) { xs2_plugin_log2("Error inserting parent category for other: " . $inserted->get_error_message()); return; } $parent_term_id = $inserted['term_id']; } else { $parent_term_id = $parent_term->term_id; } $term_ids[] = $parent_term_id; xs2_plugin_log2("Parent Term ID: " . $parent_term_id); $raw_ids = $term_ids; $term_ids = []; foreach ( $raw_ids as $id ) { $term = get_term( (int)$id, $taxonomy ); if ( $term && !is_wp_error($term) ) { $term_ids[] = (int)$id; } } wp_set_object_terms($product_id, $term_ids, $taxonomy, false); } function xs2_handle_guestdata($event_id){ $url = 'https://testapi.xs2event.com/v1/events/'.$event_id.'/guestdata'; $method = 'GET'; $headers = [ 'Content-Type: application/json', 'Accept: application/json', 'X-Api-Key: b8183752ca85486d9eab76755f77f241' ]; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); $response = curl_exec($ch); $response = json_decode($response,true); curl_close($ch); if(sizeof($response['guest_data_requirements']) > 0){ xs2_plugin_log2("Event Guest Data Requirement: " . var_export($response,true)); return $response; }else{ xs2_plugin_log2("Event Guest Data Requirement: " . var_export($response,true)); return "No Requirements"; } } function xs2_add_guestdata_to_reservations($reservation_id,$order,$guest_requirements,$quantity,$ticket_id){ $guest_requirements_filtered = parseGuestDataRequirements($guest_requirements); $all_guest = $guest_requirements_filtered['condition'][0] == "all_persons" ? false : true; $condition = in_array($guest_requirements_filtered['condition'][1], ['pre_checkout', 'pre_download']) ? true : false; $street_name = $order->get_billing_address_1(); // “street_name” $zip = $order->get_billing_postcode(); // “zip” $last_name = $order->get_billing_last_name(); // “last_name” $first_name = $order->get_billing_first_name(); // “first_name” $city = $order->get_billing_city(); // “city” $contact_email = $order->get_billing_email(); // “contact_email” $contact_phone = $order->get_billing_phone(); // “contact_phone” $country_of_residence = $order->get_billing_country(); // “country_of_residence” $url = 'https://testapi.xs2event.com/v1/reservations/'.$reservation_id.'/guestdata'; $method = 'POST'; $headers = [ 'Content-Type: application/json', 'Accept: application/json', 'X-Api-Key: b8183752ca85486d9eab76755f77f241' ]; $body = json_encode([ "items" => [ [ "guests" => [ [ "city" => $city, "conditions" => [ "lead_guest" => $guest_requirements_filtered['condition'][0], "pre_checkout" => $condition ], "contact_email" => $contact_email, "contact_phone" => $contact_phone, "country_of_residence" => $country_of_residence, "date_of_birth" => "1991-01-30", "first_name" => $first_name, "gender" => "male", "last_name" => $last_name, "lead_guest" => $all_guest, "passport_number" => "ABC123456", "province" => "Groningen", "street_name" => $street_name, "zip" => $zip ] ], "quantity" => $quantity, "ticket_id" => $ticket_id ] ] ]); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_POSTFIELDS, $body); $response = curl_exec($ch); xs2_plugin_log2("Add Guest Data Reservation: " . var_export($response,true)); // $response = json_decode($response,true); curl_close($ch); } function parseGuestDataRequirements(array $input) { $requirements = $input['guest_data_requirements'] ?? []; // Identify condition keywords $conditions = ['pre_checkout', 'pre_download', 'lead_guest', 'all_persons']; $foundConditions = array_intersect($conditions, $requirements); // Build condition key for reference table sort($foundConditions); // to ensure consistent key order $conditionKey = implode(',', $foundConditions); return [ 'condition' => $foundConditions, ]; } function show_stay22_iframe() { return get_field('event_location'); } add_shortcode('event_location', 'show_stay22_iframe');