Plugin Directory

Changeset 3398671


Ignore:
Timestamp:
11/19/2025 09:10:30 AM (4 months ago)
Author:
revechat
Message:

woocommerce update

Location:
revechat/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • revechat/trunk/assets/js/woocommerceCart.js

    r3375498 r3398671  
    1919   
    2020   
    21     var cartContents = fetch("/wp-json/wc/store/cart",
     21    var cartContents = fetch("/wp-json/revechat/v1/cart",
    2222    {
    2323        method: "GET",
     
    4949    ).catch((error)=>{}
    5050    );
    51 }, 10000);
     51}, 5000);
    5252function getCartPayload(data) {
    5353    return data;
     
    5757}
    5858function sendCartInfoToRevechat(cartInformation) {
    59     if ($_REVECHAT_API.Form && $_REVECHAT_API.Form.Online && $_REVECHAT_API.isVisitorChatting()) {
     59    if ($_REVECHAT_API.Form && $_REVECHAT_API.Form.Online) {
    6060        var cms_name = "woocommerce";
    6161        $_REVECHAT_API.Form.Online.pushCartInformation(cms_name, JSON.stringify(cartInformation), $_REVECHAT_API.getShoppingCartEventType());
  • revechat/trunk/revechat.php

    r3367101 r3398671  
    5959           
    6060            register_rest_route( 'revechat/v1', '/cart', array(
    61               'methods' => 'GET',
    62               'callback' => array($this,'getCartInfo'),
     61                'methods' => 'GET',
     62                'callback' => array($this,'getCartInfo'),
    6363            ) );
    64            
    65            
    66           });
    67          
    68      
    69         add_action('plugins_loaded', function() {
    70             load_plugin_textdomain('revechat', false, dirname(plugin_basename(__FILE__)) . '/languages');
     64       
    7165        });
    72 
    73        
    74    
    75         } // END public function __construct
     66       
     67
     68        add_action('template_redirect', function () {
     69            if (!isset($_GET['add-multiple-to-cart'])) return;
     70
     71            if (!function_exists('WC')) return;
     72
     73            // Load cart if not exists
     74            if (!WC()->cart) wc_load_cart();
     75
     76            // Ensure WooCommerce session is initialized
     77            if (WC()->session && !WC()->session->has_session()) {
     78                WC()->session->set_customer_session_cookie(true);
     79                WC()->session->init();
     80            }
     81
     82            $items = explode(',', sanitize_text_field($_GET['add-multiple-to-cart']));
     83            WC()->cart->empty_cart();
     84
     85            foreach ($items as $item) {
     86                list($product_id, $quantity) = array_pad(explode(':', $item), 2, 1);
     87
     88                $added = WC()->cart->add_to_cart(intval($product_id), intval($quantity));
     89            }
     90
     91            // ✅ Apply coupon if provided
     92            if (isset($_GET['coupon']) && !empty($_GET['coupon'])) {
     93                $coupon_code = sanitize_text_field($_GET['coupon']);
     94                $coupon_result = WC()->cart->apply_coupon($coupon_code);
     95
     96                if (is_wp_error($coupon_result)) {
     97                    error_log('[DEBUG] Coupon error: ' . $coupon_result->get_error_message());
     98                } else {
     99                    error_log('[DEBUG] Coupon applied successfully: ' . $coupon_code);
     100                }
     101            }
     102
     103            // Make sure totals and cookies are set
     104            WC()->cart->calculate_totals();
     105            WC()->cart->maybe_set_cart_cookies();
     106
     107            wp_safe_redirect(wc_get_checkout_url());
     108            exit;
     109        });
     110
     111        add_filter('woocommerce_rest_prepare_product_object', function($response, $object) {
     112            $response->data['thumbnail_id'] = get_post_thumbnail_id($object->get_id());
     113            return $response;
     114        }, 10, 2);
     115    }
     116 // END public function __construct
    76117
    77118     /**
     
    181222    {
    182223        $accountId = get_option('revechat_aid' , '');
     224        $storeUrl = get_option("revechat_wc_storeUrl", '');
    183225       
    184226        if( (isset($accountId) && !empty($accountId))  ) {
    185227
    186             $script = "<script type='text/javascript'>";
    187             $script .= 'window.$_REVECHAT_API || (function(d, w) { var r = $_REVECHAT_API = function(c) {r._.push(c);}; w.__revechat_account=\''.$accountId.'\';w.__revechat_version=2;
    188                     r._= []; var rc = d.createElement(\'script\'); rc.type = \'text/javascript\'; rc.async = true; rc.setAttribute(\'charset\', \'utf-8\');
    189                     rc.src = (\'https:\' == document.location.protocol ? \'https://\' : \'http://\') + \'static.revechat.com/widget/scripts/new-livechat.js?\'+new Date().getTime();
    190                     var s = d.getElementsByTagName(\'script\')[0]; s.parentNode.insertBefore(rc, s);
    191                     })(document, window);';
    192 
    193             $script .='</script>';
     228            // $script = "<script type='text/javascript'>
     229            //     var urlParams = new URLSearchParams(window.location.search);
     230            //     var widgetId = urlParams.get('widgetId');
     231
     232            //     window.\$_REVECHAT_API || (function(d, w) {
     233            //         var r = \$_REVECHAT_API = function(c) { r._.push(c); };
     234            //         w.__revechat_account = '{$accountId}';
     235            //         w.__revechat_version = 2;
     236
     237            //         if (widgetId) w.__revechat_widget_id = widgetId;
     238
     239            //         r._ = [];
     240            //         var rc = d.createElement('script');
     241            //         rc.type = 'text/javascript';
     242            //         rc.async = true;
     243            //         rc.setAttribute('charset', 'utf-8');
     244            //         rc.src = (document.location.protocol === 'https:' ? 'https://' : 'http://') +
     245            //                 'staging-static.revechat.com/client_2/scripts/minify/new-livechat.js?' + new Date().getTime();
     246
     247            //         var s = d.getElementsByTagName('script')[0];
     248            //         s.parentNode.insertBefore(rc, s);
     249            //     })(document, window);
     250
     251            //     \$(function() {
     252            //         \$_REVECHAT_API(function() {
     253            //             try {
     254            //                 window.frames['reve-chat-widget-holder']
     255            //                     .document.getElementById('reve-chat-widget-header')
     256            //                     .addEventListener('click', function() {
     257            //                         \$_REVECHAT_Analytics.addClientVisitInfo('/chat-button');
     258            //                     });
     259            //             } catch(e) {}
     260
     261            //             try {
     262            //                 window.frames['reve-chat-widget-holder']
     263            //                     .document.getElementById('reve-chat-widget-body')
     264            //                     .addEventListener('click', function() {
     265            //                         \$_REVECHAT_Analytics.addClientVisitInfo('/client-chat-window');
     266            //                     });
     267            //             } catch(e) {}
     268            //         });
     269            //     });
     270            // </script>";
     271
     272            // $script = "<script type='text/javascript'>window.$_REVECHAT_API || (function(d, w) { var r = $_REVECHAT_API = function(c) {r._.push(c);}; w.__revechat_account='10377411';w.__revechat_version=2;
     273            //         r._= []; var rc = d.createElement('script'); rc.type = 'text/javascript'; rc.async = true; rc.setAttribute('charset', 'utf-8');
     274            //         rc.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'static.revechat.com/widget/scripts/new-livechat.js?'+new Date().getTime();
     275            //         var s = d.getElementsByTagName('script')[0]; s.parentNode.insertBefore(rc, s);
     276            //         })(document, window);</script>";
     277
     278            $script = "<script src='https://staging-preview.revechat.com/files/{$accountId}_{$storeUrl}_WOOCOMMERCE.js'></script>";
     279
    194280           
    195281           
     
    292378              update_option( "revechat_wc_consumerSecret" , $_POST["revechat_wc_consumerSecret"]  );
    293379              update_option('revechat_wc_aid', get_option( $accountId ));
     380              update_option( "revechat_wc_storeUrl", "$_SERVER[HTTP_HOST]");
    294381            }
    295382           
     
    411498                </div>
    412499                <input type="hidden" value="<?php echo $val_accountId; ?>" name="revechat_aid">
     500                <input type="hidden" value="<?php echo $val_accountId; ?>" name="revechat_aid">
    413501                <!-- <input type="button" id="existingAccountBtn" class="btn btn-primary btn-block" value="Save Changes" /> -->
    414502                <input type="button" id="existingAccountBtn" class="btn btn-primary btn-block" value="<?php _e( 'Save Changes', 'revechat' ); ?>" />
     
    490578    }
    491579
     580    public static function my_plugin_on_deactivate() {
     581        // Your logic when plugin is deactivated
     582        $accountId = get_option('revechat_aid' , '');
     583        $storeUrl = get_option("revechat_wc_storeUrl", '');
     584
     585        // Example: send request to your external server
     586        $api_url = 'https://app.revechat.com/rest/v1/cms/remove-site';
     587
     588        $body = [
     589            'storeUrl' => $storeUrl,
     590            'accountId' => $accountId,
     591            'platform' => 'WOOCOMMERCE',
     592            'event' => 'plugin_deactivated',
     593        ];
     594
     595        wp_remote_post($api_url, [
     596            'method'      => 'POST',
     597            'timeout'     => 10,
     598            'blocking'    => true,  // true = wait for response before finishing deactivation
     599            'headers'     => [ 'Content-Type' => 'application/json' ],
     600            'body'        => json_encode($body),
     601        ]);
     602        delete_option('revechat_aid');
     603    }
     604
    492605
    493606    /**
     
    547660* Register the deactivation hook.
    548661*/
    549 register_deactivation_hook( __FILE__, array( 'WP_Plugin_Revechat', 'deactivate' ) );
     662register_deactivation_hook( __FILE__, array( 'WP_Plugin_Revechat', 'my_plugin_on_deactivate' ) );
    550663
    551664function revechat_activation_redirect( $plugin ) {
Note: See TracChangeset for help on using the changeset viewer.