PluginProbe
WPBot – AI ChatBot for Live Support, Lead Generation, AI Services / 2.2.0
WPBot – AI ChatBot for Live Support, Lead Generation, AI Services v2.2.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 2.2.0, at functions.php

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