WC_Jamef

WC_Jamef class.

Defined (1)

The class is defined in the following location(s).

/includes/class-wc-jamef.php  
  1. class WC_Jamef extends WC_Shipping_Method { 
  2.  
  3. /** 
  4. * Initialize the Jamef shipping method. 
  5. * @return void 
  6. */ 
  7. public function __construct() { 
  8. $this->id = 'jamef'; 
  9. $this->method_title = __( 'Jamef', 'wcjamef' ); 
  10. $this->init(); 
  11.  
  12. /** 
  13. * Initializes the method. 
  14. * @return void 
  15. */ 
  16. public function init() { 
  17. // Jamef Web Service. 
  18. $this->webservice = 'http://www.jamef.com.br/jamef/cotacaoFrete.do?eCommerce=true&'; 
  19.  
  20. // Load the form fields. 
  21. $this->init_form_fields(); 
  22.  
  23. // Load the settings. 
  24. $this->init_settings(); 
  25.  
  26. // Define user set variables. 
  27. $this->enabled = $this->settings['enabled']; 
  28. $this->title = $this->settings['title']; 
  29. //$this->declare_value = $this->settings['declare_value']; 
  30. //$this->display_date = $this->settings['display_date']; 
  31. //$this->additional_time = $this->settings['additional_time']; 
  32. $this->availability = $this->settings['availability']; 
  33. $this->fee = $this->settings['fee']; 
  34. $this->zip_origin = $this->settings['zip_origin']; 
  35. $this->countries = $this->settings['countries']; 
  36. $this->corporate_service = $this->settings['corporate_service']; 
  37. $this->minimum_height = $this->settings['minimum_height']; 
  38. $this->minimum_width = $this->settings['minimum_width']; 
  39. $this->minimum_length = $this->settings['minimum_length']; 
  40. $this->debug = $this->settings['debug']; 
  41.  
  42. // Active logs. 
  43. if ( 'yes' == $this->debug ) { 
  44. if ( class_exists( 'WC_Logger' ) ) { 
  45. $this->log = new WC_Logger(); 
  46. } else { 
  47. $this->log = $this->woocommerce_method()->logger(); 
  48.  
  49. // Actions. 
  50. add_action( 'woocommerce_update_options_shipping_' . $this->id, array( &$this, 'process_admin_options' ) ); 
  51.  
  52. /** 
  53. * Backwards compatibility with version prior to 2.1. 
  54. * @return object Returns the main instance of WooCommerce class. 
  55. */ 
  56. protected function woocommerce_method() { 
  57. if ( function_exists( 'WC' ) ) { 
  58. return WC(); 
  59. } else { 
  60. global $woocommerce; 
  61. return $woocommerce; 
  62.  
  63. /** 
  64. * Admin options fields. 
  65. * @return void 
  66. */ 
  67. public function init_form_fields() { 
  68. $this->form_fields = array( 
  69. 'enabled' => array( 
  70. 'title' => __( 'Enable/Disable', 'wcjamef' ),  
  71. 'type' => 'checkbox',  
  72. 'label' => __( 'Enable this shipping method', 'wcjamef' ),  
  73. 'default' => 'no' 
  74. ),  
  75. 'title' => array( 
  76. 'title' => __( 'Title', 'wcjamef' ),  
  77. 'type' => 'text',  
  78. 'description' => __( 'This controls the title which the user sees during checkout.', 'wcjamef' ),  
  79. 'desc_tip' => true,  
  80. 'default' => __( 'Jamef', 'wcjamef' ) 
  81. ),  
  82. 'availability' => array( 
  83. 'title' => __( 'Availability', 'wcjamef' ),  
  84. 'type' => 'select',  
  85. 'default' => 'all',  
  86. 'class' => 'availability',  
  87. 'options' => array( 
  88. 'all' => __( 'All allowed countries', 'wcjamef' ),  
  89. 'specific' => __( 'Specific Countries', 'wcjamef' ) 
  90. ),  
  91. 'countries' => array( 
  92. 'title' => __( 'Specific Countries', 'wcjamef' ),  
  93. 'type' => 'multiselect',  
  94. 'class' => 'chosen_select',  
  95. 'css' => 'width: 450px;',  
  96. 'options' => $this->woocommerce_method()->countries->countries 
  97. ),  
  98. 'cnpj' => array( 
  99. 'title' => __( 'CNPJ', 'wcjamef' ),  
  100. 'type' => 'text',  
  101. 'default' => '05549856000134' 
  102. ),  
  103. 'unit_origin' => array( 
  104. 'title' => __( 'Origin unit', 'wcjamef' ),  
  105. 'type' => 'select',  
  106. 'description' => __( 'Jamef unit from where the requests are sent.', 'wcjamef' ),  
  107. 'desc_tip' => true,  
  108. 'options' => array( 
  109. 174 => 'Aracaju (AJU)',  
  110. 122 => 'Barueri (BAR)',  
  111. 111 => 'Bauru (BAU)',  
  112. 1 => 'Belo Horizonte (BHZ)',  
  113. 13 => 'Blumenau (BNU)',  
  114. 163 => 'Braslia (BSB)',  
  115. 160 => 'Cricima (CSM)',  
  116. 12 => 'Campinas (CPQ)',  
  117. 5 => 'Caxias do Sul (CXJ)',  
  118. 10 => 'Curitiba (CWB)',  
  119. 204 => 'Divinpolis (DIV)',  
  120. 192 => 'Feira de Santana (FES)',  
  121. 11 => 'Florianpolis (FLN)',  
  122. 177 => 'Fortaleza (FOR)',  
  123. 148 => 'Goinia (GYN)',  
  124. 4 => 'Jaragu do Sul (JAS)',  
  125. 208 => 'Juz de Fora (JDF)',  
  126. 9 => 'Joinville (JOI)',  
  127. 8 => 'Londrina (LDB)',  
  128. 149 => 'Manaus (MAO)',  
  129. 183 => 'Macei (MCZ)',  
  130. 112 => 'Maring (MGF)',  
  131. 191 => 'Natal (NAT)',  
  132. 14 => 'Porto Alegre (POA)',  
  133. 159 => 'Pouso Alegre (PSA)',  
  134. 121 => 'Ribeiro Preto (RAO)',  
  135. 171 => 'Recife (REC)',  
  136. 3 => 'Rio de Janeiro (RIO)',  
  137. 2 => 'So Paulo (SAO)',  
  138. 125 => 'So Jos dos Campos (SJK)',  
  139. 124 => 'So Jos do Rio Preto (SJP)',  
  140. 168 => 'Salvador (SSA)',  
  141. 109 => 'Uberlndia (UDI)',  
  142. 209 => 'Vitria da Conquista (VDC)',  
  143. 7 => 'Vitria (VIX)' 
  144. ),  
  145. 'state_origin' => array( 
  146. 'title' => __( 'Origin state', 'wcjamef' ),  
  147. 'type' => 'select',  
  148. 'css' => 'width: 250px;',  
  149. 'default' => 'SP',  
  150. 'options' => $this->woocommerce_method()->countries->states['BR'] 
  151. ),  
  152. /**'declare_value' => array( 
  153. 'title' => __( 'Declare value', 'wcjamef' ),  
  154. 'type' => 'select',  
  155. 'default' => 'none',  
  156. 'options' => array( 
  157. 'declare' => __( 'Declare', 'wcjamef' ),  
  158. 'none' => __( 'None', 'wcjamef' ) 
  159. ),  
  160. ),  
  161. 'display_date' => array( 
  162. 'title' => __( 'Estimated delivery', 'wcjamef' ),  
  163. 'type' => 'checkbox',  
  164. 'label' => __( 'Enable', 'wcjamef' ),  
  165. 'description' => __( 'Display date of estimated delivery.', 'wcjamef' ),  
  166. 'desc_tip' => true,  
  167. 'default' => 'no' 
  168. ),  
  169. 'additional_time' => array( 
  170. 'title' => __( 'Additional days', 'wcjamef' ),  
  171. 'type' => 'text',  
  172. 'description' => __( 'Additional days to the estimated delivery.', 'wcjamef' ),  
  173. 'desc_tip' => true,  
  174. 'default' => '0',  
  175. 'placeholder' => '0' 
  176. ), */ 
  177. 'fee' => array( 
  178. 'title' => __( 'Handling Fee', 'wcjamef' ),  
  179. 'type' => 'text',  
  180. 'description' => __( 'Enter an amount, e.g. 2.50, or a percentage, e.g. 5%. Leave blank to disable.', 'wcjamef' ),  
  181. 'desc_tip' => true,  
  182. 'placeholder' => '0.00' 
  183. ),  
  184. 'package_standard' => array( 
  185. 'title' => __( 'Package Standard', 'wcjamef' ),  
  186. 'type' => 'title',  
  187. 'description' => __( 'Sets a minimum measure for the package.', 'wcjamef' ),  
  188. 'desc_tip' => true,  
  189. ),  
  190. 'minimum_height' => array( 
  191. 'title' => __( 'Minimum Height', 'wcjamef' ),  
  192. 'type' => 'text',  
  193. 'description' => __( 'Minimum height of the package. Jamef needs at least 2 cm.', 'wcjamef' ),  
  194. 'desc_tip' => true,  
  195. 'default' => '2' 
  196. ),  
  197. 'minimum_width' => array( 
  198. 'title' => __( 'Minimum Width', 'wcjamef' ),  
  199. 'type' => 'text',  
  200. 'description' => __( 'Minimum width of the package. Jamef needs at least 11 cm.', 'wcjamef' ),  
  201. 'desc_tip' => true,  
  202. 'default' => '11' 
  203. ),  
  204. 'minimum_length' => array( 
  205. 'title' => __( 'Minimum Length', 'wcjamef' ),  
  206. 'type' => 'text',  
  207. 'description' => __( 'Minimum length of the package. Jamef needs at least 16 cm.', 'wcjamef' ),  
  208. 'desc_tip' => true,  
  209. 'default' => '16' 
  210. ),  
  211. 'testing' => array( 
  212. 'title' => __( 'Testing', 'wcjamef' ),  
  213. 'type' => 'title' 
  214. ),  
  215. 'debug' => array( 
  216. 'title' => __( 'Debug Log', 'wcjamef' ),  
  217. 'type' => 'checkbox',  
  218. 'label' => __( 'Enable logging', 'wcjamef' ),  
  219. 'default' => 'no',  
  220. 'description' => sprintf( __( 'Log Jamef events, such as WebServices requests, inside %s.', 'wcjamef' ), '<code>woocommerce/logs/jamef-' . sanitize_file_name( wp_hash( 'jamef' ) ) . '.txt</code>' ) 
  221. ); 
  222.  
  223. /** 
  224. * Jamef options page. 
  225. * @return void 
  226. */ 
  227. public function admin_options() { 
  228. // Call the admin scripts. 
  229. //wp_enqueue_script( 'wc-jamef', WOO_JAMEF_URL . 'js/admin.js', array( 'jquery' ), '', true ); 
  230.  
  231. echo '<h3>' . $this->method_title . '</h3>'; 
  232. echo '<p>' . __( 'Jamef is a brazilian delivery method.', 'wcjamef' ) . '</p>'; 
  233. echo '<table class="form-table">'; 
  234. $this->generate_settings_html(); 
  235. echo '</table>'; 
  236.  
  237. /** 
  238. * Checks if the method is available. 
  239. * @param array $package Order package. 
  240. * @return bool 
  241. */ 
  242. public function is_available( $package ) { 
  243. $is_available = true; 
  244.  
  245. if ( 'no' == $this->enabled ) { 
  246. $is_available = false; 
  247. } else { 
  248. $ship_to_countries = ''; 
  249.  
  250. if ( 'specific' == $this->availability ) { 
  251. $ship_to_countries = $this->countries; 
  252. } elseif ( 'specific' == get_option( 'woocommerce_allowed_countries' ) ) { 
  253. $ship_to_countries = get_option( 'woocommerce_specific_allowed_countries' ); 
  254.  
  255. if ( is_array( $ship_to_countries ) && ! in_array( $package['destination']['country'], $ship_to_countries ) ) { 
  256. $is_available = false; 
  257.  
  258. return apply_filters( 'woocommerce_shipping_' . $this->id . '_is_available', $is_available, $package ); 
  259.  
  260. /** 
  261. * Replace comma by dot. 
  262. * @param mixed $value Value to fix. 
  263. * @return mixed 
  264. */ 
  265. private function fix_format( $value ) { 
  266. $value = str_replace( ', ', '.', $value ); 
  267.  
  268. return $value; 
  269.  
  270. /** 
  271. * Fix number format for SimpleXML. 
  272. * @param float $value Value with dot. 
  273. * @return string Value with comma. 
  274. */ 
  275. private function fix_simplexml_format( $value ) { 
  276. $value = str_replace( '.', ', ', $value ); 
  277.  
  278. return $value; 
  279.  
  280. /** 
  281. * Fix Zip Code format. 
  282. * @param mixed $zip Zip Code. 
  283. * @return int 
  284. */ 
  285. protected function fix_zip_code( $zip ) { 
  286. $fixed = preg_replace( '([^0-9])', '', $zip ); 
  287.  
  288. return $fixed; 
  289.  
  290. /** 
  291. * Get fee. 
  292. * @param mixed $fee 
  293. * @param mixed $total 
  294. * @return float 
  295. */ 
  296. public function get_fee( $fee, $total ) { 
  297. if ( strstr( $fee, '%' ) ) { 
  298. $fee = ( $total / 100 ) * str_replace( '%', '', $fee ); 
  299.  
  300. return $fee; 
  301.  
  302. /** 
  303. * Extracts the weight and dimensions from the order. 
  304. * @param array $package 
  305. * @return array 
  306. */ 
  307. protected function measures_extract( $package ) { 
  308. $count = 0; 
  309. $height = array(); 
  310. $width = array(); 
  311. $length = array(); 
  312. $weight = array(); 
  313.  
  314. // Shipping per item. 
  315. foreach ( $package['contents'] as $item_id => $values ) { 
  316. $product = $values['data']; 
  317. $qty = $values['quantity']; 
  318.  
  319. if ( $qty > 0 && $product->needs_shipping() ) { 
  320.  
  321. if ( version_compare( WOOCOMMERCE_VERSION, '2.1', '>=' ) ) { 
  322. $_height = wc_get_dimension( $this->fix_format( $product->height ), 'cm' ); 
  323. $_width = wc_get_dimension( $this->fix_format( $product->width ), 'cm' ); 
  324. $_length = wc_get_dimension( $this->fix_format( $product->length ), 'cm' ); 
  325. $_weight = wc_get_weight( $this->fix_format( $product->weight ), 'kg' ); 
  326. } else { 
  327. $_height = woocommerce_get_dimension( $this->fix_format( $product->height ), 'cm' ); 
  328. $_width = woocommerce_get_dimension( $this->fix_format( $product->width ), 'cm' ); 
  329. $_length = woocommerce_get_dimension( $this->fix_format( $product->length ), 'cm' ); 
  330. $_weight = woocommerce_get_weight( $this->fix_format( $product->weight ), 'kg' ); 
  331.  
  332. $height[ $count ] = $_height; 
  333. $width[ $count ] = $_width; 
  334. $length[ $count ] = $_length; 
  335. $weight[ $count ] = $_weight; 
  336.  
  337. if ( $qty > 1 ) { 
  338. $n = $count; 
  339. for ( $i = 0; $i < $qty; $i++ ) { 
  340. $height[ $n ] = $_height; 
  341. $width[ $n ] = $_width; 
  342. $length[ $n ] = $_length; 
  343. $weight[ $n ] = $_weight; 
  344. $n++; 
  345. $count = $n; 
  346.  
  347. $count++; 
  348.  
  349. return array( 
  350. 'height' => array_values( $height ),  
  351. 'length' => array_values( $length ),  
  352. 'width' => array_values( $width ),  
  353. 'weight' => array_sum( $weight ),  
  354. ); 
  355.  
  356. /** 
  357. * estimating_delivery function. 
  358. * @param string $label 
  359. * @param string $date 
  360. * @return string 
  361. */ 
  362. protected function estimating_delivery( $label, $date ) { 
  363. $msg = $label; 
  364.  
  365. if ( $this->additional_time > 0 ) { 
  366. $date += (int) $this->additional_time; 
  367.  
  368. if ( $date > 0 ) { 
  369. $msg .= ' (' . sprintf( _n( 'Delivery in %d working day', 'Delivery in %d working days', $date, 'wcjamef' ), $date ) . ')'; 
  370.  
  371. return $msg; 
  372.  
  373. /** 
  374. * Connection method. 
  375. * @param mixed $unit_origin Jamet unit of the origin. 
  376. * @param mixed $zip_destination Zip Code of the destination. 
  377. * @param float $height Height total. 
  378. * @param float $width Width total. 
  379. * @param float $diameter Diamenter total. 
  380. * @param float $length Length total. 
  381. * @param float $weight Weight total. 
  382. * @param string $receipt_notice Notice. 
  383. * @return array Quotes. 
  384. */ 
  385. protected function jamef_connect( 
  386. $cnpj,  
  387. $unit_origin,  
  388. $state_origin,  
  389. $zip_destination,  
  390. $cubage,  
  391. $weight,  
  392. $declared = 0,  
  393. $receipt_notice = 'N' ) { 
  394.  
  395. $quotes = array(); 
  396.  
  397. // Sets the get query. 
  398. $query = http_build_query( array( 
  399. 'P_CIC_NEGC' => $this->fix_zip_code( $cnpj ),  
  400. 'P_CEP' => $this->fix_zip_code( $zip_destination ),  
  401. 'P_COD_REGN' => $unit_origin,  
  402. 'P_PESO_KG' => $this->fix_simplexml_format( $weight ),  
  403. 'P_VLR_CARG' => $declared,  
  404. 'P_UF' => $state_origin,  
  405. 'P_CUBG' => $cubage 
  406. ), '', '&' ); 
  407.  
  408. if ( 'yes' == $this->debug ) { 
  409. $this->log->add( 'jamef', 'Requesting the Jamef WebServices...' ); 
  410.  
  411. // Gets the WebServices response. 
  412. $response = wp_remote_get( $this->webservice . $query, array( 'sslverify' => false, 'timeout' => 30 ) ); 
  413.  
  414. if ( is_wp_error( $response ) ) { 
  415. if ( 'yes' == $this->debug ) { 
  416. $this->log->add( 'jamef', 'WP_Error: ' . $response->get_error_message() ); 
  417. } elseif ( $response['response']['code'] >= 200 && $response['response']['code'] < 300 ) { 
  418. $result = $response['body']; 
  419.  
  420. if ( 'yes' == $this->debug ) { 
  421. $this->log->add( 'jamef', 'Jamef WebServices response: ' . print_r( substr($result, 13), true ) ); 
  422.  
  423. $quotes = substr($result, 13); 
  424. } else { 
  425. if ( 'yes' == $this->debug ) { 
  426. $this->log->add( 'jamef', 'Error accessing the Jamef WebServices: ' . $response['response']['code'] . ' - ' . $response['response']['message'] ); 
  427.  
  428. return $quotes; 
  429.  
  430. /** 
  431. * Gets the price of shipping. 
  432. * @param array $package Order package. 
  433. * @return array Jamef Quotes. 
  434. */ 
  435. protected function jamef_quote( $package ) { 
  436.  
  437. // Proccess measures. 
  438. $measures = apply_filters( 'wcjamef_default_package', $this->measures_extract( $package ) ); 
  439.  
  440. // Checks if the cart is not just virtual goods. 
  441. if ( ! empty( $measures['height'] ) && ! empty( $measures['width'] ) && ! empty( $measures['length'] ) ) { 
  442.  
  443. // Get the Cubage. 
  444. $all = array(); 
  445. $total = ''; 
  446.  
  447. for ( $i = 0; $i < count( $this->height ); $i++ ) { 
  448. $all[ $i ] = ( $measures['height'][ $i ] * $measures['width'][ $i ] * $measures['length'][ $i ] ) / 1000000; 
  449.  
  450. foreach ( $all as $value ) { 
  451. $totalcubage += $value; 
  452.  
  453. $zip_destination = $package['destination']['postcode']; 
  454.  
  455. // Test min values. 
  456. $min_height = $this->minimum_height; 
  457. $min_width = $this->minimum_width; 
  458. $min_length = $this->minimum_length; 
  459.  
  460. $test_cubage = ( $min_height * $min_width * $min_length ) / 1000000; 
  461. if( $totalcubage < $test_cubage ) 
  462. $totalcubage = $test_cubage; 
  463.  
  464. $cubage = str_replace( '.', ', ', $totalcubage ); 
  465.  
  466. $declared = 0; 
  467. $declared = number_format( $this->woocommerce_method()->cart->cart_contents_total, 2, ', ', '' ); 
  468.  
  469. // Get quotes. 
  470. $quotes = $this->jamef_connect( 
  471. $this->settings['cnpj'],  
  472. $this->settings['unit_origin'],  
  473. $this->settings['state_origin'],  
  474. $zip_destination,  
  475. $cubage,  
  476. $measures['weight'],  
  477. $declared 
  478. ); 
  479.  
  480. return $quotes; 
  481.  
  482. } else { 
  483.  
  484. // Cart only with virtual products. 
  485. if ( 'yes' == $this->debug ) { 
  486. $this->log->add( 'jamef', 'Cart only with virtual products.' ); 
  487.  
  488. return array(); 
  489.  
  490. /** 
  491. * Calculates the shipping rate. 
  492. * @param array $package Order package. 
  493. * @return void 
  494. */ 
  495. public function calculate_shipping( $package = array() ) { 
  496. $rates = array(); 
  497. $quotes = $this->jamef_quote( $package ); 
  498.  
  499. if ( $quotes > 0 ) { 
  500.  
  501. $cust = $this->fix_format( esc_attr( $quotes ) ); 
  502. $fee = $this->get_fee( $this->fix_format( $this->fee ), $cust ); 
  503.  
  504. array_push( 
  505. $rates,  
  506. array( 
  507. 'id' => $this->title,  
  508. 'label' => $this->title,  
  509. 'cost' => $cust + $fee,  
  510. ); 
  511.  
  512. $rate = apply_filters( 'woocommerce_jamef_shipping_methods', $rates, $package ); 
  513.  
  514. // Register the rate. 
  515. foreach ( $rate as $key => $value ) 
  516. $this->add_rate( $value ); 
  517. } else { 
  518. $woocommerce->add_error( 'Desculpe, sua regio ainda no atendida por nossa transportadora!' );