Plugin Directory

Changeset 3399647


Ignore:
Timestamp:
11/20/2025 11:12:48 AM (4 months ago)
Author:
cod24
Message:
  • Dokan (Multi-Vendor) Support
  • Bugs fixes and improvements
Location:
cod24-shipping
Files:
36 added
1 deleted
10 edited

Legend:

Unmodified
Added
Removed
  • cod24-shipping/trunk/assets/js/cod24-woo-admin.js

    r3366748 r3399647  
    306306    jQuery(".cod24_change_status").on('click', function(e){
    307307        e.preventDefault();
    308        
     308
     309        // Try to find the container - support both WooCommerce admin and Dokan vendor forms
    309310        var container = jQuery('#cod24_order_info');
    310311        if (!container.length) {
    311             console.error('Container with id="cod24_order_info" not found');
     312            container = jQuery('#vendor_cod24_form');
     313        }
     314
     315        if (!container.length) {
     316            console.error('Container form not found (tried #order_barcode_form and #vendor_cod24_form)');
    312317            return;
    313318        }
    314    
     319
    315320        // Create FormData object
    316321        var data = new FormData();
    317    
     322
    318323        // Get all input, select, and textarea elements within the container
    319324        container.find('input, select, textarea').each(function() {
    320325            var element = jQuery(this);
    321326            var name = element.attr('name'); // Use the 'name' attribute as the key
    322    
     327
    323328            // Skip elements without a 'name' attribute
    324329            if (!name) {
     
    326331                return;
    327332            }
    328    
     333
    329334            // Handle checkboxes separately
    330335            if (element.attr('type') === 'checkbox') {
     
    337342            }
    338343        });
    339    
     344
    340345        // Append additional data (e.g., action)
    341346        data.append('action', 'cod24_change_status');
     
    488493        });
    489494    });
    490 
     495   
     496    // Handle is_calc_srv_by_weight checkbox change
     497    jQuery(document).on('change', '#is_calc_srv_by_weight', function() {
     498        var isChecked = jQuery(this).is(':checked');
     499        var cartonSizeSelect = jQuery('#carton_size');
     500       
     501        if (isChecked) {
     502            // Disable carton size and set to 0
     503            cartonSizeSelect.prop('disabled', true);
     504            cartonSizeSelect.val('10');
     505        } else {
     506            // Enable carton size
     507            cartonSizeSelect.prop('disabled', false);
     508        }
     509    });
     510   
     511    // Initialize on page load
     512    if (jQuery('#is_calc_srv_by_weight').is(':checked')) {
     513        jQuery('#carton_size').prop('disabled', true);
     514    }
    491515});
  • cod24-shipping/trunk/cod24-shipping.php

    r3366748 r3399647  
    33Plugin Name: COD24 Shipping For Woocommerce
    44Description: This plugin is adding COD24 shipping methods to woocommerce.
    5 Version: 4.0.10
     5Version: 5.0
    66Author: COD24
    77Author URI: https://cod24.ir
     
    2727        public function __construct()
    2828        {
    29             define('COD24_PLUGIN_VERSION', '4.0.10');
     29            define('COD24_PLUGIN_VERSION', '5.0');
    3030            define('COD24_PLUGIN_DIR', plugin_dir_path(__FILE__));
    3131            define('COD24_PLUGIN_URL', plugin_dir_url(__FILE__));
     
    3737            require_once('inc/shipping/class-cod24-special.php');
    3838            require_once('inc/shipping/class-cod24-tipax.php');
    39             require_once('inc/class-cod24-shcod.php');
     39            require_once('inc/class-cod24-settings.php');
     40            require_once('inc/class-cod24-shcod.php');
    4041            require_once('inc/class-cod24-woo.php');
    41             require_once('inc/class-cod24-settings.php');
     42            require_once('inc/class-cod24-dokan.php');
     43
     44            // Include Dokan integration if Dokan is active
     45            if (!class_exists('\WeDevs_Dokan')) {
     46               
     47            }
    4248
    4349            // Schedule the cron job to run once (e.g., 2 minutes from now)
     
    143149        {
    144150            // Do nothing
     151            flush_rewrite_rules();
    145152        }
    146153
     
    150157        public static function deactivate()
    151158        {
    152             // Do nothing
     159            // Do nothing
     160            flush_rewrite_rules();
    153161        }
    154162    }
  • cod24-shipping/trunk/inc/api/class-cod24-api.php

    r3346653 r3399647  
    3030        } // END public function __construct()
    3131
     32        /**
     33         * Get credentials based on vendor_id
     34         *
     35         * @param int|null $vendor_id Optional vendor ID
     36         * @return array Array with username, password, and secure settings
     37         */
     38        private static function get_credentials( $vendor_id = null ) {
     39            if( !empty( $vendor_id ) && $vendor_id > 0 ) {
     40                // Use vendor credentials
     41                return array(
     42                    'username' => get_user_meta( $vendor_id, '_vendor_cod24_username', true ),
     43                    'password' => get_user_meta( $vendor_id, '_vendor_cod24_password', true ),
     44                    'secure'   => get_option('cod24_shipping_api_secure_connection_enabled')
     45                );
     46            } else {
     47                // Use admin credentials
     48                return array(
     49                    'username' => get_option('cod24_shipping_username_panel'),
     50                    'password' => get_option('cod24__shipping_password_panel'),
     51                    'secure'   => get_option('cod24_shipping_api_secure_connection_enabled')
     52                );
     53            }
     54        }
     55   
     56        /**
     57         * Get token based on vendor_id
     58         *
     59         * @param int|null $vendor_id Optional vendor ID
     60         * @return string|false Token or false if not found
     61         */
     62        private static function get_stored_token( $vendor_id = null ) {
     63            if( !empty( $vendor_id ) && $vendor_id > 0 ) {
     64                $token = get_user_meta( $vendor_id, '_vendor_cod24_shipping_token', true );
     65            } else {
     66                $token = get_option('cod24_shipping_token');
     67            }
     68           
     69            if( empty( $token ) )
     70            {
     71                if( !empty( $vendor_id ) && $vendor_id > 0 ) {
     72                    self::get_token( $vendor_id );
     73                } else {
     74                    self::get_token();
     75                }
     76            }
     77               
     78            return $token;
     79        }
     80   
     81        /**
     82         * Store token based on vendor_id
     83         *
     84         * @param string $token Token to store
     85         * @param int|null $vendor_id Optional vendor ID
     86         */
     87        private static function store_token( $token, $vendor_id = null ) {
     88            if( !empty( $vendor_id ) && $vendor_id > 0 ) {
     89                update_user_meta( $vendor_id, '_vendor_cod24_shipping_token', esc_attr( $token ) );
     90            } else {
     91                update_option('cod24_shipping_token', esc_attr( $token ) );
     92            }
     93        }
     94       
    3295        /**
    3396         * Admin bar wallet balance
     
    100163            }
    101164        }
     165       
     166        /**
     167         * vendor wallet balance
     168         *
     169         * @return void
     170         */
     171        public static function vendor_wallet_amount()
     172        {
     173            $vendor_id = get_current_user_id();
     174            $cod24_token = self::get_stored_token( $vendor_id );
     175           
     176            if ( ! empty( $cod24_token ) )
     177            {
     178                // Check if cached data exists
     179                $cache_key = 'cod24_vendor_wallet_amount_' . $vendor_id;
     180                $cached_vendor_wallet_amount = get_transient( $cache_key );
     181   
     182                if ( false === $cached_vendor_wallet_amount )
     183                {
     184                    // Cache is empty or expired, fetch data from API
     185                    $api_protocol = ( get_option('cod24_shipping_api_secure_connection_enabled') == 'yes' ) ? 'https://' : 'http://';
     186                    $url  = $api_protocol . 'api.cod24.ir/api/Wallet/getWalletAmount';
     187                    $body = [];
     188                    $body = wp_json_encode( $body );
     189   
     190                    // API arguments
     191                    $args = array(
     192                        'method'      => 'POST',
     193                        'timeout'     => 500,
     194                        'sslverify'   => ( get_option('cod24_shipping_api_secure_connection_enabled') == 'yes' ) ? true : false,
     195                        'headers'     => array(
     196                            'Authorization' => 'Bearer ' . $cod24_token,
     197                            'Content-Type'  => 'application/json',
     198                        ),
     199                        'body'        => $body,
     200                    );
     201   
     202                    $request = wp_remote_post( $url, $args );
     203   
     204                    if ( is_wp_error( $request ) || wp_remote_retrieve_response_code( $request ) != 200 )
     205                    {
     206                        $result['code'] = wp_remote_retrieve_response_code( $request );
     207                        if ( $result['code'] == 401 )
     208                        {
     209                            self::get_token( $vendor_id );
     210                        }
     211                    }
     212                    else
     213                    {
     214                        $response = wp_remote_retrieve_body( $request );
     215                        $result = json_decode( $response, true );
     216                        $cached_vendor_wallet_amount = number_format( $result['walletAmount'], 0 ) . " " . get_woocommerce_currency_symbol();
     217   
     218                        // Cache the result for 2 minutes
     219                        set_transient( $cache_key, $cached_vendor_wallet_amount, 2 * MINUTE_IN_SECONDS );
     220                    }
     221                }
     222               
     223                printf( esc_attr__('Wallet Amount: %s', 'cod24-shipping' ), $cached_vendor_wallet_amount );
     224            }
     225        }
    102226
    103227        /**
    104228         * Get COD24 token using username and password
    105          * @param params
    106          * @return token
    107          */
    108         public static function get_token()
    109         {
    110             $cod24_username = get_option('cod24_shipping_username_panel');
    111             $cod24_password = get_option('cod24__shipping_password_panel');
    112            
    113             if( !empty( $cod24_username ) && !empty( $cod24_password ) )
     229         * @param int|null $vendor_id Optional vendor ID
     230         * @return void
     231         */
     232        public static function get_token( $vendor_id = null )
     233        {
     234            // get credentials by vendor
     235            $credentials = self::get_credentials( $vendor_id );
     236           
     237            if( !empty( $credentials['username'] ) && !empty( $credentials['password'] ) )
    114238            {
    115239                // send to api
    116                 $api_protocol = ( get_option('cod24_shipping_api_secure_connection_enabled')=='yes' ) ? 'https://' : 'http://';
     240                $api_protocol = ( $credentials['secure'] == 'yes' ) ? 'https://' : 'http://';
    117241                $url  = $api_protocol.'api.cod24.ir/api/Account/getToken';
    118242                $body = array(
    119                     'userName' => esc_attr( $cod24_username ),
    120                     'password' => esc_attr( $cod24_password ),
     243                    'userName' => esc_attr( $credentials['username'] ),
     244                    'password' => esc_attr( $credentials['password'] ),
    121245                );
    122246   
     
    126250                $args = array(
    127251                    'method'      => 'POST',
    128                     'sslverify'   => false,
     252                    'sslverify'   => ( $credentials['secure'] == 'yes' ) ? true : false,
    129253                    'headers'     => array(
    130254                        'Content-Type'  => 'application/json',
     
    135259                $request = wp_remote_post( $url, $args );
    136260               
    137                 if ( ! is_wp_error( $request ) || wp_remote_retrieve_response_code( $request ) == 200 )
     261                if ( ! is_wp_error( $request ) && wp_remote_retrieve_response_code( $request ) == 200 )
    138262                {
    139263                    $response = wp_remote_retrieve_body( $request );
     
    141265                    if(!empty($result['token']))
    142266                    {
    143                         update_option('cod24_shipping_token', esc_attr( $result['token'] ) );
     267                        // store token
     268                        self::store_token( $result['token'], $vendor_id );
    144269                    }
    145270                }
     
    151276         *
    152277         * @param data
     278         * @param int|null $vendor_id Optional vendor ID
    153279         * @return final_result array | return code & total price of rate
    154280         */
    155         public static function get_rate( $data )
     281        public static function get_rate( $data, $vendor_id = null )
    156282        {
    157283            $final_result = [];
    158             $cod24_token = get_option('cod24_shipping_token');
     284            $cod24_token = self::get_stored_token( $vendor_id );
     285            $credentials = self::get_credentials( $vendor_id );
    159286           
    160287            if( !empty( $cod24_token ) )
     
    162289           
    163290                // send to api
    164                 $api_protocol = ( get_option('cod24_shipping_api_secure_connection_enabled')=='yes' ) ? 'https://' : 'http://';
     291                $api_protocol = ( $credentials['secure'] == 'yes' ) ? 'https://' : 'http://';
    165292                $url  = $api_protocol.'api.cod24.ir/api/Order/getPostPrice';
    166293                $body = array(
     
    179306                    'method'      => 'POST',
    180307                    'timeout'     => 500,
    181                     'sslverify'   => true,
     308                    'sslverify'   => ( $credentials['secure'] == 'yes' ) ? true : false,
    182309                    'headers'     => array(
    183310                        'Authorization' => 'Bearer '.$cod24_token,
     
    229356            $custom_total_weight = $cod24_info['total_weight']; // total weight for calculating of all products
    230357            $non_standard_package = $cod24_info['non_standard_package'];
    231             $carton_size = $cod24_info['carton_size'];
     358            $is_calc_srv_by_weight = isset( $cod24_info['is_calc_srv_by_weight'] ) ? $cod24_info['is_calc_srv_by_weight'] : 'no';
     359           
     360            // Set carton_size based on is_calc_srv_by_weight
     361            if( $is_calc_srv_by_weight == 'yes' )
     362            {
     363                $carton_size = 0; // If calculating by weight, set to 0
     364            }
     365            else
     366            {
     367                $carton_size = isset( $cod24_info['carton_size'] ) ? $cod24_info['carton_size'] : 0;
     368            }
    232369            $content_parcel = ( isset( $cod24_info['content_parcel'] ) && !empty( $cod24_info['content_parcel'] ) ) ? $cod24_info['content_parcel'] : '';
    233370
     
    376513                'contentParcell' => $content_parcel,
    377514                'idCartonType' => (int) $carton_size,
     515                'isCalcSrvByWeight' => ( $is_calc_srv_by_weight == 'yes' ) ? true : false,
    378516            );
    379517
     
    385523         *
    386524         * @param order_id int
     525         * @param int|null $vendor_id Optional vendor ID
    387526         * @return final_result array | return code & total price of rate
    388527         */
    389         public static function add_order( $order_id )
     528        public static function add_order( $order_id, $vendor_id = null )
    390529        {
    391530            $final_result = [];
    392             $cod24_token = get_option('cod24_shipping_token');
     531            $cod24_token = self::get_stored_token( $vendor_id );
     532            $credentials = self::get_credentials( $vendor_id );
    393533           
    394534            if( !empty( $cod24_token ) )
    395535            {
    396536                // send to api
    397                 $api_protocol = ( get_option('cod24_shipping_api_secure_connection_enabled')=='yes' ) ? 'https://' : 'http://';
     537                $api_protocol = ( $credentials['secure'] == 'yes' ) ? 'https://' : 'http://';
    398538                $url  = $api_protocol.'api.cod24.ir/api/Order/addOrder';
    399539                $sanitized_data = self::prepare_data( $order_id );
     
    405545                    'method'      => 'POST',
    406546                    'timeout'     => 500,
    407                     'sslverify'   => true,
     547                    'sslverify'   => ( $credentials['secure'] == 'yes' ) ? true : false,
    408548                    'headers'     => array(
    409549                        'Authorization' => 'Bearer '.$cod24_token,
     
    458598         *
    459599         * @param order_ids array
     600         * @param int|null $vendor_id Optional vendor ID
    460601         * @return final_result array | return code & total price of rate
    461602         */
    462         public static function add_order_batch( $order_ids )
     603        public static function add_order_batch( $order_ids, $vendor_id = null )
    463604        {
    464605            $final_result_list = [];
    465606            $sanitized_data = [];
    466             $cod24_token = get_option('cod24_shipping_token');
     607            $cod24_token = self::get_stored_token( $vendor_id );
     608            $credentials = self::get_credentials( $vendor_id );
    467609           
    468610            if( !empty( $cod24_token ) )
    469611            {
    470612                // send to api
    471                 $api_protocol = ( get_option('cod24_shipping_api_secure_connection_enabled')=='yes' ) ? 'https://' : 'http://';
     613                $api_protocol = ( $credentials['secure'] == 'yes' ) ? 'https://' : 'http://';
    472614                $url  = $api_protocol.'api.cod24.ir/api/Order/addOrderBatch';
    473615                if( !empty( $order_ids ) )
     
    485627                    'method'      => 'POST',
    486628                    'timeout'     => 500,
    487                     'sslverify'   => true,
     629                    'sslverify'   => ( $credentials['secure'] == 'yes' ) ? true : false,
    488630                    'headers'     => array(
    489631                        'Authorization' => 'Bearer '.$cod24_token,
     
    554696         *
    555697         * @param params
     698         * @param int|null $vendor_id Optional vendor ID
    556699         * @return final_result array | return code & total price of rate
    557700         */
    558         public static function suspend_order( $params )
     701        public static function suspend_order( $params, $vendor_id = null )
    559702        {
    560703            $final_result = [];
    561             $cod24_token = get_option('cod24_shipping_token');
     704            $cod24_token = self::get_stored_token( $vendor_id );
     705            $credentials = self::get_credentials( $vendor_id );
    562706           
    563707            if( !empty( $cod24_token ) )
    564708            {
    565709                // send to api
    566                 $api_protocol = ( get_option('cod24_shipping_api_secure_connection_enabled')=='yes' ) ? 'https://' : 'http://';
     710                $api_protocol = ( $credentials['secure'] == 'yes' ) ? 'https://' : 'http://';
    567711                $url  = $api_protocol.'api.cod24.ir/api/Order/suspendOrder';
    568712   
     
    573717                    'method'      => 'POST',
    574718                    'timeout'     => 500,
    575                     'sslverify'   => true,
     719                    'sslverify'   => ( $credentials['secure'] == 'yes' ) ? true : false,
    576720                    'headers'     => array(
    577721                        'Authorization' => 'Bearer '.$cod24_token,
     
    608752         *
    609753         * @param params
     754         * @param int|null $vendor_id Optional vendor ID
    610755         * @return final_result array | return code & total price of rate
    611756         */
    612         public static function ready_to_send_order( $params )
     757        public static function ready_to_send_order( $params, $vendor_id = null )
    613758        {
    614759            $final_result = [];
    615             $cod24_token = get_option('cod24_shipping_token');
     760            $cod24_token = self::get_stored_token( $vendor_id );
     761            $credentials = self::get_credentials( $vendor_id );
    616762           
    617763            if( !empty( $cod24_token ) )
    618764            {
    619765                // send to api
    620                 $api_protocol = ( get_option('cod24_shipping_api_secure_connection_enabled')=='yes' ) ? 'https://' : 'http://';
     766                $api_protocol = ( $credentials['secure'] == 'yes' ) ? 'https://' : 'http://';
    621767                $url  = $api_protocol.'api.cod24.ir/api/Order/readyToSend';
    622768               
     
    627773                    'method'      => 'POST',
    628774                    'timeout'     => 500,
    629                     'sslverify'   => true,
     775                    'sslverify'   => ( $credentials['secure'] == 'yes' ) ? true : false,
    630776                    'headers'     => array(
    631777                        'Authorization' => 'Bearer '.$cod24_token,
     
    662808         *
    663809         * @param params
     810         * @param int|null $vendor_id Optional vendor ID
    664811         * @return final_result array | return code & total price of rate
    665812         */
    666         public static function cancel_order( $params )
     813        public static function cancel_order( $params, $vendor_id = null )
    667814        {
    668815            $final_result = [];
    669             $cod24_token = get_option('cod24_shipping_token');
     816            $cod24_token = self::get_stored_token( $vendor_id );
     817            $credentials = self::get_credentials( $vendor_id );
    670818           
    671819            if( !empty( $cod24_token ) )
    672820            {
    673821                // send to api
    674                 $api_protocol = ( get_option('cod24_shipping_api_secure_connection_enabled')=='yes' ) ? 'https://' : 'http://';
     822                $api_protocol = ( $credentials['secure'] == 'yes' ) ? 'https://' : 'http://';
    675823                $url  = $api_protocol.'api.cod24.ir/api/Order/cancelOrder';
    676824   
     
    681829                    'method'      => 'POST',
    682830                    'timeout'     => 500,
    683                     'sslverify'   => true,
     831                    'sslverify'   => ( $credentials['secure'] == 'yes' ) ? true : false,
    684832                    'headers'     => array(
    685833                        'Authorization' => 'Bearer '.$cod24_token,
     
    716864         *
    717865         * @param serials
     866         * @param int|null $vendor_id Optional vendor ID
    718867         * @return final_result factor url preview
    719868         */
    720         public static function factor_preview( $serials )
     869        public static function factor_preview( $serials, $vendor_id = null )
    721870        {
    722871            $final_result = [];
    723             $cod24_token = get_option('cod24_shipping_token');
     872            $cod24_token = self::get_stored_token( $vendor_id );
     873            $credentials = self::get_credentials( $vendor_id );
    724874           
    725875            if( !empty( $cod24_token ) )
    726876            {
    727877                // send to api
    728                 $api_protocol = ( get_option('cod24_shipping_api_secure_connection_enabled')=='yes' ) ? 'https://' : 'http://';
     878                $api_protocol = ( $credentials['secure'] == 'yes' ) ? 'https://' : 'http://';
    729879                $url  = $api_protocol.'api.cod24.ir/api/Print/factorPreview';
    730880                $body = wp_json_encode( $serials );
     
    734884                    'method'      => 'POST',
    735885                    'timeout'     => 500,
    736                     'sslverify'   => true,
     886                    'sslverify'   => ( $credentials['secure'] == 'yes' ) ? true : false,
    737887                    'headers'     => array(
    738888                        'Authorization' => 'Bearer '.$cod24_token,
     
    763913                                'method'      => 'POST',
    764914                                'timeout'     => 500,
    765                                 'sslverify'   => true,
     915                                'sslverify'   => ( $credentials['secure'] == 'yes' ) ? true : false,
    766916                                'headers'     => array(
    767917                                    'Authorization' => esc_attr( $result['token'] ),
     
    790940         *
    791941         * @param serials
     942         * @param int|null $vendor_id Optional vendor ID
    792943         * @return final_result barcode url preview
    793944         */
    794         public static function barcode_preview( $serials )
     945        public static function barcode_preview( $serials, $vendor_id = null )
    795946        {
    796947            $final_result = [];
    797             $cod24_token = get_option('cod24_shipping_token');
     948            $cod24_token = self::get_stored_token( $vendor_id );
     949            $credentials = self::get_credentials( $vendor_id );
    798950           
    799951            if( !empty( $cod24_token ) )
    800952            {
    801953                // send to api
    802                 $api_protocol = ( get_option('cod24_shipping_api_secure_connection_enabled')=='yes' ) ? 'https://' : 'http://';
     954                $api_protocol = ( $credentials['secure'] == 'yes' ) ? 'https://' : 'http://';
    803955                $url  = $api_protocol.'api.cod24.ir/api/Print/barcodePreview';
    804956                $body = wp_json_encode( $serials );
     
    808960                    'method'      => 'POST',
    809961                    'timeout'     => 500,
    810                     'sslverify'   => true,
     962                    'sslverify'   => ( $credentials['secure'] == 'yes' ) ? true : false,
    811963                    'headers'     => array(
    812964                        'Authorization' => 'Bearer '.$cod24_token,
     
    837989                                'method'      => 'POST',
    838990                                'timeout'     => 500,
    839                                 'sslverify'   => true,
     991                                'sslverify'   => ( $credentials['secure'] == 'yes' ) ? true : false,
    840992                                'headers'     => array(
    841993                                    'Authorization' => esc_attr( $result['token'] ),
     
    8701022            $result = [];
    8711023           
    872             // send to api
    873             $api_protocol = ( get_option('cod24_shipping_api_secure_connection_enabled')=='yes' ) ? 'https://' : 'http://';
    874             $url  = $api_protocol.'cod24-learn.s3.ir-thr-at1.arvanstorage.ir/States.json';
     1024            // get states from cdn
     1025            $api_protocol = ( get_option('cod24_shipping_api_secure_connection_enabled') == 'yes' ) ? 'https://' : 'http://';
     1026            $url  = $api_protocol.'apicod24.s3.ir-thr-at1.arvanstorage.ir/States.json';
    8751027            $body = [];
    8761028
     
    9051057        {
    9061058            $result = [];
    907             $cod24_token = get_option('cod24_shipping_token');
    908            
    909             $api_protocol = ( get_option('cod24_shipping_api_secure_connection_enabled')=='yes' ) ? 'https://' : 'http://';
     1059           
     1060            // get cities form cdn
     1061            $api_protocol = ( get_option('cod24_shipping_api_secure_connection_enabled') == 'yes' ) ? 'https://' : 'http://';
    9101062            $url  = $api_protocol.'apicod24.s3.ir-thr-at1.arvanstorage.ir/Cities.json';
    9111063            $body = [];
     
    9301082                // Update the values of the 'cityNameFa' key for all elements in the array
    9311083                foreach ($result as &$item) {
    932                     $item['cityNameFa'] = self::sanitize_first_last_space_character($item['cityNameFa']);
     1084                    $item['cityNameFa'] = self::convert_arabic_to_persian_character( self::sanitize_first_last_space_character($item['cityNameFa']) );
    9331085                }
    9341086            }
     
    9791131                            'state_id' => $value['statePostCode'],
    9801132                            'city_id' => $value['cityPostCode'],
    981                             'city_name' => self::sanitize_first_last_space_character( $value['cityNameFa'] )
     1133                            'city_name' => self::convert_arabic_to_persian_character( self::sanitize_first_last_space_character( $value['cityNameFa'] ) )
    9821134                        ];
    9831135                    }
     
    10451197            $user_city_name=str_replace($arabic, $farsi, $user_city_name);
    10461198       
    1047             $sanitize_user_city_name = self::sanitize_first_last_space_character( $user_city_name );
     1199            $sanitize_user_city_name = self::convert_arabic_to_persian_character( self::sanitize_first_last_space_character( $user_city_name ) );
    10481200       
    10491201            $data = self::get_all_cities();
     
    10791231            return $str;
    10801232        }
     1233       
     1234        public static function convert_arabic_to_persian_character($string)
     1235        {
     1236            $characters = [
     1237                'ك' => 'ک',
     1238                'دِ' => 'د',
     1239                'بِ' => 'ب',
     1240                'زِ' => 'ز',
     1241                'ذِ' => 'ذ',
     1242                'شِ' => 'ش',
     1243                'سِ' => 'س',
     1244                'ى' => 'ی',
     1245                'ي' => 'ی',
     1246                '١' => '۱',
     1247                '٢' => '۲',
     1248                '٣' => '۳',
     1249                '٤' => '۴',
     1250                '٥' => '۵',
     1251                '٦' => '۶',
     1252                '٧' => '۷',
     1253                '٨' => '۸',
     1254                '٩' => '۹',
     1255                '٠' => '۰',
     1256            ];
     1257            return str_replace(array_keys($characters), array_values($characters),$string);
     1258        }
    10811259
    10821260        public static function get_carton_sizes()
  • cod24-shipping/trunk/inc/api/class-cod24-tipax-api.php

    r3346653 r3399647  
    3030
    3131        /**
     32         * Get credentials based on vendor_id
     33         *
     34         * @param int|null $vendor_id Optional vendor ID
     35         * @return array Array with username, password, and secure settings
     36         */
     37        private static function get_credentials( $vendor_id = null ) {
     38            if( !empty( $vendor_id ) && $vendor_id > 0 ) {
     39                // Use vendor credentials
     40                return array(
     41                    'username' => get_user_meta( $vendor_id, '_vendor_cod24_username', true ),
     42                    'password' => get_user_meta( $vendor_id, '_vendor_cod24_password', true ),
     43                    'secure'   => get_user_meta( $vendor_id, '_vendor_cod24_secure_connection', true ) ?: 'yes'
     44                );
     45            } else {
     46                // Use admin credentials
     47                return array(
     48                    'username' => get_option('cod24_shipping_username_panel'),
     49                    'password' => get_option('cod24__shipping_password_panel'),
     50                    'secure'   => get_option('cod24_shipping_api_secure_connection_enabled')
     51                );
     52            }
     53        }
     54
     55        /**
     56         * Get token based on vendor_id
     57         *
     58         * @param int|null $vendor_id Optional vendor ID
     59         * @return string|false Token or false if not found
     60         */
     61        private static function get_stored_token( $vendor_id = null ) {
     62            if( !empty( $vendor_id ) && $vendor_id > 0 ) {
     63                return get_user_meta( $vendor_id, '_vendor_cod24_shipping_token', true );
     64            } else {
     65                return get_option('cod24_shipping_token');
     66            }
     67        }
     68
     69        /**
    3270         * Get cod24 rate
    3371         *
    3472         * @param data
     73         * @param int|null $vendor_id Optional vendor ID
    3574         * @return final_result array | return code & total price of rate
    3675         */
    37         public static function get_rate( $data )
     76        public static function get_rate( $data, $vendor_id = null )
    3877        {
    3978            $final_result = [];
    40             $cod24_token = get_option('cod24_shipping_token');
     79            $cod24_token = self::get_stored_token( $vendor_id );
     80            $credentials = self::get_credentials( $vendor_id );
    4181           
    4282            if( !empty( $cod24_token ) )
     
    4484           
    4585                // send to api
    46                 $api_protocol = ( get_option('cod24_shipping_api_secure_connection_enabled')=='yes' ) ? 'https://' : 'http://';
     86                $api_protocol = ( $credentials['secure'] == 'yes' ) ? 'https://' : 'http://';
    4787                $url  = $api_protocol.'api.cod24.ir/api/OrderTipax/getTipaxPrice';
    4888                $body = array(
     
    64104                    'method'      => 'POST',
    65105                    'timeout'     => 500,
    66                     'sslverify'   => true,
     106                    'sslverify'   => ( $credentials['secure'] == 'yes' ) ? true : false,
    67107                    'headers'     => array(
    68108                        'Authorization' => 'Bearer '.$cod24_token,
     
    284324                'height' => ( !empty( $custom_total_dimension['height'] ) && $custom_total_dimension['height'] > 0 ) ? floatval( $custom_total_dimension['height'] ) : 0,
    285325                'isUnusual' => ( !empty( $non_standard_package ) && $non_standard_package == 'yes' ) ? (bool) 1 : (bool) 0,
    286                 'serviceId' => 1,
     326                'serviceId' => 2,
    287327                'idPickupType' => $idPickupType,
    288328                'idDistributionType' => $idDistributionType
     
    293333
    294334        /**
    295          * Add suspend order on cod24 panel
     335         * Add order on cod24 panel
    296336         *
    297337         * @param order_id int
     338         * @param int|null $vendor_id Optional vendor ID
    298339         * @return final_result array | return code & total price of rate
    299340         */
    300         public static function add_order( $order_id )
     341        public static function add_order( $order_id, $vendor_id = null )
    301342        {
    302343            $final_result = [];
    303344            $sanitized_data = [];
    304             $cod24_token = get_option('cod24_shipping_token');
     345            $cod24_token = self::get_stored_token( $vendor_id );
     346            $credentials = self::get_credentials( $vendor_id );
    305347           
    306348            if( !empty( $cod24_token ) )
    307349            {
    308350                // send to api
    309                 $api_protocol = ( get_option('cod24_shipping_api_secure_connection_enabled')=='yes' ) ? 'https://' : 'http://';
     351                $api_protocol = ( $credentials['secure'] == 'yes' ) ? 'https://' : 'http://';
    310352                $url  = $api_protocol.'api.cod24.ir/api/OrderTipax/addOrderBatch';
    311353                $sanitized_data[] = self::prepare_data( $order_id );
     
    317359                    'method'      => 'POST',
    318360                    'timeout'     => 500,
    319                     'sslverify'   => true,
     361                    'sslverify'   => ( $credentials['secure'] == 'yes' ) ? true : false,
    320362                    'headers'     => array(
    321363                        'Authorization' => 'Bearer '.$cod24_token,
     
    332374                    $result = json_decode( $response, true );
    333375                    $final_result['code'] = wp_remote_retrieve_response_code( $request );
    334                     $final_result['message'] = $result[0]['message'];
     376                    $final_result['message'] = $result['message'];
    335377                    if( !empty( $result[0]['errors'] ) )
    336378                    {
     
    370412         *
    371413         * @param order_ids array
     414         * @param int|null $vendor_id Optional vendor ID
    372415         * @return final_result array | return code & total price of rate
    373416         */
    374         public static function add_order_batch( $order_ids )
     417        public static function add_order_batch( $order_ids, $vendor_id = null )
    375418        {
    376419            $final_result_list = [];
    377420            $sanitized_data = [];
    378             $cod24_token = get_option('cod24_shipping_token');
     421            $cod24_token = self::get_stored_token( $vendor_id );
     422            $credentials = self::get_credentials( $vendor_id );
    379423           
    380424            if( !empty( $cod24_token ) )
    381425            {
    382426                // send to api
    383                 $api_protocol = ( get_option('cod24_shipping_api_secure_connection_enabled')=='yes' ) ? 'https://' : 'http://';
     427                $api_protocol = ( $credentials['secure'] == 'yes' ) ? 'https://' : 'http://';
    384428                $url  = $api_protocol.'api.cod24.ir/api/OrderTipax/addOrderBatch';
    385429                if( !empty( $order_ids ) )
     
    396440                    'method'      => 'POST',
    397441                    'timeout'     => 500,
    398                     'sslverify'   => true,
     442                    'sslverify'   => ( $credentials['secure'] == 'yes' ) ? true : false,
    399443                    'headers'     => array(
    400444                        'Authorization' => 'Bearer '.$cod24_token,
     
    464508         *
    465509         * @param params
     510         * @param int|null $vendor_id Optional vendor ID
    466511         * @return final_result array | return code & total price of rate
    467512         */
    468         public static function ready_to_send_order( $params )
     513        public static function ready_to_send_order( $params, $vendor_id = null )
    469514        {
    470515            $final_result = [];
    471             $cod24_token = get_option('cod24_shipping_token');
     516            $cod24_token = self::get_stored_token( $vendor_id );
     517            $credentials = self::get_credentials( $vendor_id );
    472518           
    473519            if( !empty( $cod24_token ) )
    474520            {
    475521                // send to api
    476                 $api_protocol = ( get_option('cod24_shipping_api_secure_connection_enabled')=='yes' ) ? 'https://' : 'http://';
     522                $api_protocol = ( $credentials['secure'] == 'yes' ) ? 'https://' : 'http://';
    477523                $url  = $api_protocol.'api.cod24.ir/api/OrderTipax/readyToSendOrder';
    478524               
     
    483529                    'method'      => 'POST',
    484530                    'timeout'     => 500,
    485                     'sslverify'   => true,
     531                    'sslverify'   => ( $credentials['secure'] == 'yes' ) ? true : false,
    486532                    'headers'     => array(
    487533                        'Authorization' => 'Bearer '.$cod24_token,
     
    518564         *
    519565         * @param params
     566         * @param int|null $vendor_id Optional vendor ID
    520567         * @return final_result array | return code & total price of rate
    521568         */
    522         public static function cancel_order( $params )
     569        public static function cancel_order( $params, $vendor_id = null )
    523570        {
    524571            $final_result = [];
    525             $cod24_token = get_option('cod24_shipping_token');
     572            $cod24_token = self::get_stored_token( $vendor_id );
     573            $credentials = self::get_credentials( $vendor_id );
    526574           
    527575            if( !empty( $cod24_token ) )
    528576            {
    529577                // send to api
    530                 $api_protocol = ( get_option('cod24_shipping_api_secure_connection_enabled')=='yes' ) ? 'https://' : 'http://';
     578                $api_protocol = ( $credentials['secure'] == 'yes' ) ? 'https://' : 'http://';
    531579                $url  = $api_protocol.'api.cod24.ir/api/OrderTipax/cancelOrder';
    532580   
     
    537585                    'method'      => 'POST',
    538586                    'timeout'     => 500,
    539                     'sslverify'   => true,
     587                    'sslverify'   => ( $credentials['secure'] == 'yes' ) ? true : false,
    540588                    'headers'     => array(
    541589                        'Authorization' => 'Bearer '.$cod24_token,
     
    579627            $result = [];
    580628           
    581             // send to api
    582             $api_protocol = ( get_option('cod24_shipping_api_secure_connection_enabled')=='yes' ) ? 'https://' : 'http://';
     629            // get tipax cities from cdn
     630            $api_protocol = ( get_option('cod24_shipping_api_secure_connection_enabled') == 'yes' ) ? 'https://' : 'http://';
    583631            $url  = $api_protocol.'apicod24.s3.ir-thr-at1.arvanstorage.ir/TipaxCities.json';
    584632            $body = [];
     
    634682        {
    635683            $result = [];
    636             $cod24_token = get_option('cod24_shipping_token');
    637            
    638             $api_protocol = ( get_option('cod24_shipping_api_secure_connection_enabled')=='yes' ) ? 'https://' : 'http://';
     684           
     685            // get all cities from api
     686            $api_protocol = ( get_option('cod24_shipping_api_secure_connection_enabled') == 'yes' ) ? 'https://' : 'http://';
    639687            $url  = $api_protocol.'apicod24.s3.ir-thr-at1.arvanstorage.ir/TipaxCities.json';
    640688            $body = [];
     
    659707                // Update the values of the 'cityNameFa' key for all elements in the array
    660708                foreach ($result as &$item) {
    661                     $item['cityNameFa'] = self::sanitize_first_last_space_character($item['cityNameFa']);
     709                    $item['cityNameFa'] = self::convert_arabic_to_persian_character( self::sanitize_first_last_space_character($item['cityNameFa']) );
    662710                }
    663711            }
     
    708756                            'state_id' => $value['stateCode'],
    709757                            'city_id' => $value['cityCode'],
    710                             'city_name' => self::sanitize_first_last_space_character( $value['cityNameFa'] )
     758                            'city_name' => self::convert_arabic_to_persian_character( self::sanitize_first_last_space_character( $value['cityNameFa'] ) )
    711759                        ];
    712760                    }
     
    727775        public static function find_city( $user_city_name )
    728776        {
    729             $sanitize_user_city_name = self::sanitize_first_last_space_character( $user_city_name );
     777            // convert user city characters from arabic to persian
     778            $arabic = array('ي', 'ك');
     779            $farsi = array('ی', 'ک');
     780            $user_city_name=str_replace($arabic, $farsi, $user_city_name);
     781           
     782            $sanitize_user_city_name = self::convert_arabic_to_persian_character( self::sanitize_first_last_space_character( $user_city_name ) );
    730783            $data = self::get_all_cities();
    731784
     
    759812       
    760813            return $str;
     814        }
     815       
     816        public static function convert_arabic_to_persian_character($string)
     817        {
     818            $characters = [
     819                'ك' => 'ک',
     820                'دِ' => 'د',
     821                'بِ' => 'ب',
     822                'زِ' => 'ز',
     823                'ذِ' => 'ذ',
     824                'شِ' => 'ش',
     825                'سِ' => 'س',
     826                'ى' => 'ی',
     827                'ي' => 'ی',
     828                '١' => '۱',
     829                '٢' => '۲',
     830                '٣' => '۳',
     831                '٤' => '۴',
     832                '٥' => '۵',
     833                '٦' => '۶',
     834                '٧' => '۷',
     835                '٨' => '۸',
     836                '٩' => '۹',
     837                '٠' => '۰',
     838            ];
     839            return str_replace(array_keys($characters), array_values($characters),$string);
    761840        }
    762841       
  • cod24-shipping/trunk/inc/class-cod24-shcod.php

    r3254068 r3399647  
    11<?php
    22/**
    3  * COD24 API
    4  * This class is connecting to COD24 API.
     3 * COD24 SHCOD
     4 * This class is for shipping cost on delivery
    55 *
    66 * Package: COD24 Shipping
  • cod24-shipping/trunk/inc/class-cod24-woo.php

    r3366748 r3399647  
    319319            $fields['city']['priority'] = 60;
    320320            $fields['address_1']['priority'] = 70;
    321             //$fields['address_2']['priority'] = 80;
     321           
    322322            return $fields;
    323323        }
     
    486486                    </div>
    487487                    <div class="cod24-meta-fields" style="margin-bottom:10px">
     488                        <input type="checkbox" class="postbox is_calc_srv_by_weight" id="is_calc_srv_by_weight" style="margin-bottom:0px" name="is_calc_srv_by_weight" value="yes" <?php if( !empty( $order_cod24_info ) && isset( $order_cod24_info['is_calc_srv_by_weight'] ) && $order_cod24_info['is_calc_srv_by_weight'] == 'yes' ) checked( 'yes', $order_cod24_info['is_calc_srv_by_weight'] ); ?>/>
     489                        <label for="is_calc_srv_by_weight"><?php esc_attr_e('Calculate service cost by weight', 'cod24-shipping'); ?></label>
     490                        <div class="description"><?php esc_attr_e('If checked, carton size will be set to 0 and service cost will be calculated based on weight only.', 'cod24-shipping'); ?></div>
     491                    </div>
     492                    <div class="cod24-meta-fields" style="margin-bottom:10px">
    488493                        <div class="label" style="margin-bottom:10px"><label for="carton_size"><?php esc_attr_e('Carton Size', 'cod24-shipping'); ?></label></div>
    489                         <select name="carton_size" id="carton_size" class="postbox carton_size" style="margin-bottom:10px">
     494                        <select name="carton_size" id="carton_size" class="postbox carton_size" style="margin-bottom:10px" <?php if( !empty( $order_cod24_info ) && isset( $order_cod24_info['is_calc_srv_by_weight'] ) && $order_cod24_info['is_calc_srv_by_weight'] == 'yes' ) echo 'disabled'; ?>>
    490495                            <option value="none"><?php esc_attr_e('Select the size...','cod24-shipping'); ?></option>
    491496                            <?php foreach(COD24_API::get_carton_sizes() as $key => $carton_item) : ?>
     
    660665                        $cod24_info['non_standard_package'] = 'no';
    661666                   
    662                     // check carton size
    663                     $carton_sizes = COD24_API::get_carton_sizes();
    664                     $user_carton_size = sanitize_text_field( $_POST['carton_size'] );
    665                     if( in_array( $user_carton_size, array_keys( $carton_sizes ) ) )
    666                     {
    667                         $cod24_info['carton_size'] = $user_carton_size;
    668                     }
    669                    
    670                     // check content parcel
    671                     if( isset( $_POST['content_parcel'] ) && !empty( $_POST['content_parcel'] ) )
    672                     {
    673                         $cod24_info['content_parcel'] = sanitize_textarea_field( $_POST['content_parcel'] );
    674                     }
     667                    // check is_calc_srv_by_weight
     668                    if( isset( $_POST['is_calc_srv_by_weight'] ) && $_POST['is_calc_srv_by_weight'] == 'yes' )
     669                    {
     670                        $cod24_info['is_calc_srv_by_weight'] = 'yes';
     671                        $cod24_info['carton_size'] = 0; // Set carton size to 0 when calculating by weight
     672                    }
     673                    else
     674                    {
     675                        $cod24_info['is_calc_srv_by_weight'] = 'no';
     676                        // check carton size
     677                        $carton_sizes = COD24_API::get_carton_sizes();
     678                        $user_carton_size = sanitize_text_field( $_POST['carton_size'] );
     679                        if( in_array( $user_carton_size, array_keys( $carton_sizes ) ) )
     680                        {
     681                            $cod24_info['carton_size'] = $user_carton_size;
     682                        }
     683                    }
     684                       
     685                        // check content parcel
     686                        if( isset( $_POST['content_parcel'] ) && !empty( $_POST['content_parcel'] ) )
     687                        {
     688                            $cod24_info['content_parcel'] = sanitize_textarea_field( $_POST['content_parcel'] );
     689                        }
    675690                }
    676691                elseif( $old_cod24_info['shipping_type'] == 'tipax' && isset( $_POST['tipax_custom_total_weight'] ) && !empty( $_POST['tipax_custom_total_weight'] ) )
     
    763778            {
    764779                $order_id = intval( $_REQUEST['order_id'] );
     780               
    765781                $cod24_new_status = sanitize_text_field( $_REQUEST['cod24_status'] ); // user enter custom total weight
    766 
     782   
    767783                // check and get order info
    768784                if( !empty( $order_id ) && absint( $order_id ) > 0 )
     
    790806                                $cod24_info['non_standard_package'] = 'no';
    791807                           
    792                             // check carton size
    793                             $carton_sizes = COD24_API::get_carton_sizes();
    794                             $user_carton_size = sanitize_text_field( $_POST['carton_size'] );
    795                             if( in_array( $user_carton_size, array_keys( $carton_sizes ) ) )
    796                             {
    797                                 $cod24_info['carton_size'] = $user_carton_size;
    798                             }
     808                            // check is_calc_srv_by_weight
     809                            if( isset( $_POST['is_calc_srv_by_weight'] ) && $_POST['is_calc_srv_by_weight'] == 'yes' )
     810                            {
     811                                $cod24_info['is_calc_srv_by_weight'] = 'yes';
     812                                $cod24_info['carton_size'] = 0; // Set carton size to 0 when calculating by weight
     813                            }
     814                            else
     815                            {
     816                                $cod24_info['is_calc_srv_by_weight'] = 'no';
     817                                // check carton size
     818                                $carton_sizes = COD24_API::get_carton_sizes();
     819                                $user_carton_size = sanitize_text_field( $_POST['carton_size'] );
     820                                if( in_array( $user_carton_size, array_keys( $carton_sizes ) ) )
     821                                {
     822                                    $cod24_info['carton_size'] = $user_carton_size;
     823                                }
     824                            }
    799825                           
    800826                            // check content parcel
     
    890916                                else
    891917                                {
     918                                    // Get vendor id
     919                                    $vendor_id = self::get_vendor_id_for_shipping( $order_id );
     920                                   
    892921                                    if( $cod24_info['shipping_type'] == 'tipax' )
    893                                         $result = COD24_Tipax_API::add_order( $order_id );
     922                                        $result = COD24_Tipax_API::add_order( $order_id, $vendor_id );
    894923                                    else
    895                                         $result = COD24_API::add_order( $order_id );
     924                                        $result = COD24_API::add_order( $order_id, $vendor_id );
    896925   
    897926                                    if( !empty( $result['serial'] ) )
     
    9771006                                    if( $cod24_info['shipping_type'] == 'post' ) // suspend order only for post shipping
    9781007                                    {
    979                                         $result = COD24_API::suspend_order( array( array( 'serial' => absint( $cod24_info['serial'] ), 'idOrderShop' =>  strval( $order_id ) ) ) );
     1008                                        // Get vendor id
     1009                                        $vendor_id = self::get_vendor_id_for_shipping( $order_id );
     1010                                       
     1011                                        $result = COD24_API::suspend_order( array( array( 'serial' => absint( $cod24_info['serial'] ), 'idOrderShop' =>  strval( $order_id ) ) ), $vendor_id );
    9801012                                       
    9811013                                        if( !empty( $result[0]['barcode'] ) && $result[0]['isSuccess'] && $result[0]['code'] == 0 )
     
    10621094                                    else
    10631095                                    {
    1064                                         $result = COD24_API::ready_to_send_order( array( array( 'serial' => absint( $cod24_info['serial'] ), 'idOrderShop' =>  strval( $order_id ) ) ) );
     1096                                        // Get vendor id
     1097                                        $vendor_id = self::get_vendor_id_for_shipping( $order_id );
     1098                                       
     1099                                        $result = COD24_API::ready_to_send_order( array( array( 'serial' => absint( $cod24_info['serial'] ), 'idOrderShop' =>  strval( $order_id ) ) ), $vendor_id );
    10651100       
    10661101                                        if( $result[0]['isSuccess'] && $result[0]['code'] == 0 )
     
    11121147                                else
    11131148                                {
    1114                                     $result = COD24_Tipax_API::ready_to_send_order( array( array( 'serial' => absint( $cod24_info['serial'] ), 'idOrderShop' =>  strval( $order_id ) ) ) );
     1149                                    // Get vendor id
     1150                                    $vendor_id = self::get_vendor_id_for_shipping( $order_id );
     1151                                   
     1152                                    $result = COD24_Tipax_API::ready_to_send_order( array( array( 'serial' => absint( $cod24_info['serial'] ), 'idOrderShop' =>  strval( $order_id ) ) ), $vendor_id );
    11151153       
    11161154                                    if( $result[0]['isSuccess'] && $result[0]['code'] == 0 )
     
    11641202                            else
    11651203                            {
     1204                                // Get vendor id
     1205                                $vendor_id = self::get_vendor_id_for_shipping( $order_id );
     1206                                   
    11661207                                if( $cod24_info['shipping_type'] == 'tipax' )
    1167                                     $result = COD24_Tipax_API::cancel_order( array( array( 'serial' => $cod24_info['serial'], 'idOrderShop' =>  strval( $order_id ) ) ) );
     1208                                    $result = COD24_Tipax_API::cancel_order( array( array( 'serial' => $cod24_info['serial'], 'idOrderShop' =>  strval( $order_id ) ) ), $vendor_id );
    11681209                                else
    1169                                     $result = COD24_API::cancel_order( array( array( 'serial' => $cod24_info['serial'], 'idOrderShop' =>  strval( $order_id ) ) ) );
     1210                                    $result = COD24_API::cancel_order( array( array( 'serial' => $cod24_info['serial'], 'idOrderShop' =>  strval( $order_id ) ) ), $vendor_id );
    11701211   
    11711212                                if( $result[0]['isSuccess'] && $result[0]['code'] == 0 )
     
    12151256                            $result = array( 'status' => false, 'msg' => $error_message, 'url' => add_query_arg( array( 'post' => $order_id , 'action' => 'edit' ), admin_url( 'post.php' ) ) );
    12161257                        else
    1217                             $result = array( 'status' => true, 'url' => add_query_arg( array( 'post' => $order_id , 'action' => 'edit' ), admin_url( 'post.php' ) ) );
     1258                            $result = array( 'status' => true );
    12181259                    }
    12191260                    else
     
    12541295                    if( !empty( $cod24_info['serial'] ) )
    12551296                    {
     1297                        $vendor_id = self::get_vendor_id_for_shipping( $order_id );
     1298                       
    12561299                        $data[] = [ 'serial' => (int) $cod24_info['serial'] ];
    1257                         $factor_preview_result = COD24_API::factor_preview( $data );
     1300                        $factor_preview_result = COD24_API::factor_preview( $data, $vendor_id );
    12581301                        if( !empty( $factor_preview_result['url'] ) )
    12591302                        {
     
    12631306                        else
    12641307                        {
    1265                             $message = sprintf( esc_attr__('There was an error in getting factor preview from COD24 panel. Error: %s ', 'cod24-shipping'), $result['code']. " - ". $result['message'] );
     1308                            $message = sprintf( esc_attr__('There was an error in getting factor preview from COD24 panel. Error: %s ', 'cod24-shipping'), $factor_preview_result['code']. " - ". $factor_preview_result['message'] );
    12661309                            $result = array('status' => false , 'msg' => $message );
    12671310                        }
     
    18201863                                   
    18211864                                    if( !empty( $post_sanitized_data ) )
    1822                                         $post_result = COD24_API::add_order_batch( $post_sanitized_data );
     1865                                    {
     1866                                        // Get first vendor id
     1867                                        $vendor_id = self::get_vendor_id_for_shipping( $post_sanitized_data[0] );
     1868                                       
     1869                                        $post_result = COD24_API::add_order_batch( $post_sanitized_data, $vendor_id );
     1870                                    }
    18231871                                       
    18241872                                    if( !empty( $tipax_sanitized_data ) )
    1825                                         $tipax_result = COD24_Tipax_API::add_order_batch( $tipax_sanitized_data );
     1873                                    {
     1874                                        // Get first vendor id
     1875                                        $vendor_id = self::get_vendor_id_for_shipping( $tipax_sanitized_data[0] );
     1876                                       
     1877                                        $tipax_result = COD24_Tipax_API::add_order_batch( $tipax_sanitized_data, $vendor_id );
     1878                                    }
    18261879                                       
    18271880                                    $result = array_merge($post_result, $tipax_result);
    1828 
    1829                                     error_log("RESULT DEBUG BATCH:" . print_r($result, true ) );
    18301881
    18311882                                    if( !empty( $result ) )
     
    19211972                                    $post_result = [];
    19221973                                    $tipax_result = [];
     1974                                   
    19231975                                    if( !empty( $post_sanitized_data ) )
    1924                                         $post_result = COD24_API::suspend_order( $post_sanitized_data );
     1976                                    {
     1977                                        // Get first vendor id
     1978                                        $vendor_id = self::get_vendor_id_for_shipping( $post_sanitized_data[0] );
     1979                                       
     1980                                        $post_result = COD24_API::suspend_order( $post_sanitized_data, $vendor_id );
     1981                                    }
    19251982                                       
    19261983                                    $result = array_merge($post_result, $tipax_result);
     
    19892046                                   
    19902047                                    if( !empty( $post_sanitized_data ) )
    1991                                         $post_result = COD24_API::ready_to_send_order( $post_sanitized_data );
     2048                                    {
     2049                                        // Get first vendor id
     2050                                        $vendor_id = self::get_vendor_id_for_shipping( $post_sanitized_data[0] );
     2051                                       
     2052                                        $post_result = COD24_API::ready_to_send_order( $post_sanitized_data, $vendor_id );
     2053                                    }
    19922054                                       
    19932055                                    if( !empty( $tipax_sanitized_data ) )
    1994                                         $tipax_result = COD24_Tipax_API::ready_to_send_order( $tipax_sanitized_data );
     2056                                    {
     2057                                        // Get first vendor id
     2058                                        $vendor_id = self::get_vendor_id_for_shipping( $tipax_sanitized_data[0] );
     2059                                       
     2060                                        $tipax_result = COD24_Tipax_API::ready_to_send_order( $tipax_sanitized_data, $vendor_id );
     2061                                    }
    19952062                                       
    19962063                                    $result = array_merge($post_result, $tipax_result);
     
    20582125                                    $post_result = [];
    20592126                                    $tipax_result = [];
     2127                                       
    20602128                                    if( !empty( $post_sanitized_data ) )
    2061                                         $post_result = COD24_API::cancel_order( $post_sanitized_data );
     2129                                    {
     2130                                        // Get first vendor id
     2131                                        $vendor_id = self::get_vendor_id_for_shipping( $post_sanitized_data[0] );
     2132                                       
     2133                                        $post_result = COD24_API::cancel_order( $post_sanitized_data, $vendor_id );
     2134                                    }
    20622135                                       
    20632136                                    if( !empty( $tipax_sanitized_data ) )
    2064                                         $tipax_result = COD24_Tipax_API::cancel_order( $tipax_sanitized_data );
     2137                                    {
     2138                                        // Get first vendor id
     2139                                        $vendor_id = self::get_vendor_id_for_shipping( $tipax_sanitized_data[0] );
     2140                                       
     2141                                        $tipax_result = COD24_Tipax_API::cancel_order( $tipax_sanitized_data, $vendor_id );
     2142                                    }
    20652143                                       
    20662144                                    $result = array_merge($post_result, $tipax_result);
     
    22182296            wp_die();
    22192297        }
     2298       
     2299        public static function is_dokan_shipping_enabled()
     2300        {
     2301            // Dokan core must be active
     2302            if ( ! class_exists( 'WeDevs_Dokan' ) && ! function_exists( 'dokan' ) ) {
     2303                return false;
     2304            }
     2305       
     2306            // Dokan Pro must be active (several possible checks)
     2307            $pro_available = defined( 'DOKAN_PRO_INC' ) || function_exists( 'dokan_pro' ) || class_exists( 'WeDevs_Dokan_Pro' );
     2308            if ( ! $pro_available ) {
     2309                return false;
     2310            }
     2311       
     2312            // Common WooCommerce / Dokan option name you mentioned:
     2313            $opt = get_option( 'woocommerce_dokan_product_shipping_enabled', null );
     2314            if ( $opt != '1' ) {
     2315                return false;
     2316            }
     2317           
     2318            return true;
     2319        }
     2320       
     2321        /**
     2322         * Get vendor ID for appropriate shipping token usage
     2323         *
     2324         * @param int $order_id Order ID
     2325         * @return int|null Returns vendor_id if valid vendor exists, null otherwise (uses admin token)
     2326         */
     2327        public static function get_vendor_id_for_shipping($order_id)
     2328        {
     2329            // Default: use admin settings
     2330            $vendor_id = null;
     2331           
     2332            // Check if Dokan Shipping is enabled
     2333            if (!self::is_dokan_shipping_enabled()) {
     2334                return null; // Use admin token
     2335            }
     2336           
     2337            // Check if dokan function exists
     2338            if (!function_exists('dokan_get_seller_id_by_order')) {
     2339                return null; // Use admin token
     2340            }
     2341           
     2342            // Get vendor ID from order
     2343            $vendor_ids = \dokan_get_seller_id_by_order($order_id);
     2344           
     2345            // Get the first vendor
     2346            $potential_vendor_id = (is_array($vendor_ids)) ? reset($vendor_ids) : (int) $vendor_ids;
     2347           
     2348            // If vendor_id is not valid
     2349            if (empty($potential_vendor_id) || $potential_vendor_id <= 0) {
     2350                return null;
     2351            }
     2352           
     2353            // Check if this user is actually a vendor/seller
     2354            if (function_exists('dokan_is_user_seller')) {
     2355                if (dokan_is_user_seller($potential_vendor_id)) {
     2356                    return $potential_vendor_id; // Return valid vendor_id
     2357                }
     2358            }
     2359           
     2360            // If not a valid vendor, use admin token
     2361            return null;
     2362        }
    22202363
    22212364    } // END class COD24_WOO
  • cod24-shipping/trunk/inc/shipping/class-cod24-pishtaz.php

    r3354882 r3399647  
    9999                    if(empty($user_delivery_address['state_id']) && empty($user_delivery_address['city_id']))
    100100                        $available = false;
     101                   
     102                    // Check weight range
     103                    if ( $available && !empty($package['contents']) ) {
     104                        $min_weight = 30; // gram
     105                        $max_weight = 20000; // gram
     106                        $weight = 0;
     107                       
     108                        foreach ($package['contents'] as $item_id => $values) {
     109                            $_product = $values['data'];
     110                            $weight = $weight + ( floatval( $_product->get_weight() ) * absint( $values['quantity'] ) );
     111                        }
     112                       
     113                        // Convert weight to grams
     114                        $weight = wc_get_weight($weight, 'g');
     115                       
     116                        // If weight is outside the valid range, make shipping unavailable
     117                        if ($weight < $min_weight || $weight > $max_weight) {
     118                            $available = false;
     119                        }
     120                    }
    101121               
    102122                    return apply_filters( 'woocommerce_shipping_' . $this->id . '_is_available', $available, $package, $this );
     
    242262    function C24_pishtaz_shipping($methods)
    243263    {
    244         $methods['cod24_pishtaz'] = 'cod24_pishtaz_shipping';
     264        $methods['cod24_pishtaz'] = 'COD24_Pishtaz_Shipping';
    245265        return $methods;
    246266    }
  • cod24-shipping/trunk/inc/shipping/class-cod24-special.php

    r3354882 r3399647  
    104104                    if(empty($user_delivery_address['state_id']) && empty($user_delivery_address['city_id']))
    105105                        $available = false;
     106                   
     107                    // Check weight range
     108                    if ( $available && !empty($package['contents']) ) {
     109                        $min_weight = 30; // gram
     110                        $max_weight = 20000; // gram
     111                        $weight = 0;
     112                       
     113                        foreach ($package['contents'] as $item_id => $values) {
     114                            $_product = $values['data'];
     115                            $weight = $weight + ( floatval( $_product->get_weight() ) * absint( $values['quantity'] ) );
     116                        }
     117                       
     118                        // Convert weight to grams
     119                        $weight = wc_get_weight($weight, 'g');
     120                       
     121                        // If weight is outside the valid range, make shipping unavailable
     122                        if ($weight < $min_weight || $weight > $max_weight) {
     123                            $available = false;
     124                        }
     125                    }
    106126               
    107127                    return apply_filters( 'woocommerce_shipping_' . $this->id . '_is_available', $available, $package, $this );
     
    247267    function C24_special_shipping($methods)
    248268    {
    249         $methods['cod24_special'] = 'cod24_special_shipping';
     269        $methods['cod24_special'] = 'COD24_Special_Shipping';
    250270        return $methods;
    251271    }
  • cod24-shipping/trunk/inc/shipping/class-cod24-tipax.php

    r3264094 r3399647  
    108108                        $available = false;
    109109                    }
     110
     111                    // Check weight range
     112                    if ( $available && !empty($package['contents']) ) {
     113                        $min_weight = 30; // gram
     114                        $max_weight = 20000; // gram
     115                        $weight = 0;
     116                       
     117                        foreach ($package['contents'] as $item_id => $values) {
     118                            $_product = $values['data'];
     119                            $weight = $weight + ( floatval( $_product->get_weight() ) * absint( $values['quantity'] ) );
     120                        }
     121                       
     122                        // Convert weight to grams
     123                        $weight = wc_get_weight($weight, 'g');
     124                       
     125                        // If weight is outside the valid range, make shipping unavailable
     126                        if ($weight < $min_weight || $weight > $max_weight) {
     127                            $available = false;
     128                        }
     129                    }
    110130               
    111131                    return apply_filters( 'woocommerce_shipping_' . $this->id . '_is_available', $available, $package, $this );
     
    159179    function C24_tipax_shipping($methods)
    160180    {
    161         $methods['cod24_tipax'] = 'cod24_tipax_shipping';
     181        $methods['cod24_tipax'] = 'COD24_Tipax_Shipping';
    162182        return $methods;
    163183    }
  • cod24-shipping/trunk/readme.txt

    r3366748 r3399647  
    44Requires at least: 5.0
    55Tested up to: 6.8
    6 Stable tag: 4.0.10
     6Stable tag: 5.0
    77License: GPL-2.0+
    88License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    2222- Ability to activate Tipax shipping
    2323- Ability to activate Chapar & Alopeyk (coming soon)
     24- Dokan Support
    2425
    2526== Installation ==
     
    4344
    4445== Changelog ==
     46
     47= 5.0 =
     48* Dokan (Multi-Vendor) Support
     49* Bugs fixes and improvements
    4550
    4651= 4.0.10 =
Note: See TracChangeset for help on using the changeset viewer.