PluginProbe
WPBot – AI ChatBot for Live Support, Lead Generation, AI Services / 1.6.0
WPBot – AI ChatBot for Live Support, Lead Generation, AI Services v1.6.0
8.7.5 8.7.4 8.7.3 8.7.2 8.7.1 8.7.0 8.6.9 8.6.8 8.6.7 8.6.6 8.6.5 8.6.4 8.6.2 8.6.1 8.6.0 8.5.9 8.5.8 8.5.7 8.5.6 8.5.5 8.5.4 8.5.3 8.5.2 8.5.0 8.4.9 All 531 releases
chatbot / functions.php

functions.php in WPBot – AI ChatBot for Live Support, Lead Generation, AI Services 1.6.0, at functions.php

1,825 lines 100.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @param $type
4 * Display wpwBot Icon ball
5 */
6 if (!defined('ABSPATH')) exit; // Exit if accessed directly
7 add_action('wp_footer', 'wp_chatbot_load_footer_html');
8 function wp_chatbot_load_footer_html(){
9 if (get_option('disable_wp_chatbot') != 1 && wp_chatbot_load_controlling() == true) {
10 ?>
11 <style>
12 <?php if(get_option('wp_chatbot_custom_css')!="") {
13 echo get_option('wp_chatbot_custom_css');
14 }
15 ?>
16 </style>
17 <?php if (get_option('qcld_wb_chatbot_change_bg') == 1) {
18 if (get_option('qcld_wb_chatbot_board_bg_path') != "") {
19 $qcld_wb_chatbot_board_bg_path = get_option('qcld_wb_chatbot_board_bg_path');
20 } else {
21 $qcld_wb_chatbot_board_bg_path = QCLD_wpCHATBOT_IMG_URL . 'background/background.png';
22 }
23 ?>
24 <style>
25 .wp-chatbot-container {
26 background-image: url(<?php echo $qcld_wb_chatbot_board_bg_path;
27 ?>) !important;
28 }
29 </style>
30 <?php }
31 $wp_chatbot_enable_rtl = "";
32 if (get_option('enable_wp_chatbot_rtl')) {
33 $wp_chatbot_enable_rtl .= "wp-chatbot-rtl";
34 }
35 $wp_chatbot_enable_mobile_screen = "";
36 if (get_option('enable_wp_chatbot_mobile_full_screen')==1) {
37 $wp_chatbot_enable_mobile_screen .= "wp-chatbot-mobile-full-screen";
38 }
39 ?>
40 <div id="wp-chatbot-chat-container" class="<?php echo $wp_chatbot_enable_rtl .' '.$wp_chatbot_enable_mobile_screen; ?>">
41 <div id="wp-chatbot-integration-container">
42 <div class="wp-chatbot-integration-button-container">
43 <?php if (get_option('enable_wp_chatbot_skype_floating_icon') == 1) { ?>
44 <a href="skype:<?php echo get_option('enable_wp_chatbot_skype_id'); ?>?chat"><span
45 class="inetegration-skype-btn" title="<?php _e('Skype', 'wpchatbot'); ?>"> </span></a>
46 <?php } ?>
47 <?php if (get_option('enable_wp_chatbot_floating_whats') == 1) { ?>
48 <a href="<?php echo 'https://api.whatsapp.com/send?phone=' . get_option('qlcd_wp_chatbot_whats_num'); ?>"
49 target="_blank"><span class="intergration-whats"
50 title="<?php _e('WhatsApp', 'wpchatbot'); ?>"></span></a>
51 <?php } ?>
52 <?php if (get_option('enable_wp_chatbot_floating_viber') == 1) { ?>
53 <a href="<?php echo 'https://live.viber.com/#/' . get_option('qlcd_wp_chatbot_viber_acc'); ?>"
54 target="_blank"><span class="intergration-viber"
55 title="<?php _e('Viber', 'wpchatbot'); ?>"></span></a>
56 <?php } ?>
57 <?php if (get_option('enable_wp_chatbot_floating_phone') == 1 && get_option('qlcd_wp_chatbot_phone') != "") { ?>
58 <a href="tel:<?php echo get_option('qlcd_wp_chatbot_phone'); ?>"><span
59 class="intergration-phone"
60 title="<?php _e('Phone', 'wpchatbot'); ?>"> </span></a>
61 <?php } ?>
62 <?php if (get_option('enable_wp_chatbot_floating_link') == 1 && get_option('qlcd_wp_chatbot_weblink') != "") { ?>
63 <a href="<?php echo get_option('qlcd_wp_chatbot_weblink'); ?>" target="_blank"><span
64 class="intergration-weblink" title="<?php _e('Web Link', 'wpchatbot'); ?>"></span></a>
65 <?php } ?>
66 </div>
67 </div>
68 <?php
69 //Get wpcommerce cart
70 global $wpcommerce;
71 $cart_items_number = $wpcommerce->cart->cart_contents_count;
72 $qcld_wb_chatbot_theme = get_option('qcld_wb_chatbot_theme');
73 if (file_exists(QCLD_wpCHATBOT_PLUGIN_DIR_PATH . '/templates/' . $qcld_wb_chatbot_theme . '/style.css')) {
74 wp_register_style('qcld-wp-chatbot-style', plugins_url(basename(plugin_dir_path(__FILE__)) . '/templates/' . $qcld_wb_chatbot_theme . '/style.css', basename(__FILE__)), '', QCLD_wpCHATBOT_VERSION, 'screen');
75 wp_enqueue_style('qcld-wp-chatbot-style');
76 }
77 if (file_exists(QCLD_wpCHATBOT_PLUGIN_DIR_PATH . '/templates/' . $qcld_wb_chatbot_theme . '/template.php')) {
78 require_once(QCLD_wpCHATBOT_PLUGIN_DIR_PATH . '/templates/' . $qcld_wb_chatbot_theme . '/template.php');
79 } else {
80 echo "<h2>" . __('No wpWBot Theme Found!', 'wpchatbot') . "</h2>";
81 }
82 ?>
83 <?php
84 if (get_option('disable_wp_chatbot_notification') != 1) {
85 ?>
86 <div id="wp-chatbot-notification-container" class="wp-chatbot-notification-container">
87 <div class="wp-chatbot-notification-controller"> <span class="wp-chatbot-notification-close">
88 <?php _e('X', 'wpchatbot'); ?>
89 </span></div>
90 <?php
91 $testingTip="";
92 if (get_option('wp_chatbot_agent_image') == "custom-agent.png") {
93 $wp_chatbot_custom_agent_path = get_option('wp_chatbot_custom_agent_path');
94 } else if (get_option('wp_chatbot_agent_image') != "custom-agent.png") {
95 $wp_chatbot_custom_agent_path = QCLD_wpCHATBOT_IMG_URL . get_option('wp_chatbot_agent_image');
96 } else {
97 $wp_chatbot_custom_agent_path = QCLD_wpCHATBOT_IMG_URL . 'custom-agent.png';
98 }
99 ?>
100 <div class="wp-chatbot-notification-agent-profile">
101 <div class="wp-chatbot-notification-widget-avatar" ><img
102 src="<?php echo $wp_chatbot_custom_agent_path; ?>" alt=""></div>
103 <div class="wp-chatbot-notification-welcome"><?php echo randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_welcome'))) . ' <strong>' . get_option('qlcd_wp_chatbot_host') . '</strong>'; ?></div>
104 </div>
105 <?php
106 //update_option('qlcd_wp_chatbot_notifications','Welcome to WpBot');
107 $notifications = qcld_wb_chatbot_func_str_replace(unserialize(get_option('qlcd_wp_chatbot_notifications')));
108
109 ?>
110
111 <div class="wp-chatbot-notification-message"><?php echo $notifications[0]; ?></div>
112 </div>
113 <?php } ?>
114 <!--wp-chatbot-board-container-->
115 <div id="wp-chatbot-ball" class="">
116 <div class="wp-chatbot-ball">
117 <div class="wp-chatbot-ball-animator wp-chatbot-ball-animation-switch"></div>
118 <?php
119 if (get_option('wp_chatbot_icon') == "custom.png") {
120 $wp_chatbot_custom_icon_path = get_option('wp_chatbot_custom_icon_path');
121 } else if (get_option('wp_chatbot_icon') != "custom.png") {
122 $wp_chatbot_custom_icon_path = QCLD_wpCHATBOT_IMG_URL . get_option('wp_chatbot_icon');
123 } else {
124 $wp_chatbot_custom_icon_path = QCLD_wpCHATBOT_IMG_URL . 'custom.png';
125 }
126 ?>
127 <img src="<?php echo $wp_chatbot_custom_icon_path; ?>"
128 alt="wpChatIcon">
129
130 </div>
131 </div>
132 <?php
133 $fb_app_id = get_option('qlcd_wp_chatbot_fb_app_id');
134 $fb_page_id = get_option('qlcd_wp_chatbot_fb_page_id');
135 $fb_mgs_color = get_option('qlcd_wp_chatbot_fb_color') != '' ? get_option('qlcd_wp_chatbot_fb_color') : '#0084ff';
136 $fb_mgs_in = get_option('qlcd_wp_chatbot_fb_in_msg') != '' ? get_option('qlcd_wp_chatbot_fb_in_msg') : 'You are welcome';
137 $fb_mgs_out = get_option('qlcd_wp_chatbot_fb_out_msg') != '' ? get_option('qlcd_wp_chatbot_fb_out_msg') : 'You are not logged in';
138 if (get_option('enable_wp_chatbot_messenger') == 1 && get_option('enable_wp_chatbot_messenger_floating_icon') == 1) {
139 ?>
140 <!-- wp-chatbot-board-container-->
141 <script>
142 window.fbAsyncInit = function () {
143 FB.init({
144 appId: '<?php echo $fb_app_id;?>',
145 autoLogAppEvents: true,
146 xfbml: true,
147 version: 'v2.12'
148 });
149 };
150 (function (d, s, id) {
151 var js, fjs = d.getElementsByTagName(s)[0];
152 if (d.getElementById(id)) return;
153 js = d.createElement(s);
154 js.id = id;
155 js.src = 'https://connect.facebook.net/en_US/sdk.js';
156 fjs.parentNode.insertBefore(js, fjs);
157 }(document, 'script', 'facebook-jssdk'));
158 </script>
159 <div class="fb-customerchat"
160 page_id="<?php echo $fb_page_id; ?>"
161 greeting_dialog_display="hide"
162 theme_color="<?php echo $fb_mgs_color; ?>"
163 logged_in_greeting="<?php echo $fb_mgs_in; ?>"
164 logged_out_greeting="<?php echo $fb_mgs_out; ?>"></div>
165 <?php
166 }
167 ?>
168 <!--container-->
169 <!--wp-chatbot-ball-wrapper-->
170 </div>
171 <audio id="wp-chatbot-proactive-audio" <?php if (get_option('enable_wp_chatbot_sound_initial') == 1) {
172 echo "autoplay";
173 } ?>>
174 <source src="<?php echo QCLD_wpCHATBOT_IMG_URL; ?>pro-active.mp3">
175 </source>
176 </audio>
177 <?php
178 }else{
179 ?>
180 <script>
181 var openingHourIsFn = 1;
182 </script>
183 <?php
184 }
185 }
186 //wp_chatbot load control handler.
187 function wp_chatbot_load_controlling(){
188 $wp_chatbot_load = true;
189 if (wp_chatbot_is_mobile() && get_option('disable_wp_chatbot_on_mobile') == 1) {
190 $wp_chatbot_load = false;
191 }
192 if (get_option('wp_chatbot_show_home_page') == 'off' && is_home()) {
193 $wp_chatbot_load = false;
194 }
195 if (get_option('wp_chatbot_show_posts') == 'off' && 'post' == get_post_type()) {
196 $wp_chatbot_load = false;
197 }
198 if (get_option('wp_chatbot_show_pages') == 'off') {
199 $wp_chatbot_select_pages = unserialize(get_option('wp_chatbot_show_pages_list'));
200 if (is_page() && !empty($wp_chatbot_select_pages)) {
201 if (in_array(get_the_ID(), $wp_chatbot_select_pages) == true) {
202 $wp_chatbot_load = true;
203 } else {
204 $wp_chatbot_load = false;
205 }
206 }
207 }
208 if (get_option('wp_chatbot_show_wpcommerce') == 'off') {
209 if (is_shop() || is_cart() || is_checkout() || 'product' == get_post_type()) {
210 $wp_chatbot_load = false;
211 }
212 }
213 //load wpwbot shortcode template and prevent default wpwbot from footer.
214 if (is_page()) {
215 $page_id = get_the_ID();
216 $page = get_post($page_id);
217 if (has_shortcode($page->post_content, 'wpwbot')) {
218 $wp_chatbot_load = false;
219 }
220 }
221 //Opening Hours for wpwBot.
222 if (get_option('enable_wp_chatbot_opening_hour') == 1) {
223 if(wp_chatbot_check_opening_hours()==false){
224 $wp_chatbot_load = false;
225 }else{
226 $wp_chatbot_load = true;
227 }
228 }
229
230 return $wp_chatbot_load;
231 }
232 //checking Devices
233 function wp_chatbot_is_mobile(){
234 $useragent = $_SERVER['HTTP_USER_AGENT'];
235 if (preg_match('/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i', $useragent) || preg_match('/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i', substr($useragent, 0, 4))) {
236 return true;
237 } else {
238 return false;
239 }
240 }
241 //Checking wpwbot opening hour
242 function wp_chatbot_check_opening_hours(){
243 $curent_day=strtolower(date('l',strtotime(current_time( 'mysql' ))));
244 $current_time=date('H:i',strtotime(current_time( 'mysql')));
245 $is_wpwbot_open =false;
246 if(get_option('wpwbot_hours')) {
247 $wpwbot_times = unserialize(get_option('wpwbot_hours'));
248 if (isset($wpwbot_times[$curent_day])) {
249 $day_times = $wpwbot_times[$curent_day];
250 if (!empty($day_times)) {
251 foreach ($day_times as $day_time) {
252 if(strtotime($current_time) > strtotime($day_time[0]) && strtotime($current_time) < strtotime($day_time[1]) ){
253 $is_wpwbot_open=true;
254 }
255 }
256 }
257 }
258 }
259 return $is_wpwbot_open;
260 }
261 //wpwBot shortcode.
262 add_shortcode('wpwbot', 'wp_chatbot_short_code');
263 function wp_chatbot_short_code($atts = []){
264 ob_start();
265 wp_chatbot_shortcode_dom($atts);
266 $content = ob_get_clean();
267 return $content;
268 }
269 function wp_chatbot_shortcode_dom($atts){
270 //Defaults & Set Parameters for shortcode
271 extract(shortcode_atts(
272 array(
273 'template' => '01',
274 ), $atts
275 ));
276 ?>
277 <style>
278 <?php if(get_option('wp_chatbot_custom_css')!=""){echo get_option('wp_chatbot_custom_css'); } ?>
279 </style>
280 <?php if (get_option('qcld_wb_chatbot_change_bg') == 1) {
281 if (get_option('qcld_wb_chatbot_board_bg_path') != "") {
282 $qcld_wb_chatbot_board_bg_path = get_option('qcld_wb_chatbot_board_bg_path');
283 } else {
284 $qcld_wb_chatbot_board_bg_path = QCLD_wpCHATBOT_IMG_URL . 'background/background.png';
285 }
286 ?>
287 <style>
288 .wp-chatbot-container {
289 background: url(<?php echo $qcld_wb_chatbot_board_bg_path ;?>) no-repeat top right !important;
290 }
291 </style>
292 <?php }
293 $wp_chatbot_enable_rtl = "";
294 if (get_option('enable_wp_chatbot_rtl')) {
295 $wp_chatbot_enable_rtl .= "wp-chatbot-rtl";
296 }
297 ?>
298 <div id="wp-chatbot-chat-container" class="<?php echo $wp_chatbot_enable_rtl; ?>">
299 <?php
300 //Get wpcommerce cart
301 global $wpcommerce;
302 $cart_items_number = $wpcommerce->cart->cart_contents_count;
303 $qcld_wb_chatbot_theme = 'template-' . $template;
304 if (file_exists(QCLD_wpCHATBOT_PLUGIN_DIR_PATH . '/templates/' . $qcld_wb_chatbot_theme . '/style.css')) {
305 wp_register_style('qcld-wp-chatbot-style', plugins_url(basename(plugin_dir_path(__FILE__)) . '/templates/' . $qcld_wb_chatbot_theme . '/style.css', basename(__FILE__)), '', QCLD_wpCHATBOT_VERSION, 'screen');
306 wp_enqueue_style('qcld-wp-chatbot-style');
307 }
308 if (file_exists(QCLD_wpCHATBOT_PLUGIN_DIR_PATH . '/templates/' . $qcld_wb_chatbot_theme . '/template.php')) {
309 require_once(QCLD_wpCHATBOT_PLUGIN_DIR_PATH . '/templates/' . $qcld_wb_chatbot_theme . '/template.php');
310 } else {
311 echo "<h2>" . __('No wpWBot Theme Found!', 'wpchatbot') . "</h2>";
312 }
313 ?>
314 <?php if (get_option('disable_wp_chatbot') != 1): ?>
315 <div id="wp-chatbot-notification-container" class="wp-chatbot-notification-container">
316 <div class="wp-chatbot-notification-controller"> <span class="wp-chatbot-notification-close">X</span> </div>
317 <?php
318 if (get_option('wp_chatbot_custom_agent_path') != "" && get_option('wp_chatbot_agent_image') == "custom-agent.png") {
319 $wp_chatbot_custom_icon_path = get_option('wp_chatbot_custom_agent_path');
320 } else if (get_option('wp_chatbot_custom_agent_path') != "" && get_option('wp_chatbot_agent_image') != "custom-agent.png") {
321 $wp_chatbot_custom_icon_path = QCLD_wpCHATBOT_IMG_URL . get_option('wp_chatbot_agent_image');
322 } else {
323 $wp_chatbot_custom_icon_path = QCLD_wpCHATBOT_IMG_URL . 'custom-agent.png';
324 }
325 ?>
326 <div class="wp-chatbot-notification-agent-profile">
327 <div class="wp-chatbot-notification-widget-avatar"><img
328 src="<?php echo $wp_chatbot_custom_icon_path; ?>" alt=""></div>
329 <div class="wp-chatbot-notification-welcome"><?php echo randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_welcome'))) . ' <strong>' . get_option('qlcd_wp_chatbot_host') . '</strong>'; ?></div>
330 </div>
331 <div class="wp-chatbot-notification-message"><?php echo randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_notifications'))); ?></div>
332 </div>
333 <!--wp-chatbot-board-container-->
334 <div id="wp-chatbot-ball" class="">
335 <div class="wp-chatbot-ball">
336 <div class="wp-chatbot-ball-animator wp-chatbot-ball-animation-switch"></div>
337 <?php
338 if (get_option('wp_chatbot_custom_icon_path') != "" && get_option('wp_chatbot_icon') == "custom.png") {
339 $wp_chatbot_custom_icon_path = get_option('wp_chatbot_custom_icon_path');
340 } else if (get_option('wp_chatbot_custom_icon_path') != "" && get_option('wp_chatbot_icon') != "custom.png") {
341 $wp_chatbot_custom_icon_path = QCLD_wpCHATBOT_IMG_URL . get_option('wp_chatbot_icon');
342 } else {
343 $wp_chatbot_custom_icon_path = QCLD_wpCHATBOT_IMG_URL . 'custom.png';
344 }
345 ?>
346 <img src="<?php echo $wp_chatbot_custom_icon_path; ?>"
347 alt="wpChatIcon"> <!--<span class="wp-chatbot-ball-cart-items"><?php echo $cart_items_number; ?></span>--> </div>
348 </div>
349 <!-- wp-chatbot-board-container-->
350 <?php endif; ?>
351 <!--container-->
352 <!--wp-chatbot-ball-wrapper-->
353 </div>
354 <?php } ?>
355 <?php
356 //Create shortcode for wpwBot for pages.
357 add_shortcode('wpbot-page', 'wp_chatbot_page_short_code');
358 function wp_chatbot_page_short_code(){
359 ob_start();
360 wp_chatbot_page_dom();
361 $content = ob_get_clean();
362 return $content;
363 }
364 function wp_chatbot_page_dom(){ ?>
365 <style>
366 <?php if(get_option('wp_chatbot_custom_css')!=""){echo get_option('wp_chatbot_custom_css'); } ?>
367 </style>
368 <?php
369 //Get wpcommerce cart
370 global $wpcommerce;
371 $cart_items_number = $wpcommerce->cart->cart_contents_count;
372 $qcld_wb_chatbot_theme = get_option('qcld_wb_chatbot_theme');
373 $wp_chatbot_enable_rtl = "";
374 if (get_option('enable_wp_chatbot_rtl') == 1) {
375 $wp_chatbot_enable_rtl .= "wp-chatbot-rtl";
376 }
377 if (file_exists(QCLD_wpCHATBOT_PLUGIN_DIR_PATH . '/templates/' . $qcld_wb_chatbot_theme . '/shortcode.php')) {
378 require_once(QCLD_wpCHATBOT_PLUGIN_DIR_PATH . '/templates/' . $qcld_wb_chatbot_theme . '/shortcode.php');
379 } else {
380 echo "<h2>" . __('No WPBot ShortCode Theme Found!', 'wpchatbot') . "</h2>";
381 }
382 }
383 //shortcode for wpWBot mobile app
384 add_shortcode('wpwbot_app', 'wp_chatbot_mobile_app_short_code');
385 function wp_chatbot_mobile_app_short_code(){ ?>
386 <style>
387 <?php if(get_option('wp_chatbot_custom_css')!=""){echo get_option('wp_chatbot_custom_css'); } ?>
388 </style>
389 <?php if (get_option('qcld_wb_chatbot_change_bg') == 1) {
390 if (get_option('qcld_wb_chatbot_board_bg_path') != "") {
391 $qcld_wb_chatbot_board_bg_path = get_option('qcld_wb_chatbot_board_bg_path');
392 } else {
393 $qcld_wb_chatbot_board_bg_path = QCLD_wpCHATBOT_IMG_URL . 'background/background.png';
394 }
395 ?>
396 <style>
397 .wp-chatbot-container {
398 background: url(<?php echo $qcld_wb_chatbot_board_bg_path ;?>) no-repeat top right !important;
399 }
400 </style>
401 <?php }
402 $wp_chatbot_enable_rtl = "";
403 if (get_option('enable_wp_chatbot_rtl')) {
404 $wp_chatbot_enable_rtl .= "wp-chatbot-rtl";
405 }
406 ?>
407 <div id="wp-chatbot-chat-app-shortcode-container" class="<?php echo $wp_chatbot_enable_rtl; ?>">
408 <?php
409 // keep traking app template.
410 $template_app = 'yes';
411 //Get wpcommerce cart
412 global $wpcommerce;
413 $cart_items_number = $wpcommerce->cart->cart_contents_count;
414 //Handling shortcode enqeue and remove features part.
415 define('wpCOMMERCE', true);
416 wp_enqueue_script('jquery');
417 wp_enqueue_script('wpcommerce', array('jquery'));
418 wp_enqueue_script('wc-cart', array('jquery', 'wpcommerce'));
419 wp_enqueue_script('wc-address-i18n');
420 wp_enqueue_script('wc-country-select');
421 wp_enqueue_script('wc-checkout', array('jquery', 'wpcommerce', 'wc-address-i18n', 'wc-country-select'));
422 remove_action('wpcommerce_cart_collaterals', 'wpcommerce_cross_sell_display');
423 // add the action
424 if (isset($_GET['from']) && $_GET['from'] == 'app') {
425 if (!isset($_COOKIE['from_app'])) {
426 setcookie('from_app', 'yes', time() + 3600);
427 }
428 }
429 $qcld_wb_chatbot_theme = get_option('qcld_wb_chatbot_theme');
430 if (file_exists(QCLD_wpCHATBOT_PLUGIN_DIR_PATH . '/templates/' . $qcld_wb_chatbot_theme . '/template.php')) {
431 require_once(QCLD_wpCHATBOT_PLUGIN_DIR_PATH . '/templates/' . $qcld_wb_chatbot_theme . '/template.php');
432 } else {
433 echo "<h2>" . __('No WPBot Theme Found!', 'wpchatbot') . "</h2>";
434 }
435 ?>
436 </div>
437 <?php
438 }
439
440 /**
441 * wpwBot Search keyword product
442 */
443 add_action('wp_ajax_qcld_wb_chatbot_keyword', 'qcld_wb_chatbot_keyword');
444 add_action('wp_ajax_nopriv_qcld_wb_chatbot_keyword', 'qcld_wb_chatbot_keyword');
445 function qcld_wb_chatbot_keyword(){
446 $keyword = sanitize_text_field($_POST['keyword']);
447 $product_per_page = get_option('qlcd_wp_chatbot_ppp') != '' ? get_option('qlcd_wp_chatbot_ppp') : 10;
448 if (get_option('qlcd_wp_chatbot_search_option') == 'standard') {
449 $product_orderby = get_option('qlcd_wp_chatbot_product_orderby') != '' ? get_option('qlcd_wp_chatbot_product_orderby') : 'title';
450 $product_order = get_option('qlcd_wp_chatbot_product_order') != '' ? get_option('qlcd_wp_chatbot_product_order') : 'ASC';
451 //Merging all query together.
452 $argu_params = array(
453 'post_type' => 'product',
454 'post_status' => 'publish',
455 'posts_per_page' => $product_per_page,
456 'orderby' => $product_orderby,
457 'order' => $product_order,
458 's' => $keyword,
459 );
460 /******
461 *WP Query Operation to get products.*
462 *******/
463 $product_query = new WP_Query($argu_params);
464 $product_num = $product_query->post_count;
465 //Getting total product number by string.
466 $total_argu = array('post_type' => 'product', 's' => $keyword, 'posts_per_page' => 100);
467 $total_query = new WP_Query($total_argu);
468 $total_product_num = $total_query->post_count;
469 $html = '<div class="wp-chatbot-products-area">';
470 $_pf = new WC_Product_Factory();
471 //repeating the products
472 if ($product_num > 0) {
473 $html .= '<ul class="wp-chatbot-products">';
474 while ($product_query->have_posts()) : $product_query->the_post();
475 $product = $_pf->get_product(get_the_ID());
476 if (wp_chatbot_product_controlling(get_the_ID()) == true) {
477 $html .= '<li class="wp-chatbot-product">';
478 $html .= '<a target="_blank" href="' . get_permalink(get_the_ID()) . '" wp-chatbot-pid= "' . get_the_ID() . '" title="' . esc_attr($product->post->post_title ? $product->post->post_title : get_the_ID()) . '">';
479 $html .= get_the_post_thumbnail(get_the_ID(), 'shop_catalog') . '
480 <div class="wp-chatbot-product-summary">
481 <div class="wp-chatbot-product-table">
482 <div class="wp-chatbot-product-table-cell">
483 <h3 class="wp-chatbot-product-title">' . $product->post->post_title . '</h3>
484 <div class="price">' . $product->get_price_html() . '</div>';
485 $html .= ' </div>
486 </div>
487 </div></a>
488 </li>';
489 }
490 endwhile;
491 wp_reset_postdata();
492 $html .= '</ul>';
493 if ($total_product_num > $product_per_page && $product_per_page > 0 ) {
494 $html .= '<p style="text-align: center"><button type="button" id="wp-chatbot-loadmore" data-offset="' . $product_per_page . '" data-search-type="product" data-search-term="' . $keyword . '" >' . randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_load_more'))) . ' <span id="wp-chatbot-loadmore-loader"></span></button> </p>';
495 }
496 }
497 $html .= '</div>';
498 } else if (get_option('qlcd_wp_chatbot_search_option') == 'advanced') {
499 $result = wpwBot_Search::factory()->search($keyword);
500 $products = $result['products'];
501 $product_num = count($result['products']);
502 $total_product_num = $result['total_products'];
503 $more_product_ids = implode(",", $result['more_ids']);
504 $html = '<div class="wp-chatbot-products-area">';
505 $_pf = new WC_Product_Factory();
506 //repeating the products
507 if ($product_num > 0) {
508 $html .= '<ul class="wp-chatbot-products">';
509 foreach ($products as $product) {
510 if (wp_chatbot_product_controlling($product->get_id()) == true) {
511 $html .= '<li class="wp-chatbot-product">';
512 $html .= '<a target="_blank" href="' . get_permalink($product->get_id()) . '" wp-chatbot-pid= "' . $product->get_id() . '" title="' . esc_attr($product->get_title()) . '">';
513 $html .= get_the_post_thumbnail($product->get_id(), 'shop_catalog') . '
514 <div class="wp-chatbot-product-summary">
515 <div class="wp-chatbot-product-table">
516 <div class="wp-chatbot-product-table-cell">
517 <h3 class="wp-chatbot-product-title">' . $product->get_title() . '</h3>
518 <div class="price">' . $product->get_price_html() . '</div>';
519 $html .= ' </div></div></div></a></li>';
520 }
521 }
522 $html .= '</ul>';
523 if ($total_product_num > $product_per_page && $product_per_page > 0) {
524 $html .= '<p style="text-align: center"><button type="button" id="wp-chatbot-loadmore" data-offset="' . $product_per_page . '" data-search-type="product" data-search-term="' . $more_product_ids . '" >' . randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_load_more'))) . ' <span id="wp-chatbot-loadmore-loader"></span></button> </p>';
525 }
526 }
527 $html .= '</div>';
528 }
529 $response = array('html' => $html, 'product_num' => $total_product_num, 'per_page' => $product_per_page);
530 echo wp_send_json($response);
531 wp_die();
532 }
533 /**
534 * wpwBot Categories
535 */
536 add_action('wp_ajax_qcld_wb_chatbot_category', 'qcld_wb_chatbot_category');
537 add_action('wp_ajax_nopriv_qcld_wb_chatbot_category', 'qcld_wb_chatbot_category');
538 function qcld_wb_chatbot_category(){
539 $category_type="common";
540 if (get_option('wp_chatbot_show_parent_category') != "") {
541 $terms = get_terms('product_cat', array('parent' => 0, 'hide_empty' => true, 'fields' => 'all'));
542
543 } else {
544 $terms = get_terms('product_cat', array('hide_empty' => true, 'fields' => 'all'));
545 }
546 $html = "";
547 foreach ($terms as $term) {
548 $child_terms=get_terms('product_cat', array('parent' => $term->term_id, 'hide_empty' => true, 'fields' => 'all'));
549 if(get_option('wp_chatbot_show_sub_category')==1 && count($child_terms) >0){
550 $category_type="hasChilds";
551 }
552 $html .= '<span class="qcld-chatbot-product-category" data-category-type="' . $category_type . '" data-category-slug="' . $term->slug . '" data-category-id="' . $term->term_id . '">' . $term->name . '</span>';
553 }
554 echo wp_send_json($html);
555 wp_die();
556 }
557 /**
558 * wpwBot Sub categories
559 */
560 add_action('wp_ajax_qcld_wb_chatbot_sub_category', 'qcld_wb_chatbot_sub_category');
561 add_action('wp_ajax_nopriv_qcld_wb_chatbot_sub_category', 'qcld_wb_chatbot_sub_category');
562 function qcld_wb_chatbot_sub_category(){
563 $parent_id = stripslashes($_POST['parent_id']);
564 $terms = get_terms('product_cat', array('parent' => $parent_id, 'hide_empty' => true, 'fields' => 'all'));
565 $html = "";
566 foreach ($terms as $term) {
567 $html .= '<span class="qcld-chatbot-product-category" data-category-type="common" data-category-slug="' . $term->slug . '" data-category-id="' . $term->term_id . '">' . $term->name . '</span>';
568 }
569 echo wp_send_json($html);
570 wp_die();
571 }
572 /**
573 * wpwBot category product
574 */
575 add_action('wp_ajax_qcld_wb_chatbot_category_products', 'qcld_wb_chatbot_category_products');
576 add_action('wp_ajax_nopriv_qcld_wb_chatbot_category_products', 'qcld_wb_chatbot_category_products');
577 function qcld_wb_chatbot_category_products(){
578 $category_id = stripslashes($_POST['category']);
579 $product_per_page = get_option('qlcd_wp_chatbot_ppp') != '' ? get_option('qlcd_wp_chatbot_ppp') : 10;
580 $product_orderby = get_option('qlcd_wp_chatbot_product_orderby') != '' ? get_option('qlcd_wp_chatbot_product_orderby') : 'title';
581 $product_order = get_option('qlcd_wp_chatbot_product_order') != '' ? get_option('qlcd_wp_chatbot_product_order') : 'ASC';
582 //Merging all query together.
583 $argu_params = array(
584 'post_type' => 'product',
585 'post_status' => 'publish',
586 'ignore_sticky_posts' => 1,
587 'orderby' => $product_orderby,
588 'order' => $product_order,
589 'posts_per_page' => $product_per_page,
590 'tax_query' => array(
591 array(
592 'taxonomy' => 'product_cat',
593 'field' => 'term_id',
594 'terms' => $category_id,
595 'operator' => 'IN'
596 )
597 )
598 );
599 /******
600 *WP Query Operation to get products.*
601 *******/
602 $product_query = new WP_Query($argu_params);
603 $product_num = $product_query->post_count;
604 //Getting total product number by string.
605 $total_argu = array(
606 'post_type' => 'product',
607 'post_status' => 'publish',
608 'posts_per_page' => 100,
609 'tax_query' => array(
610 array(
611 'taxonomy' => 'product_cat',
612 'field' => 'term_id',
613 'terms' => $category_id,
614 'operator' => 'IN'
615 )
616 )
617 );
618 $total_query = new WP_Query($total_argu);
619 $total_product_num = $total_query->post_count;
620 $_pf = new WC_Product_Factory();
621 //repeating the products
622 $html = '';
623 if ($product_num > 0) {
624 $html .= '<div class="wp-chatbot-products-area">';
625 $html .= '<ul class="wp-chatbot-products">';
626 while ($product_query->have_posts()) : $product_query->the_post();
627 $product = $_pf->get_product(get_the_ID());
628 //$qcld_thumb = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'shop_thumbnail' );
629 $html .= '<li class="wp-chatbot-product">';
630 $html .= '<a class="wp-chatbot-product-url" wp-chatbot-pid= "' . get_the_ID() . '" target="_blank" href="' . get_permalink(get_the_ID()) . '" title="' . esc_attr($product->get_title() ? $product->get_title() : get_the_ID()) . '">';
631 $html .= get_the_post_thumbnail(get_the_ID(), 'shop_catalog') . '
632 <div class="wp-chatbot-product-summary">
633 <div class="wp-chatbot-product-table">
634 <div class="wp-chatbot-product-table-cell">
635 <h3 class="wp-chatbot-product-title">' . $product->get_title() . '</h3>
636 <div class="price">' . $product->get_price_html() . '</div>';
637 $html .= ' </div>
638 </div>
639 </div></a>
640 </li>';
641 endwhile;
642 wp_reset_postdata();
643 $html .= '</ul>';
644 if ($total_product_num > $product_per_page && $product_per_page >0) {
645 $html .= '<p style="text-align: center"><button type="button" id="wp-chatbot-loadmore" data-offset="' . $product_per_page . '" data-search-type="category" data-search-term="' . $category_id . '" >' . randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_load_more'))) . ' <span id="wp-chatbot-loadmore-loader"></span></button> </p>';
646 }
647 $html .= '</div>';
648 } else {
649 $html = '';
650 }
651 $response = array('html' => $html, 'product_num' => $total_product_num, 'per_page' => $product_per_page);
652 echo wp_send_json($response);
653 wp_die();
654 }
655 /**
656 * wpwBot latest, featured, recent product
657 */
658 add_action('wp_ajax_qcld_wb_chatbot_featured_products', 'qcld_wb_chatbot_featured_products');
659 add_action('wp_ajax_nopriv_qcld_wb_chatbot_featured_products', 'qcld_wb_chatbot_featured_products');
660 function qcld_wb_chatbot_featured_products(){
661 $product_per_page = get_option('qlcd_wp_chatbot_ppp') != '' ? get_option('qlcd_wp_chatbot_ppp') : 10;
662 $product_orderby = get_option('qlcd_wp_chatbot_product_orderby') != '' ? get_option('qlcd_wp_chatbot_product_orderby') : 'title';
663 $product_order = get_option('qlcd_wp_chatbot_product_order') != '' ? get_option('qlcd_wp_chatbot_product_order') : 'ASC';
664 //get featured products query.
665 $argu_params = array('post_status' => 'publish',
666 'posts_per_page' => $product_per_page,
667 'post_type' => 'product',
668 'post_status' => 'publish',
669 'tax_query' => array(array('taxonomy' => 'product_visibility', 'field' => 'name', 'terms' => 'featured'))
670 );
671 /******
672 *WP Query Operation to get products.*
673 *******/
674 $product_query = new WP_Query($argu_params);
675 $product_num = $product_query->post_count;
676 //Getting total product number by string.
677 $total_argu = array('post_status' => 'publish',
678 'posts_per_page' => 100,
679 'post_type' => 'product',
680 'post_status' => 'publish',
681 'tax_query' => array(array('taxonomy' => 'product_visibility', 'field' => 'name', 'terms' => 'featured',),)
682 );
683 $total_query = new WP_Query($total_argu);
684 $total_product_num = $total_query->post_count;
685 $html = '<div class="wp-chatbot-products-area">';
686 $_pf = new WC_Product_Factory();
687 //repeating the products
688 if ($product_num > 0) {
689 //$html .= '<p>sdf sdfdsf : '.$asdfdf.'</p>';
690 $html .= '<ul class="wp-chatbot-products">';
691 while ($product_query->have_posts()) : $product_query->the_post();
692 $product = $_pf->get_product(get_the_ID());
693 $html .= '<li class="wp-chatbot-product">';
694 $html .= '<a class="wp-chatbot-product-url" wp-chatbot-pid= "' . get_the_ID() . '" target="_blank" href="' . get_permalink(get_the_ID()) . '" title="' . esc_attr($product->get_title() ? $product->get_title() : get_the_ID()) . '">';
695 $html .= get_the_post_thumbnail(get_the_ID(), 'shop_catalog') . '
696 <div class="wp-chatbot-product-summary">
697 <div class="wp-chatbot-product-table">
698 <div class="wp-chatbot-product-table-cell">
699 <h3 class="wp-chatbot-product-title">' . $product->get_title() . '</h3>
700 <div class="price">' . $product->get_price_html() . '</div>';
701 $html .= ' </div>
702 </div>
703 </div></a>
704 </li>';
705 endwhile;
706 wp_reset_postdata();
707 $html .= '</ul>';
708 if ($total_product_num > $product_per_page) {
709 $html .= '<p style="text-align: center"><button type="button" id="wp-chatbot-loadmore" data-offset="' . $product_per_page . '" data-search-type="product" data-search-term="featured" >' . randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_load_more'))) . ' <span id="wp-chatbot-loadmore-loader"></span></button> </p>';
710 }
711 }
712 $html .= '</div>';
713 $response = array('html' => $html, 'product_num' => $total_product_num, 'per_page' => $product_per_page);
714 echo wp_send_json($response);
715 wp_die();
716 }
717 //Product display controll
718 function wp_chatbot_product_controlling($product_id){
719 $display_product = true;
720 //Controlling Out of Stock product display from back end.
721 $_pf = new WC_Product_Factory();
722 $product = $_pf->get_product($product_id);
723 if ($product->manage_stock == 'yes' && $product->stock_quantity <= 0 && get_option('wp_chatbot_exclude_stock_out_product') == 1) {
724 $display_product = false;
725 }
726 return $display_product;
727 }
728 //Get Sale products
729 add_action('wp_ajax_qcld_wb_chatbot_sale_products', 'qcld_wb_chatbot_sale_products');
730 add_action('wp_ajax_nopriv_qcld_wb_chatbot_sale_products', 'qcld_wb_chatbot_sale_products');
731 function qcld_wb_chatbot_sale_products(){
732 $product_per_page = get_option('qlcd_wp_chatbot_ppp') != '' ? get_option('qlcd_wp_chatbot_ppp') : 10;
733 $product_orderby = get_option('qlcd_wp_chatbot_product_orderby') != '' ? get_option('qlcd_wp_chatbot_product_orderby') : 'title';
734 $product_order = get_option('qlcd_wp_chatbot_product_order') != '' ? get_option('qlcd_wp_chatbot_product_order') : 'ASC';
735 //get sale products query.
736 $argu_params = array(
737 'post_type' => 'product',
738 'post_status' => 'publish',
739 'posts_per_page' => $product_per_page,
740 'meta_query' => array(
741 'relation' => 'OR',
742 array( // Simple products type
743 'key' => '_sale_price',
744 'value' => 0,
745 'compare' => '>',
746 'type' => 'numeric'
747 ),
748 array( // Variable products type
749 'key' => '_min_variation_sale_price',
750 'value' => 0,
751 'compare' => '>',
752 'type' => 'numeric'
753 )
754 )
755 );
756 /******
757 *WP Query Operation to get products.*
758 *******/
759 $product_query = new WP_Query($argu_params);
760 $product_num = $product_query->post_count;
761 //Getting total product number by string.
762 $total_argu = array(
763 'post_type' => 'product',
764 'post_status' => 'publish',
765 'posts_per_page' => 100,
766 'meta_query' => array(
767 'relation' => 'OR',
768 array( // Simple products type
769 'key' => '_sale_price',
770 'value' => 0,
771 'compare' => '>',
772 'type' => 'numeric'
773 ),
774 array( // Variable products type
775 'key' => '_min_variation_sale_price',
776 'value' => 0,
777 'compare' => '>',
778 'type' => 'numeric'
779 )
780 )
781 );
782 $total_query = new WP_Query($total_argu);
783 $total_product_num = $total_query->post_count;
784 $html = '<div class="wp-chatbot-products-area">';
785 $_pf = new WC_Product_Factory();
786 //repeating the products
787 if ($product_num > 0) {
788 //$html .= '<p>sdf sdfdsf : '.$asdfdf.'</p>';
789 $html .= '<ul class="wp-chatbot-products">';
790 while ($product_query->have_posts()) : $product_query->the_post();
791 $product = $_pf->get_product(get_the_ID());
792 $html .= '<li class="wp-chatbot-product">';
793 $html .= '<a class="wp-chatbot-product-url" wp-chatbot-pid= "' . get_the_ID() . '" target="_blank" href="' . get_permalink(get_the_ID()) . '" title="' . esc_attr($product->get_title() ? $product->get_title() : get_the_ID()) . '">';
794 $html .= get_the_post_thumbnail(get_the_ID(), 'shop_catalog') . '
795 <div class="wp-chatbot-product-summary">
796 <div class="wp-chatbot-product-table">
797 <div class="wp-chatbot-product-table-cell">
798 <h3 class="wp-chatbot-product-title">' . $product->get_title() . '</h3>
799 <div class="price">' . $product->get_price_html() . '</div>';
800 $html .= ' </div>
801 </div>
802 </div></a>
803 </li>';
804 endwhile;
805 wp_reset_postdata();
806 $html .= '</ul>';
807 if ($total_product_num > $product_per_page) {
808 $html .= '<p style="text-align: center"><button type="button" id="wp-chatbot-loadmore" data-offset="' . $product_per_page . '" data-search-type="product" data-search-term="sale" >' . randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_load_more'))) . ' <span id="wp-chatbot-loadmore-loader"></span></button> </p>';
809 }
810 }
811 $html .= '</div>';
812 $response = array('html' => $html, 'product_num' => $total_product_num, 'per_page' => $product_per_page);
813 echo wp_send_json($response);
814 wp_die();
815 }
816 //load more
817 add_action('wp_ajax_qcld_wb_chatbot_load_more', 'qcld_wb_chatbot_load_more');
818 add_action('wp_ajax_nopriv_qcld_wb_chatbot_load_more', 'qcld_wb_chatbot_load_more');
819 function qcld_wb_chatbot_load_more(){
820 $offset = stripslashes($_POST['offset']);
821 $search_type = stripslashes($_POST['search_type']);
822 $search_term = stripslashes($_POST['search_term']);
823 $product_per_page = get_option('qlcd_wp_chatbot_ppp') != '' ? get_option('qlcd_wp_chatbot_ppp') : 10;
824 $product_orderby = get_option('qlcd_wp_chatbot_product_orderby') != '' ? get_option('qlcd_wp_chatbot_product_orderby') : 'title';
825 $product_order = get_option('qlcd_wp_chatbot_product_order') != '' ? get_option('qlcd_wp_chatbot_product_order') : 'ASC';
826 $next_offset = intval($product_per_page + $offset);
827 if ($search_type == 'product' && $search_term != 'featured' && $search_term != 'sale' && get_option('qlcd_wp_chatbot_search_option') == 'advanced') {
828 //if have multiple ids then explode else have single need to array push
829 if (strpos($search_term, ',') !== false) {
830 $product_ids = explode(',', $search_term);
831 } else {
832 $product_ids = array($search_term);
833 }
834 $result = wpwBot_Search::factory()->get_load_more_products($product_ids);
835 $products = $result['products'];
836 $product_num = count($result['products']);
837 $total_product_num = $result['total_products'];
838 $more_product_ids = implode(",", $result['more_ids']);
839 $_pf = new WC_Product_Factory();
840 //repeating the products
841 $html = '';
842 if ($product_num > 0) {
843 foreach ($products as $product) {
844 $html .= '<li class="wp-chatbot-product">';
845 $html .= '<a target="_blank" href="' . get_permalink($product->get_id()) . '" wp-chatbot-pid= "' . $product->get_id() . '" title="' . esc_attr($product->get_title()) . '">';
846 $html .= get_the_post_thumbnail($product->get_id(), 'shop_catalog') . '
847 <div class="wp-chatbot-product-summary">
848 <div class="wp-chatbot-product-table">
849 <div class="wp-chatbot-product-table-cell">
850 <h3 class="wp-chatbot-product-title">' . $product->get_title() . '</h3>
851 <div class="price">' . $product->get_price_html() . '</div>';
852 $html .= ' </div></div></div></a></li>';
853 }
854 }
855 $response = array('html' => $html, 'product_num' => $total_product_num, 'search_term' => $more_product_ids, 'offset' => $next_offset, 'per_page' => $product_per_page);
856 } else {
857 if ($search_type == 'product' && $search_term != 'featured' && $search_term != 'sale') { //For Standard search
858 $argu_params = array(
859 'post_type' => 'product',
860 'post_status' => 'publish',
861 'posts_per_page' => $product_per_page,
862 'offset' => $offset,
863 'orderby' => $product_orderby,
864 'order' => $product_order,
865 's' => $search_term,
866 );
867 } else if ($search_type == 'category') {
868 $argu_params = array(
869 'post_type' => 'product',
870 'post_status' => 'publish',
871 'ignore_sticky_posts' => 1,
872 'posts_per_page' => $product_per_page,
873 'orderby' => $product_orderby,
874 'order' => $product_order,
875 'offset' => $offset,
876 'tax_query' => array(
877 array(
878 'taxonomy' => 'product_cat',
879 'field' => 'term_id',
880 'terms' => $search_term,
881 'operator' => 'IN'
882 )
883 )
884 );
885 } else if ($search_type == 'product' && $search_term == 'featured') {
886 $argu_params = array(
887 'post_type' => 'product',
888 'post_status' => 'publish',
889 'ignore_sticky_posts' => 1,
890 'posts_per_page' => $product_per_page,
891 'orderby' => $product_orderby,
892 'order' => $product_order,
893 'offset' => $offset,
894 'meta_query' => array('key' => '_featured', 'value' => 'yes')
895 );
896 } else if ($search_type == 'product' && $search_term == 'sale') {
897 $argu_params = array(
898 'post_type' => 'product',
899 'post_status' => 'publish',
900 'ignore_sticky_posts' => 1,
901 'posts_per_page' => $product_per_page,
902 'orderby' => $product_orderby,
903 'order' => $product_order,
904 'offset' => $offset,
905 'meta_query' => array(
906 'relation' => 'OR',
907 array( // Simple products type
908 'key' => '_sale_price',
909 'value' => 0,
910 'compare' => '>',
911 'type' => 'numeric'
912 ),
913 array( // Variable products type
914 'key' => '_min_variation_sale_price',
915 'value' => 0,
916 'compare' => '>',
917 'type' => 'numeric'
918 )
919 )
920 );
921 }
922 $product_query = new WP_Query($argu_params);
923 $product_num = $product_query->post_count;
924 $_pf = new WC_Product_Factory();
925 //repeating the products
926 $html = '';
927 if ($product_num > 0) {
928 while ($product_query->have_posts()) : $product_query->the_post();
929 $product = $_pf->get_product(get_the_ID());
930 $html .= '<li class="wp-chatbot-product">';
931 $html .= '<a target="_blank" href="' . get_permalink(get_the_ID()) . '" wp-chatbot-pid= "' . get_the_ID() . '" title="' . esc_attr($product->post->post_title ? $product->post->post_title : get_the_ID()) . '">';
932 $html .= get_the_post_thumbnail(get_the_ID(), 'shop_catalog') . '
933 <div class="wp-chatbot-product-summary">
934 <div class="wp-chatbot-product-table">
935 <div class="wp-chatbot-product-table-cell">
936 <h3 class="wp-chatbot-product-title">' . $product->post->post_title . '</h3>
937 <div class="price">' . $product->get_price_html() . '</div>';
938 $html .= ' </div>
939 </div>
940 </div></a>
941 </li>';
942 endwhile;
943 wp_reset_postdata();
944 } else {
945 $html .= '';
946 }
947 $response = array('html' => $html, 'product_num' => $product_num, 'search_term' => $search_term, 'offset' => $next_offset, 'per_page' => $product_per_page);
948 }
949 echo wp_send_json($response);
950 wp_die();
951 }
952 //product details
953 add_action('wp_ajax_qcld_wb_chatbot_product_details', 'qcld_wb_chatbot_product_details');
954 add_action('wp_ajax_nopriv_qcld_wb_chatbot_product_details', 'qcld_wb_chatbot_product_details');
955 function qcld_wb_chatbot_product_details(){
956 $product_id = stripslashes($_POST['wp_chatbot_pid']);
957 //Tracking product view from chat board
958 wp_chatbot_view_track_product_by_id($product_id);
959 //wpcommerce product factory
960 $wc_pf = new WC_Product_Factory();
961 $product = $wc_pf->get_product($product_id);
962 $product_type = $wc_pf->get_product_type($product_id);
963 $product_title = '<h2 id="wp-chatbot-product-title" ><a target="_blank" href="' . get_permalink($product->get_id()) . '">' . $product->get_title() . '</a></h2>';
964 $product_desc = apply_filters('the_excerpt', $product->post->post_excerpt);//$product->post->post_excerpt;
965 $gallery_ids = $product->get_gallery_image_ids();
966 //images processing..
967 $product_feature_image_id = get_post_thumbnail_id($product_id);
968 $product_feature_image = wp_get_attachment_image_src($product_feature_image_id, 'full');
969 $product_feature_thumb = wp_get_attachment_image_src($product_feature_image_id, 'shop_thumbnail');
970 //$full_img_src = $product_feature_image[0];
971 //$product_image = '<img style="width:500px;height:300px" src="' . $full_img_src . '" >';
972 $product_image = '<div class="wp-chatbot-product-image-large">
973 <a href="' . $product_feature_image[0] . '" id="wp-chatbot-product-image-large-path"><img id="wp-chatbot-product-image-large-src" src="' . $product_feature_image[0] . '" alt="Large Image" title="Zoom Image" /></a>
974 </div>';
975 $product_image .= '<div class="wp-chatbot-product-image-thumbs"><ul>
976 <li class="wp-chatbot-product-active-image-thumbs"><a href="' . $product_feature_image[0] . '" class="wp-chatbot-product-image-thumbs-path"><img class="wp-chatbot-product-image-thumbs-src" src="' . $product_feature_thumb[0] . '" alt="Thumb Image" /></a></li>';
977 if (!empty($gallery_ids)) {
978 foreach ($gallery_ids as $gallery_id) {
979 $gallery_image = wp_get_attachment_image_src($gallery_id, 'full');
980 $gallery_thumb = wp_get_attachment_image_src($gallery_id, 'shop_thumbnail');
981 $product_image .= '<li><a href="' . $gallery_image[0] . '" class="wp-chatbot-product-image-thumbs-path"><img class="wp-chatbot-product-image-thumbs-src" src="' . $gallery_thumb[0] . '" alt="Thumb Image" /></a></li>';
982 }
983 }
984 $product_image .= '</ul></div>';
985 $product_price = '<p class="wp-chatbot-product-price" id="wp-chatbot-product-price">' . $product->get_price_html() . '</p>';
986 $product_sku = '<p class="wp-chatbot-product-sku"> ' . __('SKU', 'wpchatbot') . ' : ' . $product->get_sku() . '</p>';
987 //if ( $product->is_in_stock() || $product->is_purchasable() )
988 //Handle variable product start
989 $variations = "";
990 $add_cart_button = "";
991 $product_quantity = "";
992 if ($product->is_in_stock()) {
993 if ($product_type == "variable") {
994 //Getting product variation number based details
995 $variations_data = array();
996 foreach ($product->get_children() as $child_id) {
997 $all_cfs = get_post_custom($child_id);
998 array_push($variations_data, array('variation_id' => $child_id, 'variation_data' => $all_cfs));
999 }
1000 $variations_data = json_encode($variations_data);
1001 $attributes = $product->get_attributes();
1002 //Handling Variant & Non Variant products
1003 $var_attrs = $product->get_variation_attributes();
1004 $varation_names = array();
1005 if (!empty($var_attrs)) {
1006 foreach ($var_attrs as $key => $value) {
1007 array_push($varation_names, $key);
1008 }
1009 }
1010 $debug = $varation_names;
1011 foreach ($attributes as $attribute) {
1012 /*if (empty($attribute['is_visible']) || ($attribute['is_taxonomy'] && !taxonomy_exists($attribute['name']))) {
1013 continue;
1014 }*/
1015 $title = wc_attribute_label($attribute['name']);
1016 $name = $attribute['name'];
1017 if ($attribute['is_taxonomy']) {
1018 $values = wc_get_product_terms($product->get_id(), $attribute['name'], array('fields' => 'slugs'));
1019 } else {
1020 $values = array_map('trim', explode(WC_DELIMITER, $attribute['value']));
1021 }
1022 natsort($values);
1023 if (!in_array($name, $varation_names)) {
1024 $variations .= '<p><label for="' . sanitize_title($name) . '">' . $title . '</label> ' . ucfirst(implode(",", $values)) . '</p>';
1025 } else {
1026 $variations .= '<div class="wp-chatbot-variable-' . sanitize_title($name) . '">';
1027 $variations .= '<label for="' . sanitize_title($name) . '">' . $title . '</label>';
1028 $variations .= '<select id="' . esc_attr(sanitize_title($name)) . '" name="attribute_' . sanitize_title($name) . '" data-attribute_name="attribute_' . sanitize_title($name) . '" class="each_attribute">';
1029 $variations .= '<option value="">' . randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_choose_option'))) . '</option>';
1030 foreach ($values as $value) {
1031 if (isset($_REQUEST['attribute_' . sanitize_title($name)])) {
1032 $selected_value = $_REQUEST['attribute_' . sanitize_title($name)];
1033 } else {
1034 $selected_value = '';
1035 }
1036 $variations .= '<option value="' . esc_attr(strtolower($value)) . '"' . selected($selected_value, $value, false) . '>' . apply_filters('wpcommerce_variation_option_name', $value) . '</option>';
1037 }
1038 $variations .= '</select></div>';
1039 }
1040 }
1041 $add_cart_button .= '<input type="button" id="wp-chatbot-variation-add-to-cart" wp-chatbot-product-id="' . $product_id . '" value="' . randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_add_to_cart'))) . '" variation_id="" />';
1042 $add_cart_button .= "<input type='hidden' id='wp-chatbot-variation-data' data-product-variation='" . $variations_data . "' />";
1043 } else {
1044 $add_cart_button .= '<input type="button" id="wp-chatbot-add-cart-button" wp-chatbot-product-id="' . $product_id . '" value="' . randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_add_to_cart'))) . '" />';
1045 }
1046 //Handle variable product end.
1047 $product_quantity .= '<label for="">' . randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_cart_quantity'))) . '</label>
1048 <input type="number" id="vPQuantity" value="1" />';
1049 }
1050 //$response=$full_size_image;
1051 $response = array('title' => $product_title, 'description' => $product_desc, 'image' => $product_image, 'price' => $product_price, 'sku' => $product_sku, 'quantity' => $product_quantity, 'buttton' => $add_cart_button, 'variation' => $variations, 'type' => $product_type, 'debug' => $debug);
1052 echo wp_send_json($response);
1053 wp_die();
1054 }
1055 //Add to cart for variable product.
1056 add_action('wp_ajax_variable_add_to_cart', 'qcld_wb_chatbot_variable_add_to_cart');
1057 add_action('wp_ajax_nopriv_variable_add_to_cart', 'qcld_wb_chatbot_variable_add_to_cart');
1058 function qcld_wb_chatbot_variable_add_to_cart(){
1059 $product_id = stripslashes($_POST['p_id']);
1060 $quantity = stripslashes($_POST['quantity']);
1061 $variations_id = stripslashes($_POST['variations_id']);
1062 $attrs = stripslashes($_POST['attributes']);
1063 //echo wp_send_json(array('p_id'=>$product_id,'qnty'=>$quantity,'id'=>$variations_id,'att'=>$attrs));
1064 $attributes = array();
1065 foreach ($attrs as $attr) {
1066 $single = explode("#", $attr);
1067 if (isset($single[0])) {
1068 $a_name = explode("_", $single[0]);
1069 }
1070 $attributes[$a_name[2]] = $single[1];
1071 }
1072 global $wpcommerce;
1073 $result = $wpcommerce->cart->add_to_cart($product_id, $quantity, $variations_id, $attributes, null);
1074 if ($result != false) {
1075 echo wp_send_json('variable');
1076 } else {
1077 echo wp_send_json('error');
1078 }
1079 wp_die();
1080 }
1081 //Add to cart for simple product.
1082 add_action('wp_ajax_qcld_wb_chatbot_add_to_cart', 'qcld_wb_chatbot_add_to_cart');
1083 add_action('wp_ajax_nopriv_qcld_wb_chatbot_add_to_cart', 'qcld_wb_chatbot_add_to_cart');
1084 function qcld_wb_chatbot_add_to_cart(){
1085 $product_id = stripslashes($_POST['product_id']);
1086 $product_quantity = stripslashes($_POST['quantity']);
1087 global $wpcommerce;
1088 $result = $wpcommerce->cart->add_to_cart($product_id, $product_quantity);
1089 if ($result != false) {
1090 echo wp_send_json('simple');
1091 } else {
1092 echo wp_send_json('error');
1093 }
1094 wp_die();
1095 }
1096 //Support part
1097 add_action('wp_ajax_qcld_wb_chatbot_support_email', 'qcld_wb_chatbot_support_email');
1098 add_action('wp_ajax_nopriv_qcld_wb_chatbot_support_email', 'qcld_wb_chatbot_support_email');
1099 function qcld_wb_chatbot_support_email(){
1100 $name = trim(sanitize_text_field($_POST['name']));
1101 $email = sanitize_email($_POST['email']);
1102 $message = sanitize_text_field($_POST['message']);
1103 $subject = get_option('qlcd_wp_chatbot_email_sub') != '' ? get_option('qlcd_wp_chatbot_email_sub') : 'Support Email from wpWBot by Client';
1104 //Extract Domain
1105 $url = get_site_url();
1106 $url = parse_url($url);
1107 $domain = $url['host'];
1108 //$admin_email = "admin@" . $domain;
1109 $admin_email = get_option('admin_email');
1110 $toEmail = get_option('qlcd_wp_chatbot_admin_email') != '' ? get_option('qlcd_wp_chatbot_admin_email') : $admin_email;
1111 $fromEmail = "wordpress@" . $domain;
1112 //Starting messaging and status.
1113 $response['status'] = 'fail';
1114 $response['message'] = str_replace('\\', '',get_option('qlcd_wp_chatbot_email_fail'));
1115 if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
1116 $response['message'] = randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_invalid_email')));
1117 $response['status'] = 'fail';
1118 } else {
1119 //build email body
1120 $bodyContent = "";
1121 $bodyContent .= '<p><strong>' . __('Support Request Details', 'wpchatbot') . ':</strong></p><hr>';
1122 $bodyContent .= '<p>' . __('Name', 'wpchatbot') . ' : ' . $name . '</p>';
1123 $bodyContent .= '<p>' . __('Email', 'wpchatbot') . ' : ' . $email . '</p>';
1124 $bodyContent .= '<p>' . __('Subject', 'wpchatbot') . ' : ' . $subject . '</p>';
1125 $bodyContent .= '<p>' . __('Message', 'wpchatbot') . ' : ' . $message . '</p>';
1126 $bodyContent .= '<p>' . __('Mail Generated on', 'wpchatbot') . ': ' . date('F j, Y, g:i a') . '</p>';
1127 $to = $toEmail;
1128 $body = $bodyContent;
1129 $headers = array();
1130 $headers[] = 'Content-Type: text/html; charset=UTF-8';
1131 $headers[] = 'From: ' . $name . ' <' . $fromEmail . '>';
1132 $headers[] = 'Reply-To: ' . $name . ' <' . $email . '>';
1133 $result = wp_mail($to, $subject, $body, $headers);
1134 if ($result) {
1135 $response['status'] = 'success';
1136 $response['message'] = str_replace('\\', '',get_option('qlcd_wp_chatbot_email_sent'));
1137 }
1138 }
1139 ob_clean();
1140 echo json_encode($response);
1141 die();
1142 }
1143 //Support Phone
1144 add_action('wp_ajax_qcld_wb_chatbot_support_phone', 'qcld_wb_chatbot_support_phone');
1145 add_action('wp_ajax_nopriv_qcld_wb_chatbot_support_phone', 'qcld_wb_chatbot_support_phone');
1146 function qcld_wb_chatbot_support_phone(){
1147 $name = trim(sanitize_text_field($_POST['name']));
1148 $phone =sanitize_text_field($_POST['phone']);
1149 $subject = 'WPBot Support Mail Request for Call Back';
1150 //Extract Domain
1151 $url = get_site_url();
1152 $url = parse_url($url);
1153 $domain = $url['host'];
1154 //$admin_email = "admin@" . $domain;
1155 $admin_email = get_option('admin_email');
1156 $toEmail = get_option('qlcd_wp_chatbot_admin_email') != '' ? get_option('qlcd_wp_chatbot_admin_email') : $admin_email;
1157 $fromEmail = "wordpress@" . $domain;
1158 //Starting messaging and status.
1159 $response['status'] = 'fail';
1160 $response['message'] = str_replace('\\', '',get_option('qlcd_wp_chatbot_phone_fail'));
1161 //build email body
1162 $bodyContent = "";
1163 $bodyContent .= '<p><strong>' . __('Support Request Details', 'wpchatbot') . ':</strong></p><hr>';
1164 $bodyContent .= '<p>' . __('Name', 'wpchatbot') . ' : ' . $name . '</p>';
1165 $bodyContent .= '<p>' . __('Phone', 'wpchatbot') . ' : ' . $phone . '</p>';
1166 $bodyContent .= '<p>' . __('Subject', 'wpchatbot') . ' : ' . $subject . '</p>';
1167 $bodyContent .= '<p>' . __('Message', 'wpchatbot') . ' : ' . __(' Call me at ', 'wpchatbot'). $phone . '</p>';
1168 $bodyContent .= '<p>' . __('Mail Generated on', 'wpchatbot') . ': ' . date('F j, Y, g:i a') . '</p>';
1169 $to = $toEmail;
1170 $body = $bodyContent;
1171 $headers = array();
1172 $headers[] = 'Content-Type: text/html; charset=UTF-8';
1173 $headers[] = 'From: ' . $name . ' <' . $fromEmail . '>';
1174 $headers[] = 'Reply-To: ' . $name . ' <' . $fromEmail . '>';
1175 $result = wp_mail($to, $subject, $body, $headers);
1176 if ($result) {
1177 $response['status'] = 'success';
1178 $response['message'] = str_replace('\\', '',get_option('qlcd_wp_chatbot_phone_sent'));
1179 }
1180 ob_clean();
1181 echo json_encode($response);
1182 die();
1183 }
1184 // Order Status part.
1185 add_action('wp_ajax_qcld_wb_chatbot_check_user', 'qcld_wb_chatbot_check_user');
1186 add_action('wp_ajax_nopriv_qcld_wb_chatbot_check_user', 'qcld_wb_chatbot_check_user');
1187 function qcld_wb_chatbot_check_user(){
1188 global $wpcommerce;
1189 $user_name = trim(sanitize_text_field($_POST['user_name']));
1190 $response = array();
1191 $response['message'] = "";
1192 if (username_exists($user_name)) {
1193 if (get_option('qlcd_wp_chatbot_order_user') == 'login') {
1194 $response['status'] = "success";
1195 $response['message'] .= randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_order_username_thanks')));
1196 $response['html'] .= '<p>' . randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_order_username_password'))) . '</p>';
1197 } else if (get_option('qlcd_wp_chatbot_order_user') == 'not_login') {
1198 $response = get_order_by_username($user_name);
1199 }
1200 } else {
1201 $response['status'] = "fail";
1202 $response['message'] .= '<strong>' . $user_name . '</strong> ' . randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_order_username_not_exist')));
1203 }
1204 echo wp_send_json($response);
1205 die();
1206 }
1207 function randmom_message_handle($items){
1208 return $items[rand(0, count($items) - 1)];
1209 }
1210 function qcld_wb_chatbot_func_str_replace($messages = array()){
1211 $refined_mesgses = array();
1212 foreach ($messages as $message) {
1213 $refined_msg = str_replace('\\', '', $message);
1214 array_push($refined_mesgses, $refined_msg);
1215 }
1216 return $refined_mesgses;
1217 }
1218 add_action('wp_ajax_qcld_wb_chatbot_login_user', 'qcld_wb_chatbot_login_user');
1219 add_action('wp_ajax_nopriv_qcld_wb_chatbot_login_user', 'qcld_wb_chatbot_login_user');
1220 function qcld_wb_chatbot_login_user(){
1221 // First check the nonce, if it fails the function will break
1222 check_ajax_referer('wpwbot-order-nonce', 'security');
1223 // Nonce is checked, get the POST data and sign user on
1224 $info = array();
1225 $info['user_login'] = trim(sanitize_text_field($_POST['user_name']));
1226 $info['user_password'] = trim(sanitize_text_field($_POST['user_pass']));
1227 $info['remember'] = true;
1228 $user_signon = wp_signon($info, false);
1229 $response = array();
1230 if (is_wp_error($user_signon)) {
1231 $response['status'] = "fail";
1232 $response['message'] = randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_order_password_incorrect')));
1233 } else {
1234 $response = get_order_by_username($info['user_login']);
1235 $response['status'] = "success";
1236 }
1237 echo wp_send_json($response);
1238 die();
1239 }
1240 add_action('wp_ajax_qcld_wb_chatbot_loged_in_user_orders', 'qcld_wb_chatbot_loged_in_user_orders');
1241 add_action('wp_ajax_nopriv_qcld_wb_chatbot_loged_in_user_orders', 'qcld_wb_chatbot_loged_in_user_orders');
1242 function qcld_wb_chatbot_loged_in_user_orders(){
1243 $current_user = wp_get_current_user();
1244 $user_name = $current_user->user_login;
1245 $response = get_order_by_username($user_name);
1246 echo wp_send_json($response);
1247 die();
1248 }
1249 function get_order_by_username($user_name){
1250 global $post;
1251 $response = array();
1252 $response['status'] .= "success";
1253 $user = get_user_by('login', $user_name);
1254 // The query arguments
1255 $customer_orders = get_posts(array(
1256 'numberposts' => -1,
1257 'meta_key' => '_customer_user',
1258 'meta_value' => $user->ID,
1259 'post_type' => wc_get_order_types(),
1260 'post_status' => array_keys(wc_get_order_statuses()),
1261 'posts_per_page' => 10,
1262 'orderby' => 'date',
1263 ));
1264 $response['order_num'] = count($customer_orders);
1265 $order_html = '';
1266 if ($response['order_num'] > 0) {
1267 $response['message'] .= randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_order_found')));
1268 $order_html .= '<div class="wp-chatbot-orders-container">
1269 <div class="wp-chatbot-orders-header">
1270 <div class="order-id">' . __('ID', 'wpchatbot') . '</div>
1271 <div class="order-date">' . __('Date', 'wpchatbot') . ' </div>
1272 <div class="order-items">' . __('Items', 'wpchatbot') . '</div>
1273 <div class="order-status">' . __('Status', 'wpchatbot') . '</div>
1274 </div>';
1275 foreach ($customer_orders as $order) {
1276 //Formatting order summery
1277 if (isset($_COOKIE['from_app']) && $_COOKIE['from_app'] == 'yes') {
1278 $thanks_page_id = get_option('wp_chatbot_app_order_thankyou');
1279 $thanks_parmanlink = esc_url(get_permalink($thanks_page_id));
1280 $order_url = '<a href="' . $thanks_parmanlink . '?order_id=' . $order->ID . '" >' . $order->ID . '</a>';
1281 } else {
1282 $order_url = '<a href="' . get_permalink(get_option('wpcommerce_myaccount_page_id')) . '/view-order/' . $order->ID . '" target="_blank" >' . $order->ID . '</a>';
1283 }
1284 $order_html .= '<div class="wp-chatbot-orders-single">
1285 <div class="order-id"> ' . $order_url . '</div>
1286 <div class="order-date"> <p>' . date("m/d/Y", strtotime($order->post_date)) . '</p> </div>
1287 <div class="order-items">';
1288 $singleOrder = new WC_Order($order->ID);
1289 $items = $singleOrder->get_items();
1290 foreach ($items as $item) {
1291 $order_html .= '<p>' . $item["name"] . ' X ' . $item["qty"] . '</p>';
1292 }
1293 $order_html .= '</div>
1294 <div class="order-status">' . strtoupper(end(explode("-", $order->post_status))) . '</div>
1295
1296 </div>';
1297 }
1298 $order_html .= '</div>';
1299 } else {
1300 $response['message'] .= get_option('qlcd_wp_chatbot_sorry') . '! <strong>' . $user_name . '</strong>';
1301 $order_html .= '<p>' . randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_order_not_found'))) . '</p>';
1302 }
1303 $response['html'] = $order_html;
1304 return $response;
1305 }
1306 //Recently viewed products
1307 //keeping id in cookies as
1308 function wp_chatbot_track_product_view(){
1309 if (!is_singular('product')) {
1310 return;
1311 }
1312 global $post;
1313 wp_chatbot_view_track_product_by_id($post->ID);
1314 }
1315 function wp_chatbot_view_track_product_by_id($post_id){
1316 if (empty($_COOKIE['wp_chatbot_wpcommerce_recently_viewed']))
1317 $viewed_products = array();
1318 else
1319 $viewed_products = (array)explode('|', $_COOKIE['wp_chatbot_wpcommerce_recently_viewed']);
1320 if (!in_array($post_id, $viewed_products)) {
1321 $viewed_products[] = $post_id;
1322 }
1323 if (sizeof($viewed_products) > 15) {
1324 array_shift($viewed_products);
1325 }
1326 // Store for session only
1327 wc_setcookie('wp_chatbot_wpcommerce_recently_viewed', implode('|', $viewed_products));
1328 }
1329 add_action('template_redirect', 'wp_chatbot_track_product_view', 20);
1330 //recent view product ajax
1331 add_action('wp_ajax_qcld_wb_chatbot_recently_viewed_products', 'qcld_wb_chatbot_recently_viewed_products');
1332 add_action('wp_ajax_nopriv_qcld_wb_chatbot_recently_viewed_products', 'qcld_wb_chatbot_recently_viewed_products');
1333 function qcld_wb_chatbot_recently_viewed_products(){
1334 // Get wpCommerce Global
1335 $_pf = new WC_Product_Factory();
1336 //show post per page.
1337 $product_per_page = get_option('qlcd_wp_chatbot_ppp') != '' ? get_option('qlcd_wp_chatbot_ppp') : 10;
1338 $wp_chatbot_product_title = randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_latest_product_welcome')));
1339 // Get recently viewed product cookies data
1340 $viewed_products = !empty($_COOKIE['wp_chatbot_wpcommerce_recently_viewed']) ? (array)explode('|', $_COOKIE['wp_chatbot_wpcommerce_recently_viewed']) : array();
1341 $viewed_products = array_filter(array_map('absint', $viewed_products));
1342 //get featured products if has.
1343 $featured_products = new WP_Query(array('post_status' => 'publish', 'posts_per_page' => $product_per_page, 'post_type' => 'product', 'tax_query' => array(array('taxonomy' => 'product_visibility', 'field' => 'name', 'terms' => 'featured'))));
1344 //Getting recently vieew products.
1345 if (!empty($viewed_products)) {
1346 $wp_chatbot_product_title = randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_viewed_product_welcome')));
1347 $product_query = new WP_Query(array(
1348 'posts_per_page' => $product_per_page,
1349 'no_found_rows' => 1,
1350 'post_status' => 'publish',
1351 'post_type' => 'product',
1352 'post__in' => $viewed_products,
1353 ));
1354 //Getting featured products
1355 } else if ($featured_products->post_count > 0) {
1356 $wp_chatbot_product_title = randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_featured_product_welcome')));
1357 $product_query = $featured_products;
1358 } else {
1359 $wp_chatbot_product_title = randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_latest_product_welcome')));
1360 //Getting recent products
1361 $product_query = new WP_Query(array('post_status' => 'publish', 'posts_per_page' => $product_per_page, 'post_type' => 'product', 'orderby' => 'date', 'order' => 'DESC'));
1362 }
1363 //$html = '<div class="wp-chatbot-widget-bubble">'.randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_viewed_product_welcome'))).'</div>';
1364 if (get_option('wp_chatbot_custom_agent_path') != "" && get_option('wp_chatbot_agent_image') == "custom-agent.png") {
1365 $wp_chatbot_custom_icon_path = get_option('wp_chatbot_custom_agent_path');
1366 } else if (get_option('wp_chatbot_custom_agent_path') != "" && get_option('wp_chatbot_agent_image') != "custom-agent.png") {
1367 $wp_chatbot_custom_icon_path = QCLD_wpCHATBOT_IMG_URL . get_option('wp_chatbot_agent_image');
1368 } else {
1369 $wp_chatbot_custom_icon_path = QCLD_wpCHATBOT_IMG_URL . 'custom-agent.png';
1370 }
1371 $html = '<div class="wp-chatbot-agent-profile">
1372 <div class="wp-chatbot-widget-avatar"><img src="' . $wp_chatbot_custom_icon_path . '" alt=""></div>
1373 <div class="wp-chatbot-widget-agent">' . get_option('qlcd_wp_chatbot_agent') . '</div>
1374 <div class="wp-chatbot-bubble">' . $wp_chatbot_product_title . '</div>
1375 </div>';
1376 if ($product_query->post_count > 0) {
1377 $html .= '<div class="wp-chatbot-products-area">';
1378 $html .= '<ul class="wp-chatbot-products">';
1379 while ($product_query->have_posts()) : $product_query->the_post();
1380 $product = $_pf->get_product(get_the_ID());
1381 $html .= '<li class="wp-chatbot-product">';
1382 $html .= '<a target="_blank" href="' . get_permalink(get_the_ID()) . '" wp-chatbot-pid= "' . get_the_ID() . '" title="' . esc_attr($product->post->post_title ? $product->post->post_title : get_the_ID()) . '">';
1383 $html .= get_the_post_thumbnail(get_the_ID(), 'shop_catalog') . '
1384 <div class="wp-chatbot-product-summary">
1385 <div class="wp-chatbot-product-table">
1386 <div class="wp-chatbot-product-table-cell">
1387 <h3 class="wp-chatbot-product-title">' . $product->post->post_title . '</h3>
1388 <div class="price">' . $product->get_price_html() . '</div>';
1389 $html .= ' </div>
1390 </div>
1391 </div></a>
1392 </li>';
1393 endwhile;
1394 wp_reset_query();
1395 wp_reset_postdata();
1396 $html .= '</ul></div>';
1397 } else {
1398 $html .= '<div class="wp-chatbot-products-area">';
1399 $html .= '<p style="text-align: center">You have no products !';
1400 $html .= '</div>';
1401 }
1402 echo wp_send_json($html);
1403 die();
1404 }
1405 //Recently viewed product shortcode
1406 add_shortcode('wpwbot_products', 'qcld_wb_chatbot_recently_viewed_shortcode');
1407 function qcld_wb_chatbot_recently_viewed_shortcode(){
1408 // Get wpCommerce Global
1409 $_pf = new WC_Product_Factory();
1410 $product_per_page = get_option('qlcd_wp_chatbot_ppp') != '' ? get_option('qlcd_wp_chatbot_ppp') : 10;
1411 $wp_chatbot_product_title = randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_latest_product_welcome')));
1412 // Get recently viewed product cookies data
1413 $viewed_products = !empty($_COOKIE['wp_chatbot_wpcommerce_recently_viewed']) ? (array)explode('|', $_COOKIE['wp_chatbot_wpcommerce_recently_viewed']) : array();
1414 $viewed_products = array_filter(array_map('absint', $viewed_products));
1415 //get featured products if has.
1416 $featured_products = new WP_Query(array('post_status' => 'publish', 'posts_per_page' => $product_per_page, 'post_type' => 'product', 'tax_query' => array(array('taxonomy' => 'product_visibility', 'field' => 'name', 'terms' => 'featured'))));
1417 //Getting recently vieew products.
1418 if (!empty($viewed_products)) {
1419 $wp_chatbot_product_title = randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_viewed_product_welcome')));
1420 $product_query = new WP_Query(array(
1421 'posts_per_page' => $product_per_page,
1422 'no_found_rows' => 1,
1423 'post_status' => 'publish',
1424 'post_type' => 'product',
1425 'post__in' => $viewed_products,
1426 ));
1427 //implementing featured products
1428 } else if ($featured_products->post_count > 0) {
1429 $wp_chatbot_product_title = randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_featured_product_welcome')));
1430 $product_query = $featured_products;
1431 } else {
1432 $wp_chatbot_product_title = randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_latest_product_welcome')));
1433 //Getting recent products
1434 $product_query = new WP_Query(array('post_status' => 'publish', 'posts_per_page' => $product_per_page, 'post_type' => 'product', 'orderby' => 'date', 'order' => 'DESC'));
1435 }
1436 //$html = '<div class="wp-chatbot-widget-bubble">'.randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_viewed_product_welcome'))).'</div>';
1437 if (get_option('wp_chatbot_custom_agent_path') != "" && get_option('wp_chatbot_agent_image') == "custom-agent.png") {
1438 $wp_chatbot_custom_icon_path = get_option('wp_chatbot_custom_agent_path');
1439 } else if (get_option('wp_chatbot_custom_agent_path') != "" && get_option('wp_chatbot_agent_image') != "custom-agent.png") {
1440 $wp_chatbot_custom_icon_path = QCLD_wpCHATBOT_IMG_URL . get_option('wp_chatbot_agent_image');
1441 } else {
1442 $wp_chatbot_custom_icon_path = QCLD_wpCHATBOT_IMG_URL . 'custom-agent.png';
1443 }
1444 $html = '<div class="wp-chatbot-agent-profile">
1445 <div class="wp-chatbot-widget-avatar"><img src="' . $wp_chatbot_custom_icon_path . '" alt=""></div>
1446 <div class="wp-chatbot-widget-agent">' . get_option('qlcd_wp_chatbot_agent') . '</div>
1447 <div class="wp-chatbot-bubble">' . $wp_chatbot_product_title . '</div>
1448 </div>';
1449 if ($product_query->post_count > 0) {
1450 $html .= '<div class="wp-chatbot-products-area">';
1451 $html .= '<ul class="wp-chatbot-products">';
1452 while ($product_query->have_posts()) : $product_query->the_post();
1453 $product = $_pf->get_product(get_the_ID());
1454 $html .= '<li class="wp-chatbot-product">';
1455 $html .= '<a target="_blank" href="' . get_permalink(get_the_ID()) . '" wp-chatbot-pid= "' . get_the_ID() . '" title="' . esc_attr($product->post->post_title ? $product->post->post_title : get_the_ID()) . '">';
1456 $html .= get_the_post_thumbnail(get_the_ID(), 'shop_catalog') . '
1457 <div class="wp-chatbot-product-summary">
1458 <div class="wp-chatbot-product-table">
1459 <div class="wp-chatbot-product-table-cell">
1460 <h3 class="wp-chatbot-product-title">' . $product->post->post_title . '</h3>
1461 <div class="price">' . $product->get_price_html() . '</div>';
1462 $html .= ' </div>
1463 </div>
1464 </div></a>
1465 </li>';
1466 endwhile;
1467 wp_reset_query();
1468 wp_reset_postdata();
1469 $html .= '</ul></div>';
1470 } else {
1471 $html .= '<div class="wp-chatbot-products-area">';
1472 $html .= '<p style="text-align: center">' . __('You have no products', 'wpchatbot') . ' !';
1473 $html .= '</div>';
1474 }
1475 return $html;
1476 }
1477 //Show cart for wp chatbot
1478 add_action('wp_ajax_qcld_wb_chatbot_show_cart', 'qcld_wb_chatbot_show_cart');
1479 add_action('wp_ajax_nopriv_qcld_wb_chatbot_show_cart', 'qcld_wb_chatbot_show_cart');
1480 function qcld_wb_chatbot_show_cart(){
1481 global $wpcommerce;
1482 // $cart_url = $wpcommerce->cart->get_cart_url();
1483 $cart_url = wc_get_cart_url();
1484 $checkout_url = wc_get_checkout_url();
1485 //$checkout_url = $wpcommerce->cart->get_checkout_url();
1486 $items = $wpcommerce->cart->get_cart();
1487 $itemCount = $wpcommerce->cart->cart_contents_count;
1488 $cart_title = randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_shopping_cart')));
1489 $no_cart_item_msg = randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_no_cart_items')));
1490 //$html = '<div class="wp-chatbot-widget-bubble">'.randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_cart_welcome'))).'</div>';
1491 if (get_option('wp_chatbot_custom_agent_path') != "" && get_option('wp_chatbot_agent_image') == "custom-agent.png") {
1492 $wp_chatbot_custom_icon_path = get_option('wp_chatbot_custom_agent_path');
1493 } else if (get_option('wp_chatbot_custom_agent_path') != "" && get_option('wp_chatbot_agent_image') != "custom-agent.png") {
1494 $wp_chatbot_custom_icon_path = QCLD_wpCHATBOT_IMG_URL . get_option('wp_chatbot_agent_image');
1495 } else {
1496 $wp_chatbot_custom_icon_path = QCLD_wpCHATBOT_IMG_URL . 'custom-agent.png';
1497 }
1498 $html = '<div class="wp-chatbot-agent-profile">
1499 <div class="wp-chatbot-widget-avatar"><img src="' . $wp_chatbot_custom_icon_path . '" alt=""></div>
1500 <div class="wp-chatbot-widget-agent">' . get_option('qlcd_wp_chatbot_agent') . '</div>
1501 <div class="wp-chatbot-bubble">' . randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_cart_welcome'))) . '</div>
1502 </div>';
1503 if ($itemCount >= 1) {
1504 $html .= '<div class ="wp-chatbot-cart-container">';
1505 $html .= '<div class="wp-chatbot-cart-header"><div class="qcld-wp-chatbot-cell">' . randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_cart_title'))) . '</div><div class="qcld-wp-chatbot-cell">' . randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_cart_quantity'))) . '</div><div class="qcld-wp-chatbot-cell">' . randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_cart_price'))) . '</div><div class="qcld-wp-chatbot-cell"></div></div>';
1506 $html .= '<div class ="wp-chatbot-cart-body">';
1507 foreach ($items as $item => $values) {
1508 $cart_item= apply_filters('wpcommerce_cart_item_product', $values['data'], $values, $item);
1509 //product image
1510 $getProductDetail = wc_get_product($values['product_id']);
1511 $price = get_post_meta($values['product_id'], '_price', true);
1512 $html .= '<div class="wp-chatbot-cart-single">
1513 <div class="qcld-wp-chatbot-cell"> <h3 class="wp-chatbot-title">' . $cart_item->get_title() . '</h3></div>';
1514 $html .= '<div class="qcld-wp-chatbot-cell">';
1515 $html .= '<input class="qcld-wp-chatbot-cart-item-qnty" data-cart-item="' . $item . '" type="number" min="1" value="' . $values['quantity'] . '"></div>';
1516 $html .= '<div class="qcld-wp-chatbot-cell"><span class="wp-chatbot-cart-price">' . apply_filters('wpcommerce_cart_item_price', WC()->cart->get_product_price($cart_item), $values, $item) . '</span> </div>';
1517 $html .= '<div class="qcld-wp-chatbot-cell"><span data-cart-item="' . $item . '" class="wp-chatbot-remove-cart-item">X</span></div> </div>';
1518 }
1519 $html .= ' </div>';//End of cart body
1520 $html .= '<div class="wp-chatbot-cart-single">
1521 <div class="qcld-wp-chatbot-cell"></div>
1522 <div class="qcld-wp-chatbot-cell"><strong>Total</strong></div>
1523 <div class="qcld-wp-chatbot-cell"><strong>' . $wpcommerce->cart->get_cart_total() . '</strong></div>
1524 </div>';
1525 $html .= '<div class="wp-chatbot-cart-footer"><div class="qcld-wp-chatbot-cart-page"><a class="wp-chatbot-cart-link" href="' . $cart_url . '" target="_blank" >' . randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_cart_link'))) . '</a></div><div class="qcld-wp-chatbot-checkout"><a class="wp-chatbot-checkout-link" href="' . $checkout_url . '" target="_blank" >' . randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_checkout_link'))) . '</a></div></div>';
1526 $html .= ' </div>';
1527 } else {
1528 $html .= '<div class="wp-chatbot-cart-container">';
1529 $html .= '<div><p style="text-align:center">' . $no_cart_item_msg . '</p></div>';
1530 $html .= '</div>';
1531 }
1532 $response = array('html' => $html, 'items' => $itemCount);
1533 echo wp_send_json($response);
1534 wp_die();
1535 }
1536 //cart onley for wp chatbot
1537 add_action('wp_ajax_qcld_wb_chatbot_only_cart', 'qcld_wb_chatbot_only_cart');
1538 add_action('wp_ajax_nopriv_qcld_wb_chatbot_only_cart', 'qcld_wb_chatbot_only_cart');
1539 function qcld_wb_chatbot_only_cart(){
1540 global $wpcommerce;
1541 // $cart_url = $wpcommerce->cart->get_cart_url();
1542 $cart_url = wc_get_cart_url();
1543 $checkout_url = wc_get_checkout_url();
1544 //$checkout_url = $wpcommerce->cart->get_checkout_url();
1545 $items = $wpcommerce->cart->get_cart();
1546 $itemCount = $wpcommerce->cart->cart_contents_count;
1547 $no_cart_item_msg = randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_no_cart_items')));
1548
1549 $html = '';
1550 if ($itemCount >= 1) {
1551 $html .= '<div class ="wp-chatbot-cart-container">';
1552 $html .= '<div class="wp-chatbot-cart-header"><div class="qcld-wp-chatbot-cell">' . randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_cart_title'))) . '</div><div class="qcld-wp-chatbot-cell">' . randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_cart_quantity'))) . '</div><div class="qcld-wp-chatbot-cell">' . randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_cart_price'))) . '</div><div class="qcld-wp-chatbot-cell"></div></div>';
1553 $html .= '<div class ="wp-chatbot-cart-body">';
1554 foreach ($items as $item => $values) {
1555 $cart_item= apply_filters('wpcommerce_cart_item_product', $values['data'], $values, $item);
1556 //product image
1557 $getProductDetail = wc_get_product($values['product_id']);
1558 $price = get_post_meta($values['product_id'], '_price', true);
1559 $html .= '<div class="wp-chatbot-cart-single">
1560 <div class="qcld-wp-chatbot-cell"> <h3 class="wp-chatbot-title">' . $cart_item->get_title() . '</h3></div>';
1561 $html .= '<div class="qcld-wp-chatbot-cell">';
1562 $html .= '<input class="qcld-wp-chatbot-cart-item-qnty" data-cart-item="' . $item . '" type="number" min="1" value="' . $values['quantity'] . '"></div>';
1563 $html .= '<div class="qcld-wp-chatbot-cell"><span class="wp-chatbot-cart-price">' . apply_filters('wpcommerce_cart_item_price', WC()->cart->get_product_price($cart_item), $values, $item) . '</span> </div>';
1564 $html .= '<div class="qcld-wp-chatbot-cell"><span data-cart-item="' . $item . '" class="wp-chatbot-remove-cart-item">X</span></div> </div>';
1565 }
1566 $html .= ' </div>';//End of cart body
1567 $html .= '<div class="wp-chatbot-cart-single">
1568 <div class="qcld-wp-chatbot-cell"></div>
1569 <div class="qcld-wp-chatbot-cell"><strong>Total</strong></div>
1570 <div class="qcld-wp-chatbot-cell"><strong>' . $wpcommerce->cart->get_cart_total() . '</strong></div>
1571 </div>';
1572 $html .= '<div class="wp-chatbot-cart-footer"><div class="qcld-wp-chatbot-cart-page"><a class="wp-chatbot-cart-link" href="' . $cart_url . '" target="_blank" >' . randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_cart_link'))) . '</a></div><div class="qcld-wp-chatbot-checkout"><a class="wp-chatbot-checkout-link" href="' . $checkout_url . '" target="_blank" >' . randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_checkout_link'))) . '</a></div></div>';
1573 $html .= ' </div>';
1574 } else {
1575 $html .= '<div class="wp-chatbot-cart-container">';
1576 $html .= '<div><p style="text-align:center">' . $no_cart_item_msg . '</p></div>';
1577 $html .= '</div>';
1578 }
1579 $response = array('html' => $html, 'items' => $itemCount);
1580 echo wp_send_json($response);
1581 wp_die();
1582 }
1583 //Cart show Shortcode
1584 add_shortcode('wpwbot_cart', 'qcld_wb_chatbot_cart_shortcode');
1585 function qcld_wb_chatbot_cart_shortcode(){
1586 global $wpcommerce;
1587 $items = $wpcommerce->cart->get_cart();
1588 $itemCount = $wpcommerce->cart->cart_contents_count;
1589 $cart_title = randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_shopping_cart')));
1590 $no_cart_item_msg = randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_no_cart_items')));
1591 // $html = '<div class="wp-chatbot-widget-bubble">'.randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_cart_welcome'))).'</div>';
1592 if (get_option('wp_chatbot_custom_agent_path') != "" && get_option('wp_chatbot_agent_image') == "custom-agent.png") {
1593 $wp_chatbot_custom_icon_path = get_option('wp_chatbot_custom_agent_path');
1594 } else if (get_option('wp_chatbot_custom_agent_path') != "" && get_option('wp_chatbot_agent_image') != "custom-agent.png") {
1595 $wp_chatbot_custom_icon_path = QCLD_wpCHATBOT_IMG_URL . get_option('wp_chatbot_agent_image');
1596 } else {
1597 $wp_chatbot_custom_icon_path = QCLD_wpCHATBOT_IMG_URL . 'custom-agent.png';
1598 }
1599 $html = '<div class="wp-chatbot-agent-profile">
1600 <div class="wp-chatbot-widget-avatar"><img src="' . $wp_chatbot_custom_icon_path . '" alt=""></div>
1601 <div class="wp-chatbot-widget-agent">' . get_option('qlcd_wp_chatbot_agent') . '</div>
1602 <div class="wp-chatbot-bubble">' . randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_cart_welcome'))) . '</div>
1603 </div>';
1604 if ($itemCount >= 1) {
1605 $html .= '<div class ="wp-chatbot-cart-container">';
1606 $html .= '<div class="wp-chatbot-cart-header"><div class="qcld-wp-chatbot-cell">' . randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_cart_title'))) . '</div><div class="qcld-wp-chatbot-cell">' . randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_cart_quantity'))) . '</div><div class="qcld-wp-chatbot-cell">' . randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_cart_price'))) . '</div> <div class="qcld-wp-chatbot-cell"></div> </div>';
1607 $html .= '<div class ="wp-chatbot-cart-body">';
1608 foreach ($items as $item => $values) {
1609 $cart_item = apply_filters('wpcommerce_cart_item_product', $values['data'], $values, $item);
1610 //product image
1611 $getProductDetail = wc_get_product($values['product_id']);
1612 $price = get_post_meta($values['product_id'], '_price', true);
1613 $html .= '<div class="wp-chatbot-cart-single">
1614 <div class="qcld-wp-chatbot-cell"> <h3 class="wp-chatbot-title">' . $cart_item->get_title() . '</h3></div>';
1615 $html .= '<div class="qcld-wp-chatbot-cell">';
1616 $html .= '<input class="qcld-wp-chatbot-cart-item-qnty" data-cart-item="' . $item . '" type="number" min="1" value="' . $values['quantity'] . '"></div>';
1617 $html .= '<div class="qcld-wp-chatbot-cell"><span class="wp-chatbot-cart-price">' . apply_filters('wpcommerce_cart_item_price', WC()->cart->get_product_price($cart_item), $values, $item) . '</span> </div>';
1618 $html .= '<div class="qcld-wp-chatbot-cell"><span data-cart-item="' . $item . '" class="wp-chatbot-remove-cart-item">X</span></div> </div>';
1619 }
1620 $html .= ' </div>';//End of cart body
1621 $html .= '<div class="wp-chatbot-cart-single">
1622 <div class="qcld-wp-chatbot-cell"></div>
1623 <div class="qcld-wp-chatbot-cell"><strong>Total</strong></div>
1624 <div class="qcld-wp-chatbot-cell"><strong>' . $wpcommerce->cart->get_cart_total() . '</strong></div>
1625 </div>';
1626 $html .= '<div class="wp-chatbot-cart-footer"><div class="qcld-wp-chatbot-cart-page"><a href="' . site_url() . '/cart" target="_blank" >' . randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_cart_link'))) . '</a></div><div class="qcld-wp-chatbot-checkout"><a href="' . site_url() . '/checkout" target="_blank">' . randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_checkout_link'))) . '</a></div></div>';
1627 $html .= ' </div>';
1628 } else {
1629 $html .= '<div class="wp-chatbot-cart-container">';
1630 $html .= '<div><p style="text-align:center">' . $no_cart_item_msg . '</p></div>';
1631 $html .= '</div>';
1632 }
1633 // $response=array('html'=>$html,'items'=>$itemCount);
1634 return $html;
1635 }
1636 //Updating the cart items.
1637 add_action('wp_ajax_qcld_wb_chatbot_update_cart_item_number', 'qcld_wb_chatbot_update_cart_item_number');
1638 add_action('wp_ajax_nopriv_qcld_wb_chatbot_update_cart_item_number', 'qcld_wb_chatbot_update_cart_item_number');
1639 function qcld_wb_chatbot_update_cart_item_number(){
1640 //getting cart items n
1641 $cart_item_key = sanitize_text_field($_POST['cart_item_key']);
1642 $qnty = sanitize_text_field($_POST['qnty']);
1643 global $wpcommerce;
1644 $result = $wpcommerce->cart->set_quantity($cart_item_key, $qnty);
1645 echo wp_send_json($result);
1646 wp_die();
1647 }
1648 //Show item after removing from cart page.
1649 add_action('wp_ajax_qcld_wb_chatbot_cart_item_remove', 'qcld_wb_chatbot_cart_item_remove');
1650 add_action('wp_ajax_nopriv_qcld_wb_chatbot_cart_item_remove', 'qcld_wb_chatbot_cart_item_remove');
1651 function qcld_wb_chatbot_cart_item_remove(){
1652 //getting cart items n
1653 $cart_item_key = sanitize_text_field($_POST['cart_item']);
1654 global $wpcommerce;
1655 $result = $wpcommerce->cart->remove_cart_item($cart_item_key);
1656 echo wp_send_json($result);
1657 wp_die();
1658 }
1659 //Show cart page by shortcode.
1660 add_action('wp_ajax_qcld_wb_chatbot_cart_page', 'qcld_wb_chatbot_cart_page');
1661 add_action('wp_ajax_nopriv_qcld_wb_chatbot_cart_page', 'qcld_wb_chatbot_cart_page');
1662 function qcld_wb_chatbot_cart_page(){
1663 global $wpcommerce;
1664 $itemCount = $wpcommerce->cart->cart_contents_count;
1665 $html = "";
1666 if ($itemCount < 0) {
1667 $html .= randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_no_cart_items')));
1668 } else {
1669 $html .= do_shortcode("[wpcommerce_cart]");
1670 }
1671 echo wp_send_json($html);
1672 wp_die();
1673 }
1674 //Show checkout page by shortcode.
1675 add_action('wp_ajax_qcld_wb_chatbot_checkout_page', 'qcld_wb_chatbot_checkout_page');
1676 add_action('wp_ajax_nopriv_qcld_wb_chatbot_checkout_page', 'qcld_wb_chatbot_checkout_page');
1677 function qcld_wb_chatbot_checkout_page(){
1678 global $wpcommerce;
1679 $itemCount = $wpcommerce->cart->cart_contents_count;
1680 $html = "";
1681 if ($itemCount < 0) {
1682 $status = 'no';
1683 $html .= randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_no_cart_items')));
1684 } else {
1685 $status = 'yes';
1686 $checkout_page_id = get_option('wp_chatbot_app_checkout');
1687 $checkout_parmanlink = esc_url(get_permalink($checkout_page_id));
1688 $html .= $checkout_parmanlink;
1689 }
1690 $response = array('status' => $status, 'html' => $html);
1691 echo wp_send_json($response);
1692 wp_die();
1693 }
1694 //User login on Checkout page.
1695 add_action('wp_ajax_qcld_wb_chatbot_checkout_user_login', 'qcld_wb_chatbot_checkout_user_login');
1696 add_action('wp_ajax_nopriv_qcld_wb_chatbot_checkout_user_login', 'qcld_wb_chatbot_checkout_user_login');
1697 function qcld_wb_chatbot_checkout_user_login(){
1698 // Nonce is checked, get the POST data and sign user on
1699 $info = array();
1700 //$info['nonce'] = $_POST['nonce_val'];
1701 $info['user_login'] = trim(sanitize_text_field($_POST['user_name']));
1702 $info['user_password'] = trim(sanitize_text_field($_POST['user_pass']));
1703 $info['remember'] = true;
1704 $user_signon = wp_signon($info, false);
1705 // $response=$info;
1706 $response = array();
1707 if (is_wp_error($user_signon)) {
1708 // $response['status'] = "fail";
1709 // $response['message'] = randmom_message_handle(unserialize(get_option('qlcd_wp_chatbot_order_password_incorrect')));
1710 $response = "no";
1711 } else {
1712 $response = "yes";
1713 }
1714 echo wp_send_json($response);
1715 die();
1716 }
1717 // Load template for App Order Thank You page url
1718 function wp_chatbot_load_app_template($template){
1719 if (is_page('wpwbot-mobile-app')) {
1720 return dirname(__FILE__) . '/templates/app-templates/app.php';
1721 }
1722 return $template;
1723 }
1724 add_filter('template_include', 'wp_chatbot_load_app_template', 99);
1725 // Load template for App Order Thank You page url
1726 function wp_chatbot_load_app_order_thankyou_template($template){
1727 if (is_page('wpwbot-app-order-thankyou')) {
1728 return dirname(__FILE__) . '/templates/app-templates/app-order-thankyou.php';
1729 }
1730 return $template;
1731 }
1732 add_filter('template_include', 'wp_chatbot_load_app_order_thankyou_template', 99);
1733 // Load template for App checkout
1734 function wp_chatbot_load_app_checkout_template($template){
1735 if (is_page('wpwbot-app-checkout')) {
1736 return dirname(__FILE__) . '/templates/app-templates/app-checkout.php';
1737 }
1738 return $template;
1739 }
1740 add_filter('template_include', 'wp_chatbot_load_app_checkout_template', 99);
1741 // Create embed page when plugin install or activate
1742 //register_activation_hook(__FILE__, 'wp_chatbot_create_app_order_thankyou_page');
1743 add_action('init', 'wp_chatbot_create_app_checkout_thankyou_page');
1744 function wp_chatbot_create_app_checkout_thankyou_page(){
1745 if (get_option('wp_chatbot_app_pages') == 1) {
1746 //Mobile App page create
1747 if (get_page_by_title('wpwBot Mobile App') == NULL) {
1748 //post status and options
1749 $app_page = array(
1750 'comment_status' => 'closed',
1751 'ping_status' => 'closed',
1752 'post_author' => get_current_user_id(),
1753 'post_date' => date('Y-m-d H:i:s'),
1754 'post_status' => 'publish',
1755 'post_title' => 'wpwBot Mobile App',
1756 'post_name' => 'wpwbot-mobile-app',
1757 'post_type' => 'page',
1758 );
1759 //insert page and save the id
1760 $wpwbot_app = wp_insert_post($app_page, false);
1761 //save the id in the database
1762 update_option('wp_chatbot_app_checkout', $wpwbot_app);
1763 }
1764 //App checkout page create
1765 if (get_page_by_title('wpwBot App Checkout') == NULL) {
1766 //post status and options
1767 $checkout_page = array(
1768 'comment_status' => 'closed',
1769 'ping_status' => 'closed',
1770 'post_author' => get_current_user_id(),
1771 'post_date' => date('Y-m-d H:i:s'),
1772 'post_status' => 'publish',
1773 'post_title' => 'wpwBot App Checkout',
1774 'post_name' => 'wpwbot-app-checkout',
1775 'post_type' => 'page',
1776 );
1777 //insert page and save the id
1778 $app_checkout = wp_insert_post($checkout_page, false);
1779 //save the id in the database
1780 update_option('wp_chatbot_app_checkout', $app_checkout);
1781 }
1782 //App Order thank you page create
1783 if (get_page_by_title('wpwBot App Order Thank You') == NULL) {
1784 //post status and options
1785 $thankyou_page = array(
1786 'comment_status' => 'closed',
1787 'ping_status' => 'closed',
1788 'post_author' => get_current_user_id(),
1789 'post_date' => date('Y-m-d H:i:s'),
1790 'post_status' => 'publish',
1791 'post_title' => 'wpwBot App Order Thank You',
1792 'post_name' => 'wpwbot-app-order-thankyou',
1793 'post_type' => 'page',
1794 );
1795 //insert page and save the id
1796 $app_order_thankyou = wp_insert_post($thankyou_page, false);
1797 //save the id in the database
1798 update_option('wp_chatbot_app_order_thankyou', $app_order_thankyou);
1799 }
1800 }
1801 //Keep tracking from App by cookies
1802 if (isset($_GET['from']) && $_GET['from'] == 'app') {
1803 if (!isset($_COOKIE['from_app'])) {
1804 setcookie('from_app', 'yes', (time() + 3600), '/');
1805 }
1806 }
1807 }
1808 /***
1809 * Override order Thank page for mobile app
1810 */
1811 add_action('wpcommerce_thankyou', 'qcld_wb_chatbot__redirect_after_purchase', 10, 1);
1812 function qcld_wb_chatbot__redirect_after_purchase($order_get_id){
1813 if (isset($_COOKIE['from_app']) && $_COOKIE['from_app'] == 'yes') {
1814 global $wp;
1815 if (is_checkout() && !empty($wp->query_vars['order-received'])) {
1816 $thanks_page_id = get_option('wp_chatbot_app_order_thankyou');
1817 $thanks_parmanlink = esc_url(get_permalink($thanks_page_id));
1818 wp_redirect($thanks_parmanlink . '?order_id=' . $order_get_id);
1819 exit;
1820 }
1821 } else {
1822 remove_action('wpcommerce_thankyou', 'qcld_wb_chatbot__redirect_after_purchase');
1823 do_action('wpcommerce_thankyou', $order_get_id);
1824 }
1825 }