Shipi + FedEx
id = 'hitshippo_fedex'; $this->method_title = __('FedEx'); // Title shown in admin $this->title = __('Fedex'); $this->method_description = __('Fedex shipping method for calculating shipping rates.'); $this->instance_id = absint($instance_id); $this->enabled = "yes"; // This can be added as an setting but for this example its forced enabled $this->supports = array( 'shipping-zones', 'instance-settings', 'instance-settings-modal', ); $this->init(); if (get_option("woocommerce_custom_orders_table_enabled") === "yes") { $this->hpos_enabled = true; } } /** * Init your settings * * @access public * @return void */ function init() { // Load the settings API $this->init_form_fields(); // This is part of the settings API. Override the method to add your own settings $this->init_settings(); // This is part of the settings API. Loads settings you previously init. $this->title = $this->get_option('title'); // Save settings in admin if you have any defined add_action('woocommerce_update_options_shipping_' . $this->id, array($this, 'process_admin_options')); } /** * calculate_shipping function. * * @access public * @param mixed $package * @return void */ public function calculate_shipping($package = array()) { $pack_aft_hook = apply_filters('hitshippo_fedex_rate_packages', $package); $add_cost_to_live_rates = 0; $manual_flat_rates = apply_filters('shipi_fedex_manual_flat_rates', array(), $package); if (!empty($manual_flat_rates) && is_array($manual_flat_rates) && isset($manual_flat_rates[0]['rate_code']) && isset($manual_flat_rates[0]['name']) && isset($manual_flat_rates[0]['rate'])) { foreach ($manual_flat_rates as $manual_flat_rate) { $rate = array( 'id' => 'hitshippo' . $manual_flat_rate['rate_code'], 'label' => $manual_flat_rate['name'], 'cost' => $manual_flat_rate['rate'], 'meta_data' => array('hitshippo_fedex_multi_ven' => '', 'hitshippo_fedex_service' => $manual_flat_rate['rate_code'], 'hitshippo_fedex_shipping_charge' => $manual_flat_rate['rate']) ); $this->add_rate($rate); } return; }elseif(!empty($manual_flat_rates) && is_array($manual_flat_rates) && isset($manual_flat_rates['total_cost'])){ $add_cost_to_live_rates = $manual_flat_rates['total_cost']; } if (empty($pack_aft_hook)) { return; } $execution_status = get_option('hitshipo_fedex_working_status'); if (!empty($execution_status)) { if ($execution_status == 'stop_working') { return; } } $_carriers = array( 'FIRST_OVERNIGHT' => 'FedEx First Overnight', 'PRIORITY_OVERNIGHT' => 'FedEx Priority Overnight', 'STANDARD_OVERNIGHT' => 'FedEx Standard Overnight', 'FEDEX_2_DAY_AM' => 'FedEx 2Day A.M', 'FEDEX_2_DAY' => 'FedEx 2Day', 'SAME_DAY' => 'FedEx Same Day', 'SAME_DAY_CITY' => 'FedEx Same Day City', 'SAME_DAY_METRO_AFTERNOON' => 'FedEx Same Day Metro Afternoon', 'SAME_DAY_METRO_MORNING' => 'FedEx Same Day Metro Morning', 'SAME_DAY_METRO_RUSH' => 'FedEx Same Day Metro Rush', 'FEDEX_EXPRESS_SAVER' => 'FedEx Express Saver', 'GROUND_HOME_DELIVERY' => 'FedEx Ground Home Delivery', 'FEDEX_GROUND' => 'FedEx Ground', 'INTERNATIONAL_ECONOMY' => 'International Economy', 'INTERNATIONAL_ECONOMY_DISTRIBUTION' => 'International Economy Distribution', 'INTERNATIONAL_FIRST' => 'International First', 'INTERNATIONAL_GROUND' => 'International Ground', 'INTERNATIONAL_PRIORITY' => 'International Priority', 'INTERNATIONAL_PRIORITY_DISTRIBUTION' => 'International Priority Distribution', 'EUROPE_FIRST_INTERNATIONAL_PRIORITY' => 'FedEx Europe First International Priority', 'INTERNATIONAL_PRIORITY_EXPRESS' => 'FedEx International Priority Express', 'FEDEX_INTERNATIONAL_PRIORITY_PLUS' => 'FedEx First International Priority Plus', 'FEDEX_INTERNATIONAL_PRIORITY_EXPRESS' => 'Fedex international priority express', 'FEDEX_INTERNATIONAL_PRIORITY' => 'Fedex international priority', 'FEDEX_INTERNATIONAL_CONNECT_PLUS' => 'Fedex international connect plus', 'INTERNATIONAL_DISTRIBUTION_FREIGHT' => 'FedEx International Distribution Fright', 'FEDEX_1_DAY_FREIGHT' => 'FedEx 1 Day Freight', 'FEDEX_2_DAY_FREIGHT' => 'FedEx 2 Day Freight', 'FEDEX_3_DAY_FREIGHT' => 'FedEx 3 Day Freight', 'INTERNATIONAL_ECONOMY_FREIGHT' => 'FedEx Economy Freight', 'INTERNATIONAL_PRIORITY_FREIGHT' => 'FedEx Priority Freight', 'SMART_POST' => 'FedEx Smart Post', 'FEDEX_FIRST_FREIGHT' => 'FedEx First Freight', 'FEDEX_FREIGHT_ECONOMY' => 'FedEx Freight Economy', 'FEDEX_FREIGHT_PRIORITY' => 'FedEx Freight Priority', 'FEDEX_CARGO_AIRPORT_TO_AIRPORT' => 'FedEx CARGO Airport to Airport', 'FEDEX_CARGO_FREIGHT_FORWARDING' => 'FedEx CARGO Freight FOrwarding', 'FEDEX_CARGO_INTERNATIONAL_EXPRESS_FREIGHT' => 'FedEx CARGO International Express Fright', 'FEDEX_CARGO_INTERNATIONAL_PREMIUM' => 'FedEx CARGO International Premium', 'FEDEX_CARGO_MAIL' => 'FedEx CARGO Mail', 'FEDEX_CARGO_REGISTERED_MAIL' => 'FedEx CARGO Registered Mail', 'FEDEX_CARGO_SURFACE_MAIL' => 'FedEx CARGO Surface Mail', 'FEDEX_CUSTOM_CRITICAL_AIR_EXPEDITE_EXCLUSIVE_USE' => 'FedEx Custom Critical Air Expedite Exclusive Use', 'FEDEX_CUSTOM_CRITICAL_AIR_EXPEDITE_NETWORK' => 'FedEx Custom Critical Air Expedite Network', 'FEDEX_CUSTOM_CRITICAL_CHARTER_AIR' => 'FedEx Custom Critical Charter Air', 'FEDEX_CUSTOM_CRITICAL_POINT_TO_POINT' => 'FedEx Custom Critical Point to Point', 'FEDEX_CUSTOM_CRITICAL_SURFACE_EXPEDITE' => 'FedEx Custom Critical Surface Expedite', 'FEDEX_CUSTOM_CRITICAL_SURFACE_EXPEDITE_EXCLUSIVE_USE' => 'FedEx Custom Critical Surface Expedite Exclusive Use', 'FEDEX_CUSTOM_CRITICAL_TEMP_ASSURE_AIR' => 'FedEx Custom Critical Temp Assure Air', 'FEDEX_CUSTOM_CRITICAL_TEMP_ASSURE_VALIDATED_AIR' => 'FedEx Custom Critical Temp Assure Validated Air', 'FEDEX_CUSTOM_CRITICAL_WHITE_GLOVE_SERVICES' => 'FedEx Custom Critical White Glove Services', 'TRANSBORDER_DISTRIBUTION_CONSOLIDATION' => 'Fedex Transborder Distribution Consolidation', 'FEDEX_DISTANCE_DEFERRED' => 'FedEx Distance Deferred', 'FEDEX_NEXT_DAY_EARLY_MORNING' => 'FedEx Next Day Early Morning', 'FEDEX_NEXT_DAY_MID_MORNING' => 'FedEx Next Day Mid Morning', 'FEDEX_NEXT_DAY_AFTERNOON' => 'FedEx Next Day Afternoon', 'FEDEX_NEXT_DAY_END_OF_DAY' => 'FedEx Next Day End of Day', 'FEDEX_NEXT_DAY_FREIGHT' => 'FedEx Next Day Freight', 'FEDEX_REGIONAL_ECONOMY' => 'FedEx Regional Economy', 'FEDEX_REGIONAL_ECONOMY_DIST' => 'FedEx Regional Economy Distribution', 'FEDEX_REGIONAL_FIRST' => 'FedEx Regional First', 'FEDEX_REGIONAL_GROUND' => 'FedEx Regional Ground', 'FEDEX_REGIONAL_PRIORITY' => 'FedEx Regional Priority', 'FEDEX_REGIONAL_PRIORITY_DIST' => 'FedEx Regional Priority Distribution', 'FEDEX_REGIONAL_PRIORITY_EXPRESS' => 'FedEx Regional Priority Express', 'FEDEX_REGIONAL_PRIORITY_EXPRESS_DIST' => 'FedEx Regional Priority Express Distribution', 'FEDEX_REGIONAL_PRIORITY_PLUS' => 'FedEx Regional Priority Plus', 'FEDEX_REGIONAL_PRIORITY_PLUS_DIST' => 'FedEx Regional Priority Plus Distribution', 'FEDEX_REGIONAL_ECONOMY_FREIGHT' => 'FedEx Regional Economy Freight', 'FEDEX_REGIONAL_PRIORITY_FREIGHT' => 'FedEx Regional Priority Freight', ); $general_settings = get_option('hitshippo_fedex_main_settings'); if (empty($general_settings)) { return false; } $general_settings = empty($general_settings) ? array() : $general_settings; //excluded Countries if (isset($general_settings['hitshippo_fedex_exclude_countries'])) { if (in_array($pack_aft_hook['destination']['country'], $general_settings['hitshippo_fedex_exclude_countries'])) { return; } } $custom_settings = array(); $custom_settings['default'] = array( 'hitshippo_fedex_site_id' => $general_settings['hitshippo_fedex_site_id'], 'hitshippo_fedex_site_pwd' => $general_settings['hitshippo_fedex_site_pwd'], 'hitshippo_fedex_acc_no' => $general_settings['hitshippo_fedex_acc_no'], 'hitshippo_fedex_access_key' => $general_settings['hitshippo_fedex_access_key'], 'hitshippo_fedex_rest_grant_type' => $general_settings['hitshippo_fedex_rest_grant_type'], 'hitshippo_fedex_rest_acc_no' => $general_settings['hitshippo_fedex_rest_acc_no'], 'hitshippo_fedex_rest_api_key' => $general_settings['hitshippo_fedex_rest_api_key'], 'hitshippo_fedex_rest_secret_key' => $general_settings['hitshippo_fedex_rest_secret_key'], 'hitshippo_fedex_shipper_name' => $general_settings['hitshippo_fedex_shipper_name'], 'hitshippo_fedex_company' => $general_settings['hitshippo_fedex_company'], 'hitshippo_fedex_mob_num' => $general_settings['hitshippo_fedex_mob_num'], 'hitshippo_fedex_email' => $general_settings['hitshippo_fedex_email'], 'hitshippo_fedex_address1' => $general_settings['hitshippo_fedex_address1'], 'hitshippo_fedex_address2' => $general_settings['hitshippo_fedex_address2'], 'hitshippo_fedex_city' => $general_settings['hitshippo_fedex_city'], 'hitshippo_fedex_state' => $general_settings['hitshippo_fedex_state'], 'hitshippo_fedex_zip' => $general_settings['hitshippo_fedex_zip'], 'hitshippo_fedex_country' => $general_settings['hitshippo_fedex_country'], 'hitshippo_fedex_con_rate' => isset($general_settings['hitshippo_fedex_con_rate']) ? $general_settings['hitshippo_fedex_con_rate'] : '', 'hitshippo_fedex_currency' => $general_settings['hitshippo_fedex_currency'], ); $vendor_settings = array(); if (isset($general_settings['hitshippo_fedex_rates']) && $general_settings['hitshippo_fedex_rates'] == 'yes' && isset($pack_aft_hook['destination']['country']) && !empty($pack_aft_hook['destination']['country'])) { if (isset($general_settings['hitshippo_fedex_v_enable']) && $general_settings['hitshippo_fedex_v_enable'] == 'yes' && isset($general_settings['hitshippo_fedex_v_rates']) && $general_settings['hitshippo_fedex_v_rates'] == 'yes') { // Multi Vendor Enabled foreach ($pack_aft_hook['contents'] as $key => $value) { $product_id = $value['product_id']; if ($this->hpos_enabled) { $hpos_prod_data = wc_get_product($product_id); $fedex_account = $hpos_prod_data->get_meta("fedex_address"); } else { $fedex_account = get_post_meta($product_id, 'fedex_address', true); } if (empty($fedex_account) || $fedex_account == 'default') { $fedex_account = 'default'; if (!isset($vendor_settings[$fedex_account])) { $vendor_settings[$fedex_account] = $custom_settings['default']; } $vendor_settings[$fedex_account]['products'][] = $value; } if ($fedex_account != 'default') { $user_account = get_post_meta($fedex_account, 'hitshippo_fedex_vendor_settings', true); $user_account = empty($user_account) ? array() : $user_account; if (!empty($user_account)) { if (!isset($vendor_settings[$fedex_account])) { $vendor_settings[$fedex_account] = $custom_settings['default']; if ($user_account['hitshippo_fedex_site_id'] != '' && $user_account['hitshippo_fedex_site_pwd'] != '' && $user_account['hitshippo_fedex_acc_no'] != '' && $user_account['hitshippo_fedex_access_key'] != '') { $vendor_settings[$fedex_account]['hitshippo_fedex_site_id'] = $user_account['hitshippo_fedex_site_id']; $vendor_settings[$fedex_account]['hitshippo_fedex_site_pwd'] = $user_account['hitshippo_fedex_site_pwd']; $vendor_settings[$fedex_account]['hitshippo_fedex_acc_no'] = $user_account['hitshippo_fedex_acc_no']; $vendor_settings[$fedex_account]['hitshippo_fedex_access_key'] = $user_account['hitshippo_fedex_access_key']; } if (isset($user_account['hitshippo_fedex_rest_grant_type']) && isset($user_account['hitshippo_fedex_rest_acc_no']) && isset($user_account['hitshippo_fedex_rest_api_key']) && isset($user_account['hitshippo_fedex_rest_secret_key'])) { if ($user_account['hitshippo_fedex_rest_grant_type'] != '' && $user_account['hitshippo_fedex_rest_acc_no'] != '' && $user_account['hitshippo_fedex_rest_api_key'] != '' && $user_account['hitshippo_fedex_rest_secret_key'] != '') { $vendor_settings[$fedex_account]['hitshippo_fedex_rest_grant_type'] = $user_account['hitshippo_fedex_rest_grant_type']; $vendor_settings[$fedex_account]['hitshippo_fedex_rest_acc_no'] = $user_account['hitshippo_fedex_rest_acc_no']; $vendor_settings[$fedex_account]['hitshippo_fedex_rest_api_key'] = $user_account['hitshippo_fedex_rest_api_key']; $vendor_settings[$fedex_account]['hitshippo_fedex_rest_secret_key'] = $user_account['hitshippo_fedex_rest_secret_key']; } } if ($user_account['hitshippo_fedex_shipper_name'] != '' && $user_account['hitshippo_fedex_address1'] != '' && $user_account['hitshippo_fedex_city'] != '' && $user_account['hitshippo_fedex_state'] != '' && $user_account['hitshippo_fedex_zip'] != '' && $user_account['hitshippo_fedex_country'] != '') { if ($user_account['hitshippo_fedex_shipper_name'] != '') { $vendor_settings[$fedex_account]['hitshippo_fedex_shipper_name'] = $user_account['hitshippo_fedex_shipper_name']; } if ($user_account['hitshippo_fedex_company'] != '') { $vendor_settings[$fedex_account]['hitshippo_fedex_company'] = $user_account['hitshippo_fedex_company']; } if ($user_account['hitshippo_fedex_mob_num'] != '') { $vendor_settings[$fedex_account]['hitshippo_fedex_mob_num'] = $user_account['hitshippo_fedex_mob_num']; } if ($user_account['hitshippo_fedex_email'] != '') { $vendor_settings[$fedex_account]['hitshippo_fedex_email'] = $user_account['hitshippo_fedex_email']; } if ($user_account['hitshippo_fedex_address1'] != '') { $vendor_settings[$fedex_account]['hitshippo_fedex_address1'] = $user_account['hitshippo_fedex_address1']; } $vendor_settings[$fedex_account]['hitshippo_fedex_address2'] = !empty($user_account['hitshippo_fedex_address2']) ? $user_account['hitshippo_fedex_address2'] : ''; if ($user_account['hitshippo_fedex_city'] != '') { $vendor_settings[$fedex_account]['hitshippo_fedex_city'] = $user_account['hitshippo_fedex_city']; } if ($user_account['hitshippo_fedex_state'] != '') { $vendor_settings[$fedex_account]['hitshippo_fedex_state'] = $user_account['hitshippo_fedex_state']; } if ($user_account['hitshippo_fedex_zip'] != '') { $vendor_settings[$fedex_account]['hitshippo_fedex_zip'] = $user_account['hitshippo_fedex_zip']; } if ($user_account['hitshippo_fedex_country'] != '') { $vendor_settings[$fedex_account]['hitshippo_fedex_country'] = $user_account['hitshippo_fedex_country']; } if (isset($user_account['hitshippo_fedex_con_rate'])) { $vendor_settings[$fedex_account]['hitshippo_fedex_con_rate'] = $user_account['hitshippo_fedex_con_rate']; } if (isset($user_account['hitshippo_fedex_currency'])) { $vendor_settings[$fedex_account]['hitshippo_fedex_currency'] = $user_account['hitshippo_fedex_currency']; } } } $vendor_settings[$fedex_account]['products'][] = $value; } else { $fedex_account = 'default'; if (!isset($vendor_settings[$fedex_account])) { $vendor_settings[$fedex_account] = $custom_settings['default']; } $vendor_settings[$fedex_account]['products'][] = $value; } } } } if (empty($vendor_settings)) { $custom_settings['default']['products'] = $pack_aft_hook['contents']; } else { $custom_settings = $vendor_settings; } foreach ($custom_settings as $key => $cust_set) { $shipping_rates[$key] = array(); $weight_unit = $dim_unit = ''; if (!empty($general_settings['hitshippo_fedex_weight_unit']) && $general_settings['hitshippo_fedex_weight_unit'] == 'KG_CM') { $weight_unit = 'KG'; $dim_unit = 'CM'; } else { $weight_unit = 'LB'; $dim_unit = 'IN'; } $fedex_selected_curr = $cust_set['hitshippo_fedex_currency']; if (isset($general_settings['hitshippo_fedex_auto_con_rate']) && $general_settings['hitshippo_fedex_auto_con_rate'] == "yes") { $frm_curr = get_option('woocommerce_currency'); if (isset($fedex_selected_curr) && !empty($fedex_selected_curr) && isset($frm_curr) && !empty($frm_curr) && ($frm_curr != $fedex_selected_curr)) { $current_date = date('m-d-Y', time()); $ex_rate_data = get_option('hitshippo_fedex_ex_rate' . $key, ''); $ex_rate_data = !empty($ex_rate_data) ? $ex_rate_data : array(); if (empty($ex_rate_data) || (isset($ex_rate_data['date']) && $ex_rate_data['date'] != $current_date) || (isset($ex_rate_data['to_currency']) && ($ex_rate_data['to_currency'] != $fedex_selected_curr)) || (isset($ex_rate_data['from_currency']) && ($ex_rate_data['from_currency'] != $frm_curr))) { if (isset($general_settings['hitshippo_fedex_shippo_int_key']) && !empty($general_settings['hitshippo_fedex_shippo_int_key'])) { $to_curr = $fedex_selected_curr; $ex_rate_Request = json_encode(array( 'integrated_key' => $general_settings['hitshippo_fedex_shippo_int_key'], 'from_curr' => $frm_curr, 'to_curr' => $to_curr )); $response = wp_remote_post( "https://app.myshipi.com/get_exchange_rate.php", array( 'method' => 'POST', 'timeout' => 45, 'redirection' => 5, 'httpversion' => '1.0', 'blocking' => true, 'headers' => array('Content-Type' => 'application/json; charset=utf-8'), 'body' => $ex_rate_Request, 'sslverify' => 0, ) ); $ex_rate_result = isset($response['body']) ? json_decode($response['body'], true) : []; if (!empty($ex_rate_result) && isset($ex_rate_result['ex_rate']) && $ex_rate_result['ex_rate'] != "Not Found") { $ex_rate_result['date'] = $current_date; update_option('hitshippo_fedex_ex_rate' . $key, $ex_rate_result); } else { if (!empty($ex_rate_data)) { $ex_rate_data['date'] = $current_date; update_option('hitshippo_fedex_ex_rate' . $key, $ex_rate_data); } } } } } } $xmlRequest = file_get_contents(dirname(__FILE__) . '/xml/rate.xml'); $xmlRequest = str_replace('{key}', $cust_set['hitshippo_fedex_site_id'], $xmlRequest); $xmlRequest = str_replace('{pwd}', $cust_set['hitshippo_fedex_site_pwd'], $xmlRequest); $xmlRequest = str_replace('{Account_num}', $cust_set['hitshippo_fedex_acc_no'], $xmlRequest); $xmlRequest = str_replace('{meter_number}', $cust_set['hitshippo_fedex_access_key'], $xmlRequest); $xmlRequest = str_replace('{timestramp}', date('c', strtotime('+1 Weekday')), $xmlRequest); $xmlRequest = str_replace('{s_postal}', $cust_set['hitshippo_fedex_zip'], $xmlRequest); $xmlRequest = str_replace('{s_countr}', $cust_set['hitshippo_fedex_country'], $xmlRequest); if (isset($general_settings['hitshippo_fedex_res_f']) && $general_settings['hitshippo_fedex_res_f'] == 'yes') { $xmlRequest = str_replace('{r_res}', 'true', $xmlRequest); } else { $xmlRequest = str_replace('{r_res}', 'false', $xmlRequest); } $hitshippo_fedex_account_rates = ($general_settings['hitshippo_fedex_account_rates'] != 'yes') ? 'LIST' : 'NONE'; $xmlRequest = str_replace('{ac_rate}', $hitshippo_fedex_account_rates, $xmlRequest); $xmlRequest = str_replace('{r_city}', $pack_aft_hook['destination']['city'], $xmlRequest); $xmlRequest = str_replace('{r_state}', $pack_aft_hook['destination']['state'], $xmlRequest); $xmlRequest = str_replace('{r_postal}', $pack_aft_hook['destination']['postcode'], $xmlRequest); $xmlRequest = str_replace('{r_county}', $pack_aft_hook['destination']['country'], $xmlRequest); $xmlRequest = str_replace('{currency}', $fedex_selected_curr, $xmlRequest); $xmlRequest = str_replace('{weightunit}', $weight_unit, $xmlRequest); $total_xml = ''; $my_par = 0; $total_weight_for_rate = 0; $total_packages = 0; $ex_rate = 0; $ex_rate_data = array(); $woo_weg_unit = get_option('woocommerce_weight_unit'); $woo_dim_unit = get_option('woocommerce_dimension_unit'); $woo_curr = get_option('woocommerce_currency'); $config_weg_unit = $general_settings['hitshippo_fedex_weight_unit']; $mod_weg_unit = (!empty($config_weg_unit) && $config_weg_unit == 'LB_IN') ? 'lbs' : 'kg'; $mod_dim_unit = (!empty($config_weg_unit) && $config_weg_unit == 'LB_IN') ? 'in' : 'cm'; if ($woo_curr != $fedex_selected_curr) { if (isset($general_settings['hitshippo_fedex_auto_con_rate']) && $general_settings['hitshippo_fedex_auto_con_rate'] == "yes") { $ex_rate_data = get_option('hitshippo_fedex_ex_rate' . $key, array()); $ex_rate_data = !empty($ex_rate_data) ? $ex_rate_data : array(); if (!empty($ex_rate_data)) { $ex_rate = $ex_rate_data['ex_rate']; } } else { if (isset($cust_set['hitshippo_fedex_con_rate']) && !empty($cust_set['hitshippo_fedex_con_rate'])) { $ex_rate = $cust_set['hitshippo_fedex_con_rate']; } } } $products = []; foreach ($cust_set['products'] as $value) { $prod_data = $value['data']->get_data(); $get_prod = wc_get_product($value['product_id']); if (!empty($prod_data['weight']) && !empty($prod_data['length']) && !empty($prod_data['width']) && !empty($prod_data['height'])) { $products[] = array( 'product_id' => $value['product_id'], 'id' => $prod_data['id'], 'quantity' => $value['quantity'], 'name' => $prod_data['name'], 'price' => $prod_data['price'], 'regular_price' => $prod_data['regular_price'], 'weight' => ($woo_weg_unit != $mod_weg_unit) ? (float)round(wc_get_weight((!empty($prod_data['weight']) ? $prod_data['weight'] : 0.5), $mod_weg_unit, $woo_weg_unit), 2) : (!empty($prod_data['weight']) ? $prod_data['weight'] : 0.5), 'length' => ($woo_dim_unit != $mod_dim_unit) ? round(wc_get_dimension((!empty($prod_data['length']) ? $prod_data['length'] : 1), $mod_dim_unit, $woo_dim_unit), 2) : (!empty($prod_data['length']) ? $prod_data['length'] : 1), 'width' => ($woo_dim_unit != $mod_dim_unit) ? round(wc_get_dimension((!empty($prod_data['width']) ? $prod_data['width'] : 1), $mod_dim_unit, $woo_dim_unit), 2) : (!empty($prod_data['width']) ? $prod_data['width'] : 1), 'height' => ($woo_dim_unit != $mod_dim_unit) ? round(wc_get_dimension((!empty($prod_data['height']) ? $prod_data['height'] : 1), $mod_dim_unit, $woo_dim_unit), 2) : (!empty($prod_data['height']) ? $prod_data['height'] : 1) ); } elseif ($get_prod->is_type('variable')) { $parent_prod_data = $value['data']->get_parent_data(); $products[] = array( 'product_id' => $value['product_id'], 'id' => $prod_data['id'], 'quantity' => $value['quantity'], 'name' => $prod_data['name'], 'price' => $prod_data['price'], 'regular_price' => $prod_data['regular_price'], 'weight' => ($woo_weg_unit != $mod_weg_unit) ? (float)round(wc_get_weight((!empty($prod_data['weight']) ? $prod_data['weight'] : (!empty($parent_prod_data['weight']) ? $parent_prod_data['weight'] : 1)), $mod_weg_unit, $woo_weg_unit), 2) : (!empty($prod_data['weight']) ? $prod_data['weight'] : (!empty($parent_prod_data['weight']) ? $parent_prod_data['weight'] : 1)), 'length' => ($woo_dim_unit != $mod_dim_unit) ? round(wc_get_dimension((!empty($prod_data['length']) ? $prod_data['length'] : (!empty($parent_prod_data['length']) ? $parent_prod_data['length'] : 1)), $mod_dim_unit, $woo_dim_unit), 2) : (!empty($prod_data['length']) ? $prod_data['length'] : (!empty($parent_prod_data['length']) ? $parent_prod_data['length'] : 1)), 'width' => ($woo_dim_unit != $mod_dim_unit) ? round(wc_get_dimension((!empty($prod_data['width']) ? $prod_data['width'] : (!empty($parent_prod_data['width']) ? $parent_prod_data['width'] : 1)), $mod_dim_unit, $woo_dim_unit), 2) : (!empty($prod_data['width']) ? $prod_data['width'] : (!empty($parent_prod_data['width']) ? $parent_prod_data['width'] : 1)), 'height' => ($woo_dim_unit != $mod_dim_unit) ? round(wc_get_dimension((!empty($prod_data['height']) ? $prod_data['height'] : (!empty($parent_prod_data['height']) ? $parent_prod_data['height'] : 1)), $mod_dim_unit, $woo_dim_unit), 2) : (!empty($prod_data['height']) ? $prod_data['height'] : (!empty($parent_prod_data['height']) ? $parent_prod_data['height'] : 1)) ); } else { $products[] = array( 'product_id' => $value['product_id'], 'id' => $prod_data['id'], 'quantity' => $value['quantity'], 'name' => $prod_data['name'], 'price' => $prod_data['price'], 'regular_price' => $prod_data['regular_price'], 'weight' => ($woo_weg_unit != $mod_weg_unit) ? (float)round(wc_get_weight((!empty($prod_data['weight']) ? $prod_data['weight'] : 0.5), $mod_weg_unit, $woo_weg_unit), 2) : (!empty($prod_data['weight']) ? $prod_data['weight'] : 0.5), 'length' => ($woo_dim_unit != $mod_dim_unit) ? round(wc_get_dimension((!empty($prod_data['length']) ? $prod_data['length'] : 1), $mod_dim_unit, $woo_dim_unit), 2) : (!empty($prod_data['length']) ? $prod_data['length'] : 1), 'width' => ($woo_dim_unit != $mod_dim_unit) ? round(wc_get_dimension((!empty($prod_data['width']) ? $prod_data['width'] : 1), $mod_dim_unit, $woo_dim_unit), 2) : (!empty($prod_data['width']) ? $prod_data['width'] : 1), 'height' => ($woo_dim_unit != $mod_dim_unit) ? round(wc_get_dimension((!empty($prod_data['height']) ? $prod_data['height'] : 1), $mod_dim_unit, $woo_dim_unit), 2) : (!empty($prod_data['height']) ? $prod_data['height'] : 1) ); } } // echo '
';print_r($products); die();
$fedex_packs = $this->hit_get_fedex_packages($products, $general_settings, $fedex_selected_curr);
// echo '';print_r($products);die();
if (empty($fedex_packs)) {
return;
}
foreach ($fedex_packs as $parcel) {
$my_par = $my_par + 1;
// $product = $parcel['data']->get_data();
// if ($woo_weg_unit != $mod_weg_unit) {
// $total_weight = (float)round(wc_get_weight($parcel['Weight']['Value'],$mod_weg_unit,$woo_weg_unit),2);
// }else{
$total_weight = !empty($parcel['Weight']['Value'] && $parcel['Weight']['Value'] > 0) ? $parcel['Weight']['Value'] : 0;
// }
$total_xml .= '' . $my_par . ' ' . $my_par . ' 1 ' . $weight_unit . ' ' . $total_weight . ' ';
// if( !empty($product['height']) && !empty($product['length']) && !empty($product['width']) ){
// if ($woo_dim_unit != $mod_dim_unit) {
// $total_xml .=''.round(wc_get_dimension($product['length'],$mod_dim_unit,$woo_dim_unit)).' '.round(wc_get_dimension($product['width'],$mod_dim_unit,$woo_dim_unit)).' '.round(wc_get_dimension($product['height'],$mod_dim_unit,$woo_dim_unit)).' '.$dim_unit.' ';
// }else{
// $total_xml .=''.$product['length'] .' '. $product['width'].' '. $product['height'].' '.$dim_unit.' ';
// }
// }
if (isset($parcel['Dimensions'])) {
$total_xml .= '' . round($parcel['Dimensions']['Length']) . ' ' . round($parcel['Dimensions']['Width']) . ' ' . round($parcel['Dimensions']['Height']) . ' ' . $dim_unit . ' ';
}
$total_xml .= ' ';
//$total_packages = $my_par;
if ($total_weight < 0.001) {
$total_weight = 0.001;
} else {
$total_weight = round((float)$total_weight, 3);
}
$total_weight_for_rate += $total_weight;
}
$xmlRequest = str_replace('{totalweight}', $total_weight_for_rate, $xmlRequest);
$xmlRequest = str_replace('{package_count}', $my_par, $xmlRequest);
$xmlRequest = str_replace('{line_items}', $total_xml, $xmlRequest);
if ($cust_set['hitshippo_fedex_country'] == $pack_aft_hook['destination']['country']) {
if (isset($general_settings['hitshippo_fedex_one_rates']) && $general_settings['hitshippo_fedex_one_rates'] == "yes") {
$one_rate = 'FEDEX_ONE_RATE ';
$xmlRequest = str_replace('{one_rate}', $one_rate, $xmlRequest);
if (isset($general_settings['hitshippo_fedex_send_pack_as_ship']) && $general_settings['hitshippo_fedex_send_pack_as_ship'] == "yes") {
if (isset($general_settings['hitshippo_fedex_ship_pack_type']) && !empty($general_settings['hitshippo_fedex_ship_pack_type'])) {
$xmlRequest = str_replace('YOUR_PACKAGING', $general_settings['hitshippo_fedex_ship_pack_type'], $xmlRequest);
}
} else {
if ($weight_unit == "KG") {
$nor_weight = $total_weight * 2.205; //Kg to Lbs conversion
} else {
$nor_weight = $total_weight;
}
if ($nor_weight > 10 && $nor_weight <= 50) {
$xmlRequest = str_replace('YOUR_PACKAGING', 'FEDEX_MEDIUM_BOX', $xmlRequest);
} elseif ($nor_weight <= 10) {
$xmlRequest = str_replace('YOUR_PACKAGING', 'FEDEX_SMALL_BOX', $xmlRequest);
}
}
} else {
$xmlRequest = str_replace('{one_rate}', '', $xmlRequest);
if (isset($general_settings['hitshippo_fedex_send_pack_as_ship']) && $general_settings['hitshippo_fedex_send_pack_as_ship'] == "yes") {
if (isset($general_settings['hitshippo_fedex_ship_pack_type']) && !empty($general_settings['hitshippo_fedex_ship_pack_type'])) {
$xmlRequest = str_replace('YOUR_PACKAGING', $general_settings['hitshippo_fedex_ship_pack_type'], $xmlRequest);
}
}
}
} else {
$xmlRequest = str_replace('{one_rate}', '', $xmlRequest);
if (isset($general_settings['hitshippo_fedex_send_pack_as_ship']) && $general_settings['hitshippo_fedex_send_pack_as_ship'] == "yes") {
if (isset($general_settings['hitshippo_fedex_ship_pack_type']) && !empty($general_settings['hitshippo_fedex_ship_pack_type'])) {
$xmlRequest = str_replace('YOUR_PACKAGING', $general_settings['hitshippo_fedex_ship_pack_type'], $xmlRequest);
}
}
}
$order_total = 0;
foreach ($pack_aft_hook['contents'] as $item_id => $values) {
$order_total += (float) $values['line_subtotal'];
if (($woo_curr != $fedex_selected_curr) && ($ex_rate > 0)) {
$order_total *= $ex_rate;
}
}
$pay_type = (isset($general_settings['duty_type']) && $general_settings['duty_type'] == "R") ? "RECIPIENT" : "SENDER";
$ind_cus = "";
if (in_array($cust_set['hitshippo_fedex_country'], array("IN", "AE"))) {
$payer_xml = "";
if ($pay_type == "SENDER") {
$payer_xml = "
" . $cust_set['hitshippo_fedex_acc_no'] . "
" . $cust_set['hitshippo_fedex_country'] . "
";
}
$ind_cus = "
". $pay_type ."
". $payer_xml ."
SOLD
" . $my_par . "
" . $cust_set['hitshippo_fedex_company'] . "
" . $cust_set['hitshippo_fedex_country'] . "
" . $weight_unit . "
" . $total_weight_for_rate . "
" . $my_par . "
PACKAGE
" . $fedex_selected_curr . "
" . $order_total . "
" . $fedex_selected_curr . "
" . $order_total . "
";
}
$xmlRequest = str_replace('{ind_customs}', $ind_cus, $xmlRequest);
// Spl service
$spl_ser_xml = $req_spl_ser_xml = "";
if (isset($general_settings['hitshippo_fedex_sat_del']) && $general_settings['hitshippo_fedex_sat_del'] == 'yes') {
$spl_ser_xml .= "SATURDAY_DELIVERY ";
}
if(!empty($spl_ser_xml)){
$req_spl_ser_xml = "";
$req_spl_ser_xml .= $spl_ser_xml;
$req_spl_ser_xml .= " ";
$xmlRequest = str_replace('{sig_req}', $req_spl_ser_xml, $xmlRequest);
} else {
$xmlRequest = str_replace('{sig_req}', $req_spl_ser_xml, $xmlRequest);
}
// Insurance
$ins_xml = "";
if (isset($general_settings['hitshippo_fedex_insurance']) && ($general_settings['hitshippo_fedex_insurance'] == "yes")) {
$ins_xml = "" . $fedex_selected_curr . " " . $order_total . " ";
}
$xmlRequest = str_replace('{insurance}', $ins_xml, $xmlRequest);
$manual_rates = apply_filters('hithipo_fedex_manual_rates', false);
if ($manual_rates) {
$rate_cost = apply_filters("hitstacks_fedex_shipping_cost_conversion", -1, $total_weight_for_rate, $pack_aft_hook['destination']['country'], 'P');
if ($rate_cost >= 0) {
// This is where you'll add your rates
$rate = array(
'id' => 'hitshippo_fedex',
'label' => 'Fedex Shipping',
'cost' => $rate_cost,
'meta_data' => array('hitshippo_fedex_multi_ven' => $multi_ven, 'hitshippo_fedex_shipping_charge' => $rate_cost)
);
// Register the rate
$this->add_rate($rate);
return;
}
}
if (isset($general_settings['hitshippo_fedex_api_type']) && $general_settings['hitshippo_fedex_api_type'] == "REST") {
$auth_token = get_transient("hitshipo_fedex_rest_auth_token_".$key);
if (!class_exists("fedex_rest")) {
include_once("hitshipo_fedex_rest_main.php");
}
$fedex_rest_obj = new fedex_rest();
$fedex_rest_obj->mode = (isset($general_settings['hitshippo_fedex_test']) && $general_settings['hitshippo_fedex_test'] == 'yes') ? 'test' : 'live';
if (empty($auth_token)) {
$auth_token = $fedex_rest_obj->gen_access_token($cust_set['hitshippo_fedex_rest_grant_type'], $cust_set['hitshippo_fedex_rest_api_key'], $cust_set['hitshippo_fedex_rest_secret_key']);
set_transient("hitshipo_fedex_rest_auth_token_".$key, $auth_token, 3000);
}
$xmlRequest = $fedex_rest_obj->make_rate_req_rest($general_settings, $cust_set, $pack_aft_hook['destination'], $fedex_packs, $order_total);
$request_url = ($fedex_rest_obj->mode == "test") ? $fedex_rest_obj->test_rate_url : $fedex_rest_obj->live_rate_url;
$result = $fedex_rest_obj->get_rate_res_rest($xmlRequest, $auth_token);
} else {
$request_url = (isset($general_settings['hitshippo_fedex_test']) && $general_settings['hitshippo_fedex_test'] == 'yes') ? 'https://wsbeta.fedex.com:443/web-services/rate' : 'https://ws.fedex.com:443/web-services/rate';
$result = wp_remote_post(
$request_url,
array(
'method' => 'POST',
'timeout' => 70,
'sslverify' => 0,
'body' => $xmlRequest
)
);
}
$admin_user = false;
if (is_user_logged_in()) {
$user = wp_get_current_user();
if (isset($user->roles) && !empty($user->roles) && in_array("administrator", $user->roles)) {
$admin_user = true;
}
}
if (isset($general_settings['hitshippo_fedex_developer_rate']) && $general_settings['hitshippo_fedex_developer_rate'] == 'yes' && $admin_user) {
echo "Debug log will be shown only for admins and it can be disabled by unchecking 'Enable Debug Mode' option in configuration.";
echo " Request
";
print_r($request_url);
if (is_array($xmlRequest)) {
echo "
";
print_r($auth_token);
echo "
";
print_r(json_encode($xmlRequest));
echo "
";
print_r($xmlRequest);
} else {
print_r(htmlspecialchars($xmlRequest));
}
echo '
Response
';
if (isset($result) && !empty($result)) {
print_r($result);
} else {
print_r("No rate response from Fedex");
die();
}
// echo "
Response - XML
";
// print_r($xml->SOAPENVBody->RateReply);
}
libxml_use_internal_errors(true);
if (!empty($result) && is_array($result) && isset($result['body'])) {
$xml = str_replace(array(':', '-'), '', $result['body']);
$xml = simplexml_load_string($xml);
if (isset($general_settings['hitshippo_fedex_developer_rate']) && $general_settings['hitshippo_fedex_developer_rate'] == 'yes' && $admin_user) {
echo '
Response Body
';
print_r($xml);
die();
}
} elseif (!empty($result) && isset($result->output)) {
$xml = $result->output;
if (isset($general_settings['hitshippo_fedex_developer_rate']) && $general_settings['hitshippo_fedex_developer_rate'] == 'yes' && $admin_user) {
die();
}
} else {
if (isset($general_settings['hitshippo_fedex_developer_rate']) && $general_settings['hitshippo_fedex_developer_rate'] == 'yes' && $admin_user) {
echo '
Response Body
';
print_r("Empty");
die();
}
return false;
}
if (isset($xml) && isset($xml->SOAPENVBody->RateReply)) {
$xml = $xml->SOAPENVBody->RateReply;
} elseif (isset($xml) && isset($xml->rateReplyDetails)) {
// do nothing
} else {
return false;
}
$carriers_available = isset($general_settings['hitshippo_fedex_carrier']) && is_array($general_settings['hitshippo_fedex_carrier']) ? $general_settings['hitshippo_fedex_carrier'] : array();
$rate = array();
$del_dates = array();
if (isset($xml->RateReplyDetails) && !empty($xml->RateReplyDetails)) {
foreach ($xml->RateReplyDetails as $quote) {
$rate_code = ((string) $quote->ServiceType);
// For international shipments fedex returning service code as FEDEX_GROUND -> same as domestic service code. So, need to alter it here to show international and return back to original before passing to add_rate().
if (isset($cust_set['hitshippo_fedex_country']) && isset($pack_aft_hook['destination']['country'])) {
if ( ($rate_code == "FEDEX_GROUND") && ($cust_set['hitshippo_fedex_country'] != $pack_aft_hook['destination']['country']) ) {
$rate_code = "INTERNATIONAL_GROUND";
}
}
$rate_cost = 0;
if (array_key_exists($rate_code, $carriers_available)) {
$shipment_details = '';
foreach ($quote->RatedShipmentDetails as $shipment_deta) {
if ($hitshippo_fedex_account_rates == "LIST") {
if (strstr($shipment_deta->ShipmentRateDetail->RateType, 'PAYOR_LIST')) {
$shipment_details = $shipment_deta;
break;
}
} else {
if (strstr($shipment_deta->ShipmentRateDetail->RateType, 'PAYOR_ACCOUNT')) {
$shipment_details = $shipment_deta;
break;
} elseif (strstr($shipment_deta->ShipmentRateDetail->RateType, 'PAYOR_ACCOUNT_PACKAGE')) {
$shipment_details = $shipment_deta;
break;
}
}
}
if (empty($shipment_details)) {
$shipment_details = $quote->RatedShipmentDetails;
}
if (empty($shipment_details)) {
continue;
}
if (isset($general_settings['hitshippo_fedex_include_tax']) && $general_settings['hitshippo_fedex_include_tax'] == "yes") {
$rate_cost = (float)((string) $shipment_details->ShipmentRateDetail->TotalNetChargeWithDutiesAndTaxes->Amount);
$rate_curr = (string) $shipment_details->ShipmentRateDetail->TotalNetChargeWithDutiesAndTaxes->Currency;
}else{
$rate_cost = (float)((string) $shipment_details->ShipmentRateDetail->TotalNetCharge->Amount);
$rate_curr = (string) $shipment_details->ShipmentRateDetail->TotalNetCharge->Currency;
}
if ($fedex_selected_curr == 'AED' || $fedex_selected_curr == 'DHS') {
$rate_curr = (string)$fedex_selected_curr;
}
if ($rate_curr != $fedex_selected_curr) {
if($woo_curr != $rate_curr){
continue;
}
}
if (($rate_curr == $fedex_selected_curr) && ($woo_curr != $fedex_selected_curr) && ($ex_rate > 0)) {
$rate_cost /= $ex_rate;
}
$rate[$rate_code] = $rate_cost;
$del_dates[$rate_code] = isset($quote->DeliveryTimestamp) ? (string)$quote->DeliveryTimestamp : "";
} else {
continue;
}
}
} elseif (isset($xml->rateReplyDetails) && !empty($xml->rateReplyDetails)) {
foreach ($xml->rateReplyDetails as $quote) {
$rate_code = ((string) $quote->serviceType);
$rate_cost = 0;
if (array_key_exists($rate_code, $carriers_available)) {
$shipment_details = '';
foreach ($quote->ratedShipmentDetails as $shipment_deta) {
if ($hitshippo_fedex_account_rates == "LIST") {
if (strstr($shipment_deta->rateType, 'PAYOR_LIST')) {
$shipment_details = $shipment_deta;
break;
} elseif (strstr($shipment_deta->rateType, 'LIST')) {
$shipment_details = $shipment_deta;
break;
}
} else {
if (strstr($shipment_deta->rateType, 'PAYOR_ACCOUNT')) {
$shipment_details = $shipment_deta;
break;
} elseif (strstr($shipment_deta->rateType, 'ACCOUNT_PACKAGE')) {
$shipment_details = $shipment_deta;
break;
} elseif (strstr($shipment_deta->rateType, 'ACCOUNT')) {
$shipment_details = $shipment_deta;
break;
}
}
}
if (empty($shipment_details)) {
$shipment_details = isset($quote->ratedShipmentDetails[0]) ? $quote->ratedShipmentDetails[0] : $quote->ratedShipmentDetails;
}
if (empty($shipment_details)) {
continue;
}
if (isset($general_settings['hitshippo_fedex_include_tax']) && $general_settings['hitshippo_fedex_include_tax'] == "yes" && isset($shipment_details->totalNetChargeWithDutiesAndTaxes)) {
$rate_cost = (float)((string) $shipment_details->totalNetChargeWithDutiesAndTaxes);
$rate_curr = (string) $shipment_details->shipmentRateDetail->currency;
}else{
$rate_cost = (float)((string) $shipment_details->totalNetCharge);
$rate_curr = (string) $shipment_details->shipmentRateDetail->currency;
}
if ($fedex_selected_curr == 'AED' || $fedex_selected_curr == 'DHS') {
$rate_curr = (string)$fedex_selected_curr;
}
if ($rate_curr != $fedex_selected_curr) {
if($woo_curr != $rate_curr){
continue;
}
}
if (($rate_curr == $fedex_selected_curr) && ($woo_curr != $fedex_selected_curr) && ($ex_rate > 0)) {
$rate_cost /= $ex_rate;
}
$rate[$rate_code] = $rate_cost;
$del_dates[$rate_code] = isset($quote->operationalDetail->deliveryDate) ? (string)$quote->operationalDetail->deliveryDate : "";
} else {
continue;
}
}
} else {
return false;
}
$shipping_rates[$key] = $rate;
}
if (!empty($shipping_rates)) {
$i = 0;
$final_price = array();
foreach ($shipping_rates as $mkey => $rate) {
$cheap_p = 0;
$cheap_s = '';
foreach ($rate as $key => $cvalue) {
if ($i > 0) {
if ($cheap_p == 0 && $cheap_s == '') {
$cheap_p = $cvalue;
$cheap_s = $key;
} else if ($cheap_p > $cvalue) {
$cheap_p = $cvalue;
$cheap_s = $key;
}
} else {
$final_price[] = array('price' => $cvalue, 'code' => $key, 'multi_v' => $mkey . '_' . $key);
}
}
if ($cheap_p != 0 && $cheap_s != '') {
foreach ($final_price as $key => $value) {
$value['price'] = $value['price'] + $cheap_p;
$value['multi_v'] = $value['multi_v'] . '|' . $mkey . '_' . $cheap_s;
$final_price[$key] = $value;
}
}
$i++;
}
foreach ($final_price as $key => $value) {
$rate_cost = $value['price'];
$rate_code = $value['code'];
$multi_ven = $value['multi_v'];
// $rate_cost = apply_filters('hitshippo_fedex_rate_cost',$rate_cost,$rate_code);
// if($rate_cost > 0)
// {
// $rate_name = $_carriers[$rate_code];
// $name = isset($carriers_name_available[$rate_code]) && !empty($carriers_name_available[$rate_code]) ? $carriers_name_available[$rate_code] : $rate_name;
// $rate = array(
// 'id' => 'hitshippo'.$rate_code,
// 'label' => $name,
// 'cost' => $rate_cost,
// 'meta_data' => array('hitshippo_fedex_service' => $rate_code, 'hitshippo_fedex_shipping_charge' => $rate_cost));
// $this->add_rate( $rate );
// }
$rate_cost = round($rate_cost, 2);
// $carriers_available = isset($general_settings['hitshippo_fedex_carrier']) && is_array($general_settings['hitshippo_fedex_carrier']) ? $general_settings['hitshippo_fedex_carrier'] : array();
$carriers_name_available = isset($general_settings['hitshippo_fedex_carrier_name']) && is_array($general_settings['hitshippo_fedex_carrier']) ? $general_settings['hitshippo_fedex_carrier_name'] : array();
if (array_key_exists($rate_code, $carriers_available)) {
$name = isset($carriers_name_available[$rate_code]) && !empty($carriers_name_available[$rate_code]) ? $carriers_name_available[$rate_code] : $_carriers[$rate_code];
$dest_zip_code = isset($pack_aft_hook['destination']['postcode']) ? $pack_aft_hook['destination']['postcode'] : '';
$rate_cost = apply_filters('hitshippo_fedex_rate_cost', $rate_cost, $rate_code, $order_total, $pack_aft_hook['destination']['country'], $dest_zip_code);
$rate_cost += (float) $add_cost_to_live_rates;
if ($rate_cost < 1) {
$name .= ' - Free';
}
if (isset($general_settings['hitshippo_fedex_del_date']) && $general_settings['hitshippo_fedex_del_date'] == "yes") {
if (isset($del_dates[$rate_code]) && !empty($del_dates[$rate_code])) {
$rc_date = str_replace(["-", ":", "T"], "", $del_dates[$rate_code] );
$formated_date = DateTime::createFromFormat('YmdHis', (string)$rc_date);
$etd_date = $formated_date->format('d/m/Y');
$etd = apply_filters('hitshippo_fedex_delivery_date', " (Etd.Delivery " . $etd_date . ")", $etd_date, "");
$name .= $etd;
}
}
if (!isset($general_settings['hitshippo_fedex_v_rates']) || $general_settings['hitshippo_fedex_v_rates'] != 'yes') {
$multi_ven = '';
}
if ($rate_code == "INTERNATIONAL_GROUND") {
$rate_code = "FEDEX_GROUND";
}
// This is where you'll add your rates
$rate = array(
'id' => 'hitshippo' . $rate_code,
'label' => $name,
'cost' => apply_filters("hitstacks_fedex_shipping_cost_conversion", $rate_cost, $total_weight_for_rate, $pack_aft_hook['destination']['country'], $rate_code),
'meta_data' => array('hitshippo_fedex_multi_ven' => $multi_ven, 'hitshippo_fedex_service' => $rate_code, 'hitshippo_fedex_shipping_charge' => $rate_cost)
);
$is_default_service = apply_filters("hitshipo_fedex_default_rate_service", "no", $rate_code, $pack_aft_hook['destination']['country']);
if ($is_default_service == "yes") {
WC()->session->set('chosen_shipping_methods', array($rate['id']));
}
// Register the rate
$this->add_rate($rate);
}
}
}
}
}
private function hitshippo_get_zipcode_or_city($country, $city, $postcode)
{
$no_postcode_country = array(
'AE', 'AF', 'AG', 'AI', 'AL', 'AN', 'AO', 'AW', 'BB', 'BF', 'BH', 'BI', 'BJ', 'BM', 'BO', 'BS', 'BT', 'BW', 'BZ', 'CD', 'CF', 'CG', 'CI', 'CK',
'CL', 'CM', 'CO', 'CR', 'CV', 'DJ', 'DM', 'DO', 'EC', 'EG', 'ER', 'ET', 'FJ', 'FK', 'GA', 'GD', 'GH', 'GI', 'GM', 'GN', 'GQ', 'GT', 'GW', 'GY', 'HK', 'HN', 'HT', 'IE', 'IQ', 'IR',
'JM', 'JO', 'KE', 'KH', 'KI', 'KM', 'KN', 'KP', 'KW', 'KY', 'LA', 'LB', 'LC', 'LK', 'LR', 'LS', 'LY', 'ML', 'MM', 'MO', 'MR', 'MS', 'MT', 'MU', 'MW', 'MZ', 'NA', 'NE', 'NG', 'NI',
'NP', 'NR', 'NU', 'OM', 'PA', 'PE', 'PF', 'PY', 'QA', 'RW', 'SA', 'SB', 'SC', 'SD', 'SL', 'SN', 'SO', 'SR', 'SS', 'ST', 'SV', 'SY', 'TC', 'TD', 'TG', 'TL', 'TO', 'TT', 'TV', 'TZ',
'UG', 'UY', 'VC', 'VE', 'VG', 'VN', 'VU', 'WS', 'XA', 'XB', 'XC', 'XE', 'XL', 'XM', 'XN', 'XS', 'YE', 'ZM', 'ZW'
);
$postcode_city = !in_array($country, $no_postcode_country) ? $postcode_city = "{$postcode} " : '';
if (!empty($city)) {
$postcode_city .= "{$city} ";
}
return $postcode_city;
}
/**
* Initialise Gateway Settings Form Fields
*/
public function init_form_fields()
{
$this->form_fields = array(
'title' => array(
'title' => __('Name', 'shipi'),
'type' => 'text',
'description' => __('Your customers will see the name of this shipping method during checkout.', 'shipi'),
'default' => $this->method_title,
'placeholder' => __('e.g. Fedex Shipping', 'shipi'),
'desc_tip' => true,
)
);
}
public function generate_hitshippo_fedex_html()
{
$general_settings = get_option('hitshippo_fedex_main_settings');
$general_settings = empty($general_settings) ? array() : $general_settings;
if (!empty($general_settings)) {
wp_redirect(admin_url('options-general.php?page=hit-fedex-configuration'));
}
if (isset($_POST['configure_the_plugin'])) {
// global $woocommerce;
// $countries_obj = new WC_Countries();
// $countries = $countries_obj->__get('countries');
// $default_country = $countries_obj->get_base_country();
// if (!isset($general_settings['hitshippo_fedex_country'])) {
// $general_settings['hitshippo_fedex_country'] = $default_country;
// update_option('hitshippo_fedex_main_settings', $general_settings);
// }
wp_redirect(admin_url('options-general.php?page=hit-fedex-configuration'));
}
?>
Shipi + FedEx
button.button-primary.woocommerce-save-button{display:none;}';
}
public function hit_get_fedex_packages($package, $general_settings, $orderCurrency, $chk = false)
{
switch ($general_settings['hitshippo_fedex_packing_type']) {
case 'box':
return $this->box_shipping($package, $general_settings, $orderCurrency, $chk);
break;
case 'weight_based':
return $this->weight_based_shipping($package, $general_settings, $orderCurrency, $chk);
break;
case 'per_item':
default:
return $this->per_item_shipping($package, $general_settings, $orderCurrency, $chk);
break;
}
}
private function weight_based_shipping($package, $general_settings, $orderCurrency, $chk = false)
{
// echo '';
// print_r($package);
// die();
if (!class_exists('WeightPack')) {
include_once 'classes/weight_pack/class-hit-weight-packing.php';
}
$max_weight = isset($general_settings['hitshippo_fedex_max_weight']) && $general_settings['hitshippo_fedex_max_weight'] != '' ? $general_settings['hitshippo_fedex_max_weight'] : 10;
$weight_pack = new WeightPack('pack_ascending');
$weight_pack->set_max_weight($max_weight);
$package_total_weight = 0;
$insured_value = 0;
$ctr = 0;
foreach ($package as $item_id => $values) {
$ctr++;
// $product = $values['data'];
// $product_data = $product->get_data();
$product_data = $values;
// echo '';print_r($values);die();
if (!$product_data['weight']) {
$product_data['weight'] = 0.001;
}
$chk_qty = $chk ? $values['product_quantity'] : $values['quantity'];
$weight_pack->add_item($product_data['weight'], $values, $chk_qty);
}
$pack = $weight_pack->pack_items();
$errors = $pack->get_errors();
if (!empty($errors)) {
//do nothing
return;
} else {
$boxes = $pack->get_packed_boxes();
$unpacked_items = $pack->get_unpacked_items();
$insured_value = 0;
$packages = array_merge($boxes, $unpacked_items); // merge items if unpacked are allowed
$package_count = sizeof($packages);
// get all items to pass if item info in box is not distinguished
$packable_items = $weight_pack->get_packable_items();
$all_items = array();
if (is_array($packable_items)) {
foreach ($packable_items as $packable_item) {
$all_items[] = $packable_item['data'];
}
}
//pre($packable_items);
$order_total = '';
$to_ship = array();
$group_id = 1;
foreach ($packages as $package) { //pre($package);
$packed_products = array();
if (($package_count == 1) && isset($order_total)) {
$insured_value = (isset($product_data['product_price']) ? $product_data['product_price'] : $product_data['price']) * (isset($values['product_quantity']) ? $values['product_quantity'] : $values['quantity']);
} else {
$insured_value = 0;
if (!empty($package['items'])) {
foreach ($package['items'] as $item) {
$insured_value = $insured_value; //+ $item->price;
}
} else {
if (isset($order_total) && $package_count) {
$insured_value = $order_total / $package_count;
}
}
}
$packed_products = isset($package['items']) ? $package['items'] : $all_items;
// Creating package request
$package_total_weight = $package['weight'];
$insurance_array = array(
'Amount' => $insured_value,
'Currency' => $orderCurrency
);
$group = array(
'GroupNumber' => $group_id,
'GroupPackageCount' => 1,
'Weight' => array(
'Value' => round($package_total_weight, 3),
'Units' => (isset($general_settings['hitshippo_fedex_weight_unit']) && $general_settings['hitshippo_fedex_weight_unit'] === 'KG_CM') ? 'KG' : 'LBS'
),
'packed_products' => $packed_products,
);
$group['InsuredValue'] = $insurance_array;
$group['packtype'] = 'BOX';
$to_ship[] = $group;
$group_id++;
}
}
return $to_ship;
}
private function box_shipping($package, $general_settings, $orderCurrency, $chk = false)
{
if (!class_exists('HIT_Boxpack')) {
include_once 'classes/hit-box-packing.php';
}
$boxpack = new HIT_Boxpack();
$boxes = isset($general_settings['hitshippo_fedex_boxes']) ? $general_settings['hitshippo_fedex_boxes'] : [];
if (empty($boxes)) {
return false;
}
// $boxes = unserialize($boxes);
// Define boxes
foreach ($boxes as $key => $box) {
if (!$box['enabled']) {
continue;
}
$box['pack_type'] = !empty($box['pack_type']) ? $box['pack_type'] : 'BOX';
$newbox = $boxpack->add_box($box['length'], $box['width'], $box['height'], $box['box_weight'], $box['pack_type']);
if (isset($box['id'])) {
$newbox->set_id(current(explode(':', $box['id'])));
}
if ($box['max_weight']) {
$newbox->set_max_weight($box['max_weight']);
}
if ($box['pack_type']) {
$newbox->set_packtype($box['pack_type']);
}
}
// Add items
foreach ($package as $item_id => $product_data) {
// $product = $values['data'];
// $product_data = $product->get_data();
// $get_prod = wc_get_product($values['product_id']);
// $parent_prod_data = [];
// if ($get_prod->is_type('variable')) {
// $parent_prod_data = $product->get_parent_data();
// }
if (isset($product_data['weight']) && !empty($product_data['weight'])) {
$item_weight = round($product_data['weight'] > 0.001 ? $product_data['weight'] : 0.001, 3);
}
if (isset($product_data['width']) && isset($product_data['height']) && isset($product_data['length']) && !empty($product_data['width']) && !empty($product_data['height']) && !empty($product_data['length'])) {
$item_dimension = array(
'Length' => max(1, round($product_data['length'], 3)),
'Width' => max(1, round($product_data['width'], 3)),
'Height' => max(1, round($product_data['height'], 3))
);
}
if (isset($item_weight) && isset($item_dimension)) {
// $dimensions = array($values['depth'], $values['height'], $values['width']);
$chk_qty = $chk ? $product_data['product_quantity'] : $product_data['quantity'];
for ($i = 0; $i < $chk_qty; $i++) {
$boxpack->add_item($item_dimension['Width'], $item_dimension['Height'], $item_dimension['Length'], $item_weight, round($product_data['price']), array(
'data' => $product_data
));
}
} else {
// $this->debug(sprintf(__('Product #%s is missing dimensions. Aborting.', 'wf-shipping-dhl'), $item_id), 'error');
return;
}
}
// Pack it
$boxpack->pack();
$packages = $boxpack->get_packages();
$to_ship = array();
$group_id = 1;
foreach ($packages as $package) {
if ($package->unpacked === true) {
//$this->debug('Unpacked Item');
} else {
//$this->debug('Packed ' . $package->id);
}
$dimensions = array($package->length, $package->width, $package->height);
sort($dimensions);
$insurance_array = array(
'Amount' => round($package->value),
'Currency' => $orderCurrency
);
$group = array(
'GroupNumber' => $group_id,
'GroupPackageCount' => 1,
'Weight' => array(
'Value' => round($package->weight, 3),
'Units' => (isset($general_settings['hitshippo_fedex_weight_unit']) && $general_settings['hitshippo_fedex_weight_unit'] === 'KG_CM') ? 'KG' : 'LBS'
),
'Dimensions' => array(
'Length' => max(1, round((float)$dimensions[2], 3)),
'Width' => max(1, round((float)$dimensions[1], 3)),
'Height' => max(1, round((float)$dimensions[0], 3)),
'Units' => (isset($general_settings['hitshippo_fedex_weight_unit']) && $general_settings['hitshippo_fedex_weight_unit'] === 'KG_CM') ? 'KG' : 'LBS'
),
'InsuredValue' => $insurance_array,
'packed_products' => array(),
'package_id' => $package->id,
'packtype' => 'BOX'
);
// echo '';print_r($packages);die();
if (!empty($package->packed) && is_array($package->packed)) {
foreach ($package->packed as $packed) {
$group['packed_products'][] = $packed->meta['data'];
}
}
if (!isset($package->packed)) {
foreach ($package->unpacked as $unpacked) {
$group['packed_products'][] = $unpacked->meta['data'];
}
}
$to_ship[] = $group;
$group_id++;
}
return $to_ship;
}
private function per_item_shipping($package, $general_settings, $orderCurrency, $chk = false)
{
$to_ship = array();
$group_id = 1;
// Get weight of order
foreach ($package as $item_id => $values) {
// $product = $values['data'];
// $product_data = $product->get_data();
$product_data = $values;
$group = array();
$insurance_array = array(
'Amount' => round($product_data['price']),
'Currency' => $orderCurrency
);
if ($product_data['weight'] < 0.001) {
$dhl_per_item_weight = 0.001;
} else {
$dhl_per_item_weight = round($product_data['weight'], 3);
}
$group = array(
'GroupNumber' => $group_id,
'GroupPackageCount' => 1,
'Weight' => array(
'Value' => $dhl_per_item_weight,
'Units' => (isset($general_settings['hitshippo_fedex_weight_unit']) && $general_settings['hitshippo_fedex_weight_unit'] == 'KG_CM') ? 'KG' : 'LBS'
),
'packed_products' => $product_data
);
if ($product_data['width'] && $product_data['height'] && $product_data['length']) {
$group['Dimensions'] = array(
'Length' => max(1, round($product_data['length'], 3)),
'Width' => max(1, round($product_data['width'], 3)),
'Height' => max(1, round($product_data['height'], 3)),
'Units' => (isset($general_settings['hitshippo_fedex_weight_unit']) && $general_settings['hitshippo_fedex_weight_unit'] == 'KG_CM') ? 'CM' : 'IN'
);
}
$group['packtype'] = 'BOX';
$group['InsuredValue'] = $insurance_array;
$chk_qty = $chk ? $values['product_quantity'] : $values['quantity'];
for ($i = 0; $i < $chk_qty; $i++)
$to_ship[] = $group;
$group_id++;
}
return $to_ship;
}
}
}
function shipi_fedex_method( $methods )
{
$methods['hitshippo_fedex'] = 'hitshippo_fedex';
return $methods;
}
add_filter( 'woocommerce_shipping_methods', 'shipi_fedex_method' );