Changeset 1966477
- Timestamp:
- 10/31/2018 01:25:02 PM (7 years ago)
- Location:
- woo-pakettikauppa/trunk
- Files:
-
- 9 edited
-
includes/class-wc-pakettikauppa-admin.php (modified) (2 diffs)
-
includes/class-wc-pakettikauppa-shipment.php (modified) (2 diffs)
-
includes/class-wc-pakettikauppa-shipping-method.php (modified) (2 diffs)
-
includes/class-wc-pakettikauppa.php (modified) (2 diffs)
-
readme.txt (modified) (1 diff)
-
vendor/pakettikauppa/api-library/src/Pakettikauppa/Client.php (modified) (10 diffs)
-
vendor/pakettikauppa/api-library/src/Pakettikauppa/Shipment.php (modified) (1 diff)
-
vendor/pakettikauppa/api-library/src/Pakettikauppa/Shipment/Parcel.php (modified) (1 diff)
-
wc-pakettikauppa.php (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
woo-pakettikauppa/trunk/includes/class-wc-pakettikauppa-admin.php
r1833640 r1966477 2 2 3 3 // Prevent direct access to this script 4 if ( ! defined( 'ABSPATH' )) {5 exit;4 if (!defined('ABSPATH')) { 5 exit; 6 6 } 7 7 … … 18 18 * @author Seravo 19 19 */ 20 class WC_Pakettikauppa_Admin { 21 private $wc_pakettikauppa_shipment = null; 22 private $errors = array(); 23 24 public function __construct() { 25 $this->id = 'wc_pakettikauppa_admin'; 26 } 27 28 public function load() { 29 add_filter( 'plugin_action_links_' . WC_PAKETTIKAUPPA_BASENAME, array( $this, 'add_settings_link' ) ); 30 add_filter( 'plugin_row_meta', array( $this, 'plugin_row_meta' ), 10, 2 ); 31 32 add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) ); 33 add_action( 'add_meta_boxes', array( $this, 'register_meta_boxes' ) ); 34 add_action( 'save_post', array( $this, 'save_metabox' ), 10, 2 ); 35 add_action( 'admin_post_show_pakettikauppa', array( $this, 'show' ), 10 ); 36 add_action( 'woocommerce_email_order_meta', array( $this, 'attach_tracking_to_email' ), 10, 4 ); 37 add_action( 'woocommerce_admin_order_data_after_shipping_address', array( $this, 'show_pickup_point_in_admin_order_meta' ), 10, 1 ); 38 // Delete the tracking label when order is deleted so the uploads directory doesn't get too bloated 39 add_action( 'before_delete_post', array( $this, 'delete_order_shipping_label' ) ); 40 // Connect shipping service and pickup points in admin 41 add_action( 'wp_ajax_admin_update_pickup_point', array( $this, 'update_meta_box_pickup_points' ) ); 42 43 try { 44 $this->wc_pakettikauppa_shipment = new WC_Pakettikauppa_Shipment(); 45 $this->wc_pakettikauppa_shipment->load(); 46 47 } catch ( Exception $e ) { 48 $this->add_error( $e->getMessage() ); 49 $this->add_error_notice( $e->getMessage() ); 50 return; 51 } 52 } 53 54 /** 55 * Check if the selected service has pickup points via wp_ajax 56 */ 57 public function update_meta_box_pickup_points() { 58 if ( isset( $_POST ) && ! empty( $_POST['service_id'] ) ) { 59 $service_id = $_POST['service_id']; 60 echo esc_attr( WC_Pakettikauppa_Shipment::service_has_pickup_points( $service_id ) ); 61 wp_die(); 62 } 63 } 64 65 /** 66 * Add an error with a specified error message. 67 * 68 * @param string $message A message containing details about the error. 69 */ 70 public function add_error( $message ) { 71 if ( ! empty( $message ) ) { 72 array_push( $this->errors, $message ); 73 error_log( $message ); 74 } 75 } 76 77 /** 78 * Return all errors that have been added via add_error(). 79 * 80 * @return array Errors 81 */ 82 public function get_errors() { 83 return $this->errors; 84 } 85 86 /** 87 * Clear all existing errors that have been added via add_error(). 88 */ 89 public function clear_errors() { 90 unset( $this->errors ); 91 $this->errors = array(); 92 } 93 94 /** 95 * Add an admin error notice to wp-admin. 96 */ 97 public function add_error_notice( $message ) { 98 if ( ! empty( $message ) ) { 99 $class = 'notice notice-error'; 100 /* translators: %s: Error message */ 101 $print_error = wp_sprintf( __( 'An error occured: %s', 'wc-pakettikauppa' ), $message ); 102 printf( '<div class="%1$s"><p>%2$s</p></div>', esc_attr( $class ), esc_html( $print_error ) ); 103 } 104 } 105 106 /** 107 * Show row meta on the plugin screen. 108 * 109 * @param mixed $links Plugin Row Meta 110 * @param mixed $file Plugin Base file 111 * @return array 112 */ 113 public static function plugin_row_meta( $links, $file ) { 114 if ( WC_PAKETTIKAUPPA_BASENAME === $file ) { 115 $row_meta = array( 116 'service' => '<a href="' . esc_url( 'https://www.pakettikauppa.fi' ) . 117 '" aria-label="' . esc_attr__( 'Visit Pakettikauppa', 'wc-pakettikauppa' ) . 118 '">' . esc_html__( 'Show site Pakettikauppa', 'wc-pakettikauppa' ) . '</a>', 119 ); 120 return array_merge( $links, $row_meta ); 121 } 122 return (array) $links; 123 } 124 125 /** 126 * Register meta boxes for WooCommerce order metapage. 127 */ 128 public function register_meta_boxes() { 129 foreach ( wc_get_order_types( 'order-meta-boxes' ) as $type ) { 130 $order_type_object = get_post_type_object( $type ); 131 add_meta_box( 132 'wc-pakettikauppa', 133 esc_attr__( 'Pakettikauppa', 'wc-pakettikauppa' ), 134 array( 135 $this, 136 'meta_box', 137 ), 138 $type, 139 'side', 140 'default' 141 ); 142 } 143 } 144 145 /** 146 * Enqueue admin-specific styles and scripts. 147 */ 148 public function admin_enqueue_scripts() { 149 wp_enqueue_style( 'wc_pakettikauppa_admin', plugin_dir_url( __FILE__ ) . '../assets/css/wc-pakettikauppa-admin.css' ); 150 wp_enqueue_script( 'wc_pakettikauppa_admin_js', plugin_dir_url( __FILE__ ) . '../assets/js/wc-pakettikauppa-admin.js', array( 'jquery' ) ); 151 } 152 153 /** 154 * Add settings link to the Pakettikauppa metabox on the plugins page when used with 155 * the WordPress hook plugin_action_links_woocommerce-pakettikauppa. 156 * 157 * @param array $links Already existing links on the plugin metabox 158 * @return array The plugin settings page link appended to the already existing links 159 */ 160 public function add_settings_link( $links ) { 161 $url = admin_url( 'admin.php?page=wc-settings&tab=shipping§ion=wc_pakettikauppa_shipping_method' ); 162 $link = '<a href="' . $url . '">' . esc_attr__( 'Settings' ) . '</a>'; 163 164 return array_merge( array( $link ), $links ); 165 } 166 167 /** 168 * Show the selected pickup point in admin order meta. Use together with the hook 169 * woocommerce_admin_order_data_after_shipping_address. 170 * 171 * @param WC_Order $order The order that is currently being viewed in wp-admin 172 */ 173 public function show_pickup_point_in_admin_order_meta( $order ) { 174 echo '<p class="form-field"><strong>' . esc_attr__('Requested pickup point', 'wc-pakettikauppa') . ':</strong><br>'; 175 if ( $order->get_meta('_pakettikauppa_pickup_point') ) { 176 echo esc_attr( $order->get_meta('_pakettikauppa_pickup_point') ); 177 echo '<br>ID: ' . esc_attr( $order->get_meta('_pakettikauppa_pickup_point_id') ); 178 } else { 179 echo esc_attr__('None'); 180 } 181 echo '</p>'; 182 } 183 184 /** 185 * Meta box for managing shipments. 186 */ 187 public function meta_box( $post ) { 188 $order = wc_get_order( $post->ID ); 189 190 if ( ! WC_Pakettikauppa_Shipment::validate_order_shipping_receiver( $order ) ) { 191 esc_attr_e( 'Please add shipping info to the order to manage Pakettikauppa shipments.', 'wc-pakettikauppa' ); 192 return; 193 } 194 195 // Get active services from active_shipping_options 196 $settings = get_option( 'woocommerce_WC_Pakettikauppa_Shipping_Method_settings', null ); 197 $active_shipping_options = json_decode( $settings['active_shipping_options'], true ); 198 199 // The tracking code will only be available if the shipment label has been generated 200 $tracking_code = get_post_meta( $post->ID, '_wc_pakettikauppa_tracking_code', true); 201 $cod = get_post_meta( $post->ID, '_wc_pakettikauppa_cod', true); 202 $cod_amount = get_post_meta( $post->ID, '_wc_pakettikauppa_cod_amount', true); 203 $cod_reference = get_post_meta( $post->ID, '_wc_pakettikauppa_cod_reference', true); 204 $service_id = get_post_meta( $post->ID, '_wc_pakettikauppa_service_id', true); 205 206 $shipping_methods = $order->get_shipping_methods(); 207 $shipping_method = reset( $shipping_methods ); 208 $ids = explode( ':', $shipping_method['method_id'] ); 209 if ( isset( $ids[1] ) && ! empty( $ids[1] ) ) { 210 $service_id = (int) $ids[1]; 211 } 212 213 $pickup_point = $order->get_meta('_pakettikauppa_pickup_point'); 214 $pickup_point_id = $order->get_meta('_pakettikauppa_pickup_point_id'); 215 $status = get_post_meta( $post->ID, '_wc_pakettikauppa_shipment_status', true); 216 217 // Set defaults 218 if ( empty( $cod ) ) { 219 $cod = ( $order->get_payment_method() === 'cod' ); 220 } 221 if ( empty( $cod_amount) ) { 222 $cod_amount = $order->get_total(); 223 } 224 if ( empty( $cod_reference) ) { 225 $cod_reference = WC_Pakettikauppa_Shipment::calculate_reference( $post->ID ); 226 } 227 if ( empty( $service_id ) ) { 228 $service_id = WC_Pakettikauppa_Shipment::get_default_service($post, $order); 229 } 230 231 $document_url = admin_url( 'admin-post.php?post=' . $post->ID . '&action=show_pakettikauppa&sid=' . $tracking_code ); 232 $tracking_url = WC_Pakettikauppa_Shipment::tracking_url( $service_id, $tracking_code ); 233 234 ?> 235 <div> 236 <?php if ( ! empty( $tracking_code ) ) : ?> 237 <p class="pakettikauppa-shipment"> 238 <strong> 239 <?php 240 printf( 241 '%1$s<br>%2$s<br>%3$s', 242 esc_attr( $this->wc_pakettikauppa_shipment->service_title($service_id) ), 243 esc_attr( $tracking_code ), 244 esc_attr( WC_Pakettikauppa_Shipment::get_status_text($status) ) 20 class WC_Pakettikauppa_Admin 21 { 22 private $wc_pakettikauppa_shipment = null; 23 private $errors = array(); 24 25 public function __construct() 26 { 27 $this->id = 'wc_pakettikauppa_admin'; 28 } 29 30 public function load() 31 { 32 add_filter('plugin_action_links_' . WC_PAKETTIKAUPPA_BASENAME, array($this, 'add_settings_link')); 33 add_filter('plugin_row_meta', array($this, 'plugin_row_meta'), 10, 2); 34 35 add_action('admin_enqueue_scripts', array($this, 'admin_enqueue_scripts')); 36 add_action('add_meta_boxes', array($this, 'register_meta_boxes')); 37 add_action('save_post', array($this, 'save_metabox'), 10, 2); 38 add_action('admin_post_show_pakettikauppa', array($this, 'show'), 10); 39 add_action('woocommerce_email_order_meta', array($this, 'attach_tracking_to_email'), 10, 4); 40 add_action('woocommerce_admin_order_data_after_shipping_address', array($this, 'show_pickup_point_in_admin_order_meta'), 10, 1); 41 // Delete the tracking label when order is deleted so the uploads directory doesn't get too bloated 42 add_action('before_delete_post', array($this, 'delete_order_shipping_label')); 43 // Connect shipping service and pickup points in admin 44 add_action('wp_ajax_admin_update_pickup_point', array($this, 'update_meta_box_pickup_points')); 45 46 try { 47 $this->wc_pakettikauppa_shipment = new WC_Pakettikauppa_Shipment(); 48 $this->wc_pakettikauppa_shipment->load(); 49 50 } catch (Exception $e) { 51 $this->add_error($e->getMessage()); 52 $this->add_error_notice($e->getMessage()); 53 return; 54 } 55 } 56 57 /** 58 * Check if the selected service has pickup points via wp_ajax 59 */ 60 public function update_meta_box_pickup_points() 61 { 62 if (isset($_POST) && !empty($_POST['service_id'])) { 63 $service_id = $_POST['service_id']; 64 echo esc_attr(WC_Pakettikauppa_Shipment::service_has_pickup_points($service_id)); 65 wp_die(); 66 } 67 } 68 69 /** 70 * Add an error with a specified error message. 71 * 72 * @param string $message A message containing details about the error. 73 */ 74 public function add_error($message) 75 { 76 if (!empty($message)) { 77 array_push($this->errors, $message); 78 error_log($message); 79 } 80 } 81 82 /** 83 * Return all errors that have been added via add_error(). 84 * 85 * @return array Errors 86 */ 87 public function get_errors() 88 { 89 return $this->errors; 90 } 91 92 /** 93 * Clear all existing errors that have been added via add_error(). 94 */ 95 public function clear_errors() 96 { 97 unset($this->errors); 98 $this->errors = array(); 99 } 100 101 /** 102 * Add an admin error notice to wp-admin. 103 */ 104 public function add_error_notice($message) 105 { 106 if (!empty($message)) { 107 $class = 'notice notice-error'; 108 /* translators: %s: Error message */ 109 $print_error = wp_sprintf(__('An error occured: %s', 'wc-pakettikauppa'), $message); 110 printf('<div class="%1$s"><p>%2$s</p></div>', esc_attr($class), esc_html($print_error)); 111 } 112 } 113 114 /** 115 * Show row meta on the plugin screen. 116 * 117 * @param mixed $links Plugin Row Meta 118 * @param mixed $file Plugin Base file 119 * @return array 120 */ 121 public static function plugin_row_meta($links, $file) 122 { 123 if (WC_PAKETTIKAUPPA_BASENAME === $file) { 124 $row_meta = array( 125 'service' => '<a href="' . esc_url('https://www.pakettikauppa.fi') . 126 '" aria-label="' . esc_attr__('Visit Pakettikauppa', 'wc-pakettikauppa') . 127 '">' . esc_html__('Show site Pakettikauppa', 'wc-pakettikauppa') . '</a>', 245 128 ); 246 ?> 247 </strong><br> 248 249 <a href="<?php echo esc_url( $document_url ); ?>" target="_blank" class="download"><?php esc_attr_e( 'Print document', 'wc-pakettikauppa' ); ?></a> - 250 251 <?php if ( ! empty( $tracking_url ) ) : ?> 252 <a href="<?php echo esc_url( $tracking_url ); ?>" target="_blank" class="tracking"><?php esc_attr_e( 'Track', 'wc-pakettikauppa' ); ?></a> 253 <?php endif; ?> 254 </p> 255 <?php endif; ?> 256 257 <?php if ( empty( $tracking_code ) ) : ?> 258 <div class="pakettikauppa-services"> 259 <fieldset class="pakettikauppa-metabox-fieldset"> 260 <h4><?php esc_attr_e( 'Service', 'wc-pakettikauppa' ); ?></h4> 261 <?php foreach ( $active_shipping_options as $shipping_code => $shipping_settings ) : ?> 262 <?php if ( $shipping_settings['active'] === 'yes' ) : ?> 263 <label for="service-<?php echo esc_attr( $shipping_code ); ?>"> 264 <input type="radio" 265 name="wc_pakettikauppa_service_id" 266 value="<?php echo esc_attr( $shipping_code ); ?>" 267 id="service-<?php echo esc_attr( $shipping_code ); ?>" 268 <?php if ( $service_id === $shipping_code ) : ?> 269 checked="checked"; 129 return array_merge($links, $row_meta); 130 } 131 return (array)$links; 132 } 133 134 /** 135 * Register meta boxes for WooCommerce order metapage. 136 */ 137 public function register_meta_boxes() 138 { 139 foreach (wc_get_order_types('order-meta-boxes') as $type) { 140 $order_type_object = get_post_type_object($type); 141 add_meta_box( 142 'wc-pakettikauppa', 143 esc_attr__('Pakettikauppa', 'wc-pakettikauppa'), 144 array( 145 $this, 146 'meta_box', 147 ), 148 $type, 149 'side', 150 'default' 151 ); 152 } 153 } 154 155 /** 156 * Enqueue admin-specific styles and scripts. 157 */ 158 public function admin_enqueue_scripts() 159 { 160 wp_enqueue_style('wc_pakettikauppa_admin', plugin_dir_url(__FILE__) . '../assets/css/wc-pakettikauppa-admin.css'); 161 wp_enqueue_script('wc_pakettikauppa_admin_js', plugin_dir_url(__FILE__) . '../assets/js/wc-pakettikauppa-admin.js', array('jquery')); 162 } 163 164 /** 165 * Add settings link to the Pakettikauppa metabox on the plugins page when used with 166 * the WordPress hook plugin_action_links_woocommerce-pakettikauppa. 167 * 168 * @param array $links Already existing links on the plugin metabox 169 * @return array The plugin settings page link appended to the already existing links 170 */ 171 public function add_settings_link($links) 172 { 173 $url = admin_url('admin.php?page=wc-settings&tab=shipping§ion=wc_pakettikauppa_shipping_method'); 174 $link = '<a href="' . $url . '">' . esc_attr__('Settings') . '</a>'; 175 176 return array_merge(array($link), $links); 177 } 178 179 /** 180 * Show the selected pickup point in admin order meta. Use together with the hook 181 * woocommerce_admin_order_data_after_shipping_address. 182 * 183 * @param WC_Order $order The order that is currently being viewed in wp-admin 184 */ 185 public function show_pickup_point_in_admin_order_meta($order) 186 { 187 echo '<p class="form-field"><strong>' . esc_attr__('Requested pickup point', 'wc-pakettikauppa') . ':</strong><br>'; 188 if ($order->get_meta('_pakettikauppa_pickup_point')) { 189 echo esc_attr($order->get_meta('_pakettikauppa_pickup_point')); 190 echo '<br>ID: ' . esc_attr($order->get_meta('_pakettikauppa_pickup_point_id')); 191 } else { 192 echo esc_attr__('None'); 193 } 194 echo '</p>'; 195 } 196 197 /** 198 * Meta box for managing shipments. 199 */ 200 public function meta_box($post) 201 { 202 $order = wc_get_order($post->ID); 203 204 if (!WC_Pakettikauppa_Shipment::validate_order_shipping_receiver($order)) { 205 esc_attr_e('Please add shipping info to the order to manage Pakettikauppa shipments.', 'wc-pakettikauppa'); 206 return; 207 } 208 209 // Get active services from active_shipping_options 210 $settings = get_option('woocommerce_WC_Pakettikauppa_Shipping_Method_settings', null); 211 $active_shipping_options = json_decode($settings['active_shipping_options'], true); 212 213 // The tracking code will only be available if the shipment label has been generated 214 $tracking_code = get_post_meta($post->ID, '_wc_pakettikauppa_tracking_code', true); 215 $cod = get_post_meta($post->ID, '_wc_pakettikauppa_cod', true); 216 $cod_amount = get_post_meta($post->ID, '_wc_pakettikauppa_cod_amount', true); 217 $cod_reference = get_post_meta($post->ID, '_wc_pakettikauppa_cod_reference', true); 218 $service_id = get_post_meta($post->ID, '_wc_pakettikauppa_service_id', true); 219 220 $shipping_methods = $order->get_shipping_methods(); 221 $shipping_method = reset($shipping_methods); 222 $ids = explode(':', $shipping_method['method_id']); 223 if (isset($ids[1]) && !empty($ids[1])) { 224 $service_id = (int)$ids[1]; 225 } 226 227 $pickup_point = $order->get_meta('_pakettikauppa_pickup_point'); 228 $pickup_point_id = $order->get_meta('_pakettikauppa_pickup_point_id'); 229 $status = get_post_meta($post->ID, '_wc_pakettikauppa_shipment_status', true); 230 231 // Set defaults 232 if (empty($cod)) { 233 $cod = ($order->get_payment_method() === 'cod'); 234 } 235 if (empty($cod_amount)) { 236 $cod_amount = $order->get_total(); 237 } 238 if (empty($cod_reference)) { 239 $cod_reference = WC_Pakettikauppa_Shipment::calculate_reference($post->ID); 240 } 241 if (empty($service_id)) { 242 $service_id = WC_Pakettikauppa_Shipment::get_default_service($post, $order); 243 } 244 245 $document_url = admin_url('admin-post.php?post=' . $post->ID . '&action=show_pakettikauppa&sid=' . $tracking_code); 246 $tracking_url = WC_Pakettikauppa_Shipment::tracking_url($service_id, $tracking_code); 247 248 ?> 249 <div> 250 <?php if (!empty($tracking_code)) : ?> 251 <p class="pakettikauppa-shipment"> 252 <strong> 253 <?php 254 printf( 255 '%1$s<br>%2$s<br>%3$s', 256 esc_attr($this->wc_pakettikauppa_shipment->service_title($service_id)), 257 esc_attr($tracking_code), 258 esc_attr(WC_Pakettikauppa_Shipment::get_status_text($status)) 259 ); 260 ?> 261 </strong><br> 262 263 <a href="<?php echo esc_url($document_url); ?>" target="_blank" 264 class="download"><?php esc_attr_e('Print document', 'wc-pakettikauppa'); ?></a> - 265 266 <?php if (!empty($tracking_url)) : ?> 267 <a href="<?php echo esc_url($tracking_url); ?>" target="_blank" 268 class="tracking"><?php esc_attr_e('Track', 'wc-pakettikauppa'); ?></a> 270 269 <?php endif; ?> 271 /> 272 <span><?php echo esc_attr( $this->wc_pakettikauppa_shipment->service_title( $shipping_code ) ); ?></span> 273 </label> 274 <br> 275 <?php endif; ?> 276 <?php endforeach; ?> 277 278 <h4><?php esc_attr_e( 'Additional services', 'wc-pakettikauppa' ); ?></h4> 279 <input type="checkbox" name="wc_pakettikauppa_cod" value="1" id="wc-pakettikauppa-cod" 280 <?php if ( $cod ) : ?> 281 checked="checked" 282 <?php endif; ?> /> 283 <label for="wc-pakettikauppa-cod"><?php esc_attr_e( 'Cash on Delivery', 'wc-pakettikauppa' ); ?></label> 284 285 <div class="form-field" id="wc-pakettikauppa-cod-amount-wrapper"> 286 <label for="wc_pakettikauppa_cod_amount"><?php esc_attr_e( 'Amount (€):', 'wc-pakettikauppa' ); ?></label> 287 <input type="text" name="wc_pakettikauppa_cod_amount" value="<?php echo esc_attr( $cod_amount ); ?>" id="wc_pakettikauppa_cod_amount" /> 288 </div> 289 290 <div class="form-field" id="wc-pakettikauppa-cod-reference-wrapper"> 291 <label for="wc_pakettikauppa_cod_reference"><?php esc_attr_e( 'Reference:', 'wc-pakettikauppa' ); ?></label> 292 <input type="text" name="wc_pakettikauppa_cod_reference" value="<?php echo esc_attr( $cod_reference ); ?>" id="wc_pakettikauppa_cod_reference" /> 293 </div> 294 295 <input type="checkbox" style="display:none;" name="wc_pakettikauppa_pickup_points" value="1" id="wc-pakettikauppa-pickup-points" 296 <?php if ( $pickup_point ) : ?> 297 checked="checked" 270 </p> 298 271 <?php endif; ?> 299 /> 300 301 <?php 272 273 <?php if (empty($tracking_code)) : ?> 274 <div class="pakettikauppa-services"> 275 <fieldset class="pakettikauppa-metabox-fieldset"> 276 <h4><?php esc_attr_e('Service', 'wc-pakettikauppa'); ?></h4> 277 <?php foreach ($active_shipping_options as $shipping_code => $shipping_settings) : ?> 278 <?php if ($shipping_settings['active'] === 'yes') : ?> 279 <?php if ($service_id == $shipping_code) : ?> 280 <label for="service-<?php echo esc_attr($shipping_code); ?>"> 281 <input type="radio" 282 name="wc_pakettikauppa_service_id" 283 value="<?php echo esc_attr($shipping_code); ?>" 284 id="service-<?php echo esc_attr($shipping_code); ?>" 285 checked="checked" 286 /> 287 <span><?php echo esc_attr($this->wc_pakettikauppa_shipment->service_title($shipping_code)); ?></span> 288 </label> 289 <br> 290 <?php endif; ?> 291 <?php endif; ?> 292 <?php endforeach; ?> 293 294 <?php if($pickup_point): ?> 295 <input type="hidden" name="wc_pakettikauppa_pickup_points" value="1"> 296 <input type="hidden" name="wc_pakettikauppa_pickup_point_id" value="<?php echo $pickup_point_id;?>"> 297 <?php endif; ?> 298 299 </fieldset> 300 301 </div> 302 <p> 303 <input type="submit" value="<?php esc_attr_e('Create', 'wc-pakettikauppa'); ?>" 304 name="wc_pakettikauppa_create" class="button"/> 305 </p> 306 <?php else : ?> 307 <p> 308 <input type="submit" value="<?php esc_attr_e('Update Status', 'wc-pakettikauppa'); ?>" 309 name="wc_pakettikauppa_get_status" class="button"/> 310 <input type="submit" value="<?php esc_attr_e('Delete Shipping Label', 'wc-pakettikauppa'); ?>" 311 name="wc_pakettikauppa_delete_shipping_label" class="button wc-pakettikauppa-delete-button"/> 312 </p> 313 <?php endif; ?> 314 </div> 315 316 <?php 317 } 318 319 /** 320 * Save metabox values and fetch the shipping label for the order. 321 */ 322 public function save_metabox($post_id, $post) 323 { 324 if (!current_user_can('edit_post', $post_id)) { 325 return; 326 } 327 328 if (wp_is_post_autosave($post_id)) { 329 return; 330 } 331 332 if (wp_is_post_revision($post_id)) { 333 return; 334 } 335 336 if (isset($_POST['wc_pakettikauppa_create'])) { 337 338 // Bail out if the receiver has not been properly configured 339 if (!WC_Pakettikauppa_Shipment::validate_order_shipping_receiver(wc_get_order($post_id))) { 340 add_action('admin_notices', function () { 341 echo '<div class="update-nag notice">' . 342 esc_attr__('The shipping label was not created because the order does not contain valid shipping details.', 'wc-pakettikauppa') 343 . '</div>'; 344 }); 345 return; 346 } 347 348 $order = new WC_Order($post_id); 349 302 350 try { 303 $shipping_postcode = $order->get_shipping_postcode(); 304 $shipping_address_1 = $order->get_shipping_address_1(); 305 $shipping_country = $order->get_shipping_country(); 306 307 if ( empty( $shipping_country ) ) { 308 $shipping_country = 'FI'; 309 } 310 311 $pickup_points = array(); 312 313 foreach ( $active_shipping_options as $shipping_code => $shipping_settings ) { 314 $shipping_provider = $this->wc_pakettikauppa_shipment->service_provider( $shipping_code ); 315 $pickup_point_data = $this->wc_pakettikauppa_shipment->get_pickup_points( $shipping_postcode, $shipping_address_1, $shipping_country, $shipping_provider ); 316 $pickup_points = array_merge( $pickup_points, json_decode( $pickup_point_data ) ); 317 } 318 ?> 319 320 <div class="form-field" id="wc-pakettikauppa-pickup-points-wrapper"> 321 <h4><?php esc_attr_e( 'Pickup Point', 'wc-pakettikauppa' ); ?></h4> 322 <select name="wc_pakettikauppa_pickup_point_id" class="wc_pakettikauppa_pickup_point_id" id="wc_pakettikauppa_pickup_point_id"> 323 <?php foreach ( $pickup_points as $key => $value ) : ?> 324 <option value="<?php echo esc_attr( $value->pickup_point_id ); ?>" 325 <?php if ( $pickup_point_id === $value->pickup_point_id ) : ?> 326 selected 327 <?php endif; ?> 328 /> 329 <?php echo esc_attr( $value->provider . ': ' . $value->name . ' (' . $value->street_address . ')' ); ?></option> 330 <?php endforeach; ?> 331 </select> 332 </div> 333 334 <?php 335 } catch ( Exception $e ) { 336 $this->add_error( $e->getMessage() ); 337 echo '<p class="wc-pakettikauppa-metabox-error">' 338 . esc_attr__( 'Pickup point search failed! Check your error log for details.', 'wc-pakettikauppa' ) 339 . '</p>'; 351 $shipment_data = $this->wc_pakettikauppa_shipment->create_shipment($post_id); 352 353 $document_url = admin_url('admin-post.php?post=' . $post_id . '&action=show_pakettikauppa&sid=' . $shipment_data['tracking_code']); 354 $tracking_url = WC_Pakettikauppa_Shipment::tracking_url($shipment_data['service_id'], $shipment_data['tracking_code']); 355 356 // Add order note 357 $dl_link = '<a href="' . $document_url . '" target="_blank">' . esc_attr__('Print document', 'wc-pakettikauppa') . '</a>'; 358 $tracking_link = '<a href="' . $tracking_url . '" target="_blank">' . __('Track', 'wc-pakettikauppa') . '</a>'; 359 360 $order->add_order_note(sprintf( 361 /* translators: 1: Shipping service title 2: Shipment tracking code 3: Shipping label URL 4: Shipment tracking URL */ 362 __('Created Pakettikauppa %1$s shipment.<br>%2$s<br>%1$s - %3$s<br>%4$s', 'wc-pakettikauppa'), 363 $this->wc_pakettikauppa_shipment->service_title($shipment_data['service_id']), 364 $shipment_data['tracking_code'], 365 $dl_link, 366 $tracking_link 367 )); 368 369 } catch (Exception $e) { 370 $this->add_error($e->getMessage()); 371 /* translators: %s: Error message */ 372 $order->add_order_note(sprintf(esc_attr__('Failed to create Pakettikauppa shipment. Errors: %s', 'wc-pakettikauppa'), $e->getMessage())); 373 add_action('admin_notices', function () { 374 /* translators: %s: Error message */ 375 $this->add_error_notice(wp_sprintf(esc_attr__('An error occured: %s', 'wc-pakettikauppa'), $e->getMessage())); 376 }); 377 return; 340 378 } 341 ?> 342 </fieldset> 343 344 </div> 345 <p> 346 <input type="submit" value="<?php esc_attr_e( 'Create', 'wc-pakettikauppa' ); ?>" name="wc_pakettikauppa_create" class="button" /> 347 </p> 348 <?php else : ?> 349 <p> 350 <input type="submit" value="<?php esc_attr_e( 'Update Status', 'wc-pakettikauppa' ); ?>" name="wc_pakettikauppa_get_status" class="button" /> 351 <input type="submit" value="<?php esc_attr_e( 'Delete Shipping Label', 'wc-pakettikauppa' ); ?>" name="wc_pakettikauppa_delete_shipping_label" class="button wc-pakettikauppa-delete-button" /> 352 </p> 353 <?php endif; ?> 354 </div> 355 356 <?php 357 } 358 359 /** 360 * Save metabox values and fetch the shipping label for the order. 361 */ 362 public function save_metabox( $post_id, $post ) { 363 if ( ! current_user_can( 'edit_post', $post_id ) ) { 364 return; 365 } 366 367 if ( wp_is_post_autosave( $post_id ) ) { 368 return; 369 } 370 371 if ( wp_is_post_revision( $post_id ) ) { 372 return; 373 } 374 375 if ( isset( $_POST['wc_pakettikauppa_create'] ) ) { 376 377 // Bail out if the receiver has not been properly configured 378 if ( ! WC_Pakettikauppa_Shipment::validate_order_shipping_receiver( wc_get_order( $post_id ) ) ) { 379 add_action( 'admin_notices', function() { 380 echo '<div class="update-nag notice">' . 381 esc_attr__( 'The shipping label was not created because the order does not contain valid shipping details.', 'wc-pakettikauppa' ) 382 . '</div>'; 383 }); 384 return; 385 } 386 387 $order = new WC_Order( $post_id ); 388 389 try { 390 $shipment_data = $this->wc_pakettikauppa_shipment->create_shipment( $post_id ); 391 392 $document_url = admin_url( 'admin-post.php?post=' . $post_id . '&action=show_pakettikauppa&sid=' . $shipment_data['tracking_code'] ); 393 $tracking_url = WC_Pakettikauppa_Shipment::tracking_url( $shipment_data['service_id'], $shipment_data['tracking_code'] ); 394 395 // Add order note 396 $dl_link = '<a href="' . $document_url . '" target="_blank">' . esc_attr__( 'Print document', 'wc-pakettikauppa' ) . '</a>'; 397 $tracking_link = '<a href="' . $tracking_url . '" target="_blank">' . __( 'Track', 'wc-pakettikauppa' ) . '</a>'; 398 399 $order->add_order_note( sprintf( 400 /* translators: 1: Shipping service title 2: Shipment tracking code 3: Shipping label URL 4: Shipment tracking URL */ 401 __('Created Pakettikauppa %1$s shipment.<br>%2$s<br>%1$s - %3$s<br>%4$s', 'wc-pakettikauppa'), 402 $this->wc_pakettikauppa_shipment->service_title($shipment_data['service_id']), 403 $shipment_data['tracking_code'], 404 $dl_link, 405 $tracking_link 406 )); 407 408 } catch ( Exception $e ) { 409 $this->add_error( $e->getMessage() ); 410 /* translators: %s: Error message */ 411 $order->add_order_note( sprintf( esc_attr__('Failed to create Pakettikauppa shipment. Errors: %s', 'wc-pakettikauppa'), $e->getMessage() ) ); 412 add_action( 'admin_notices', function() { 413 /* translators: %s: Error message */ 414 $this->add_error_notice( wp_sprintf( esc_attr__( 'An error occured: %s', 'wc-pakettikauppa' ), $e->getMessage() ) ); 415 }); 416 return; 417 } 418 } elseif ( isset( $_POST['wc_pakettikauppa_get_status'] ) ) { 419 try { 420 $status_code = $this->wc_pakettikauppa_shipment->get_shipment_status( $post_id ); 421 update_post_meta( $post_id, '_wc_pakettikauppa_shipment_status', $status_code ); 422 423 } catch ( Exception $e ) { 424 $this->add_error( $e->getMessage() ); 425 add_action( 'admin_notices', function() { 426 /* translators: %s: Error message */ 427 $this->add_error_notice( wp_sprintf( esc_attr__( 'An error occured: %s', 'wc-pakettikauppa' ), $e->getMessage() ) ); 428 }); 429 return; 430 } 431 } elseif ( isset( $_POST['wc_pakettikauppa_delete_shipping_label'] ) ) { 432 try { 433 // Delete old shipping label 434 $this->delete_order_shipping_label( $post_id ); 435 // Delete old tracking code 436 update_post_meta( $post_id, '_wc_pakettikauppa_tracking_code', '' ); 437 438 $order = new WC_Order( $post_id ); 439 $order->add_order_note( esc_attr__('Successfully deleted Pakettikauppa shipping label.', 'wc-pakettikauppa') ); 440 441 } catch ( Exception $e ) { 442 $this->add_error( $e->getMessage() ); 443 add_action( 'admin_notices', function() { 444 /* translators: %s: Error message */ 445 $this->add_error_notice( wp_sprintf( esc_attr__( 'An error occured: %s', 'wc-pakettikauppa' ), $e->getMessage() ) ); 446 }); 447 448 $order = new WC_Order( $post_id ); 449 $order->add_order_note( 450 sprintf( 451 /* translators: %s: Error message */ 452 esc_attr__('Deleting Pakettikauppa shipment failed! Errors: %s', 'wc-pakettikauppa'), 453 $e->getMessage() 454 ) 455 ); 456 } 457 } else { 458 return; 459 } 460 } 461 462 463 /** 464 * Output shipment label as PDF in browser. 465 */ 466 public function show() { 467 $shipment_id = false; 468 469 // Find shipment ID either from GET parameters or from the order 470 // data. 471 if ( isset( $_REQUEST['sid'] ) ) { 472 $shipment_id = $_REQUEST['sid']; 473 } else { 474 esc_attr_e( 'Shipment tracking code is not defined.', 'wc-pakettikauppa' ); 475 } 476 477 if ( false !== $shipment_id ) { 478 $upload_dir = wp_upload_dir(); 479 480 // Read file 481 $filepath = WC_PAKETTIKAUPPA_PRIVATE_DIR . '/' . $shipment_id . '.pdf'; 482 header('X-Sendfile: ' . $filepath); 483 484 // Output 485 $contents = file_get_contents( $filepath ); 486 header('Content-type:application/pdf'); 487 header("Content-Disposition:inline;filename={$shipment_id}.pdf"); 488 print $contents; 489 exit; 490 } 491 492 esc_attr_e( 'Cannot find shipment with given shipment number.', 'wc-pakettikauppa' ); 493 exit; 494 } 495 496 /** 497 * Attach tracking URL to email. 498 */ 499 public function attach_tracking_to_email( $order, $sent_to_admin = false, $plain_text = false, $email = null ) { 500 501 $settings = get_option( 'woocommerce_WC_Pakettikauppa_Shipping_Method_settings', null ); 502 $add_to_email = $settings['add_tracking_to_email']; 503 504 if ( 'yes' === $add_to_email && isset( $email->id ) && 'customer_completed_order' === $email->id ) { 505 506 $tracking_code = get_post_meta( $order->get_ID(), '_wc_pakettikauppa_tracking_code', true ); 507 $tracking_url = WC_Pakettikauppa_Shipment::tracking_url( '', $tracking_code ); 508 509 if ( ! empty( $tracking_code ) && ! empty( $tracking_url ) ) { 510 if ( $plain_text ) { 511 /* translators: %s: Shipment tracking URL */ 512 echo sprintf( esc_html__( "You can track your order at %1$s.\n\n", 'wc-pakettikauppa' ), esc_url( $tracking_url ) ); 379 } elseif (isset($_POST['wc_pakettikauppa_get_status'])) { 380 try { 381 $status_code = $this->wc_pakettikauppa_shipment->get_shipment_status($post_id); 382 update_post_meta($post_id, '_wc_pakettikauppa_shipment_status', $status_code); 383 384 } catch (Exception $e) { 385 $this->add_error($e->getMessage()); 386 add_action('admin_notices', function () { 387 /* translators: %s: Error message */ 388 $this->add_error_notice(wp_sprintf(esc_attr__('An error occured: %s', 'wc-pakettikauppa'), $e->getMessage())); 389 }); 390 return; 391 } 392 } elseif (isset($_POST['wc_pakettikauppa_delete_shipping_label'])) { 393 try { 394 // Delete old shipping label 395 $this->delete_order_shipping_label($post_id); 396 // Delete old tracking code 397 update_post_meta($post_id, '_wc_pakettikauppa_tracking_code', ''); 398 399 $order = new WC_Order($post_id); 400 $order->add_order_note(esc_attr__('Successfully deleted Pakettikauppa shipping label.', 'wc-pakettikauppa')); 401 402 } catch (Exception $e) { 403 $this->add_error($e->getMessage()); 404 add_action('admin_notices', function () { 405 /* translators: %s: Error message */ 406 $this->add_error_notice(wp_sprintf(esc_attr__('An error occured: %s', 'wc-pakettikauppa'), $e->getMessage())); 407 }); 408 409 $order = new WC_Order($post_id); 410 $order->add_order_note( 411 sprintf( 412 /* translators: %s: Error message */ 413 esc_attr__('Deleting Pakettikauppa shipment failed! Errors: %s', 'wc-pakettikauppa'), 414 $e->getMessage() 415 ) 416 ); 417 } 513 418 } else { 514 echo '<h2>' . esc_attr__( 'Tracking', 'wc-pakettikauppa' ) . '</h2>'; 515 /* translators: 1: Shipment tracking URL 2: Shipment tracking code */ 516 echo '<p>' . sprintf( __( 'You can <a href="%1$s">track your order</a> with tracking code %2$s.', 'wc-pakettikauppa' ), esc_url( $tracking_url ), esc_attr( $tracking_code ) ) . '</p>'; 517 } 518 } 519 } 520 } 521 522 /** 523 * Remove the shipping label of an order from the wc-pakettikauppa uploads directory 524 * 525 * @param int $post_id The post id of the order which is to be deleted 526 */ 527 public function delete_order_shipping_label( $post_id ) { 528 // Check that the post type is order 529 $post_type = get_post_type( $post_id ); 530 if ( $post_type !== 'shop_order' ) { 531 return; 532 } 533 534 $tracking_code = get_post_meta( $post_id, '_wc_pakettikauppa_tracking_code', true ); 535 if ( ! empty( $tracking_code ) ) { 536 $filepath = WC_PAKETTIKAUPPA_PRIVATE_DIR . '/' . $tracking_code . '.pdf'; 537 538 // Delete if file exists 539 if ( file_exists( $filepath) ) { 540 unlink( $filepath ); 541 } 542 } 543 } 419 return; 420 } 421 } 422 423 424 /** 425 * Output shipment label as PDF in browser. 426 */ 427 public function show() 428 { 429 $shipment_id = false; 430 431 // Find shipment ID either from GET parameters or from the order 432 // data. 433 if (isset($_REQUEST['sid'])) { 434 $shipment_id = $_REQUEST['sid']; 435 } else { 436 esc_attr_e('Shipment tracking code is not defined.', 'wc-pakettikauppa'); 437 } 438 439 if (false !== $shipment_id) { 440 $contents = $this->wc_pakettikauppa_shipment->fetch_shipping_label($shipment_id); 441 442 // Output 443 header('Content-type:application/pdf'); 444 header("Content-Disposition:inline;filename={$shipment_id}.pdf"); 445 print base64_decode($contents->{"response.file"}); 446 exit; 447 } 448 449 esc_attr_e('Cannot find shipment with given shipment number.', 'wc-pakettikauppa'); 450 exit; 451 } 452 453 /** 454 * Attach tracking URL to email. 455 */ 456 public function attach_tracking_to_email($order, $sent_to_admin = false, $plain_text = false, $email = null) 457 { 458 459 $settings = get_option('woocommerce_WC_Pakettikauppa_Shipping_Method_settings', null); 460 $add_to_email = $settings['add_tracking_to_email']; 461 462 if ('yes' === $add_to_email && isset($email->id) && 'customer_completed_order' === $email->id) { 463 464 $tracking_code = get_post_meta($order->get_ID(), '_wc_pakettikauppa_tracking_code', true); 465 $tracking_url = WC_Pakettikauppa_Shipment::tracking_url('', $tracking_code); 466 467 if (!empty($tracking_code) && !empty($tracking_url)) { 468 if ($plain_text) { 469 /* translators: %s: Shipment tracking URL */ 470 echo sprintf(esc_html__("You can track your order at %1$s.\n\n", 'wc-pakettikauppa'), esc_url($tracking_url)); 471 } else { 472 echo '<h2>' . esc_attr__('Tracking', 'wc-pakettikauppa') . '</h2>'; 473 /* translators: 1: Shipment tracking URL 2: Shipment tracking code */ 474 echo '<p>' . sprintf(__('You can <a href="%1$s">track your order</a> with tracking code %2$s.', 'wc-pakettikauppa'), esc_url($tracking_url), esc_attr($tracking_code)) . '</p>'; 475 } 476 } 477 } 478 } 479 480 /** 481 * Remove the shipping label of an order from the wc-pakettikauppa uploads directory 482 * 483 * @param int $post_id The post id of the order which is to be deleted 484 */ 485 public function delete_order_shipping_label($post_id) 486 { 487 // Check that the post type is order 488 $post_type = get_post_type($post_id); 489 if ($post_type !== 'shop_order') { 490 return; 491 } 492 } 544 493 545 494 } -
woo-pakettikauppa/trunk/includes/class-wc-pakettikauppa-shipment.php
r1908674 r1966477 5 5 6 6 // Prevent direct access to this script 7 if ( ! defined( 'ABSPATH' )) {8 exit;7 if (!defined('ABSPATH')) { 8 exit; 9 9 } 10 10 … … 28 28 * @author Seravo 29 29 */ 30 class WC_Pakettikauppa_Shipment { 31 private $wc_pakettikauppa_client = null; 32 private $wc_pakettikauppa_settings = null; 33 34 public function __construct() { 35 $this->id = 'wc_pakettikauppa_shipment'; 36 } 37 38 public function load() { 39 // Use option from database directly as WC_Pakettikauppa_Shipping_Method object is not accessible here 40 $settings = get_option( 'woocommerce_WC_Pakettikauppa_Shipping_Method_settings', null ); 41 42 if ( false === $settings ) { 43 throw new Exception( 44 'WooCommerce Pakettikauppa: 30 class WC_Pakettikauppa_Shipment 31 { 32 private $wc_pakettikauppa_client = null; 33 private $wc_pakettikauppa_settings = null; 34 35 public function __construct() 36 { 37 $this->id = 'wc_pakettikauppa_shipment'; 38 } 39 40 public function load() 41 { 42 // Use option from database directly as WC_Pakettikauppa_Shipping_Method object is not accessible here 43 $settings = get_option('woocommerce_WC_Pakettikauppa_Shipping_Method_settings', null); 44 45 if (false === $settings) { 46 throw new Exception( 47 'WooCommerce Pakettikauppa: 45 48 woocommerce_WC_Pakettikauppa_Shipping_Method_settings was not 46 49 found in the database!' 50 ); 51 } 52 53 $this->wc_pakettikauppa_settings = $settings; 54 55 $account_number = $settings['account_number']; 56 $secret_key = $settings['secret_key']; 57 $mode = $settings['mode']; 58 $is_test_mode = ($mode === 'production' ? false : true); 59 60 $options_array = array( 61 'api_key' => $account_number, 62 'secret' => $secret_key, 63 'test_mode' => $is_test_mode, 47 64 ); 48 } 49 50 $this->wc_pakettikauppa_settings = $settings; 51 52 $account_number = $settings['account_number']; 53 $secret_key = $settings['secret_key']; 54 $mode = $settings['mode']; 55 $is_test_mode = ( $mode === 'production' ? false : true ); 56 57 $options_array = array( 58 'api_key' => $account_number, 59 'secret' => $secret_key, 60 'test_mode' => $is_test_mode, 61 ); 62 63 $this->wc_pakettikauppa_client = new Pakettikauppa\Client( $options_array ); 64 } 65 66 /** 67 * Get the status of a shipment 68 * 69 * @param int $post_id The post id of the order to update status of 70 * @return int The status code of the shipment 71 */ 72 public function get_shipment_status( $post_id ) { 73 $tracking_code = get_post_meta( $post_id, '_wc_pakettikauppa_tracking_code', true); 74 75 if ( ! empty( $tracking_code ) ) { 76 $result = $this->wc_pakettikauppa_client->getShipmentStatus($tracking_code); 77 78 $data = json_decode( $result ); 79 80 if ( ! empty( $data ) && isset( $data[0] ) ) { 81 return $data[0]->{'status_code'}; 82 } 83 return ''; 84 } 85 86 } 87 88 /** 89 * Create Pakettikauppa shipment from order 90 * 91 * @param int $post_id The post id of the order to ship 92 * @return array Shipment details 93 */ 94 public function create_shipment( $post_id ) { 95 $shipment = new Shipment(); 96 $service_id = $_REQUEST['wc_pakettikauppa_service_id']; 97 $shipment->setShippingMethod( $service_id ); 98 99 $sender = new Sender(); 100 $sender->setName1( $this->wc_pakettikauppa_settings['sender_name'] ); 101 $sender->setAddr1( $this->wc_pakettikauppa_settings['sender_address'] ); 102 $sender->setPostcode( $this->wc_pakettikauppa_settings['sender_postal_code'] ); 103 $sender->setCity( $this->wc_pakettikauppa_settings['sender_city'] ); 104 $sender->setCountry( 'FI' ); 105 $shipment->setSender($sender); 106 107 $order = new WC_Order( $post_id ); 108 109 $receiver = new Receiver(); 110 $receiver->setName1( $order->get_formatted_shipping_full_name() ); 111 $receiver->setAddr1( $order->get_shipping_address_1() ); 112 $receiver->setAddr2( $order->get_shipping_address_2() ); 113 $receiver->setPostcode( $order->get_shipping_postcode() ); 114 $receiver->setCity( $order->get_shipping_city() ); 115 $receiver->setCountry('FI'); 116 $receiver->setEmail( $order->get_billing_email() ); 117 $receiver->setPhone( $order->get_billing_phone() ); 118 $shipment->setReceiver( $receiver ); 119 120 $info = new Info(); 121 $info->setReference( $order->get_order_number() ); 122 $info->setCurrency( get_woocommerce_currency() ); 123 $shipment->setShipmentInfo( $info ); 124 125 $parcel = new Parcel(); 126 $parcel->setWeight( $this::order_weight( $order ) ); 127 $parcel->setVolume( $this::order_volume( $order ) ); 128 $shipment->addParcel( $parcel ); 129 130 $cod = false; 131 132 if ( isset( $_REQUEST['wc_pakettikauppa_cod'] ) && $_REQUEST['wc_pakettikauppa_cod'] ) { 133 $cod = true; 134 $cod_amount = floatval( str_replace( ',', '.', $_REQUEST['wc_pakettikauppa_cod_amount'] ) ); 135 $cod_reference = trim( $_REQUEST['wc_pakettikauppa_cod_reference'] ); 136 $cod_iban = $this->wc_pakettikauppa_settings['cod_iban']; 137 $cod_bic = $this->wc_pakettikauppa_settings['cod_bic']; 138 139 $additional_service = new AdditionalService(); 140 $additional_service->addSpecifier( 'amount', $cod_amount ); 141 $additional_service->addSpecifier( 'account', $cod_iban ); 142 $additional_service->addSpecifier( 'codbic', $cod_bic ); 143 $additional_service->setServiceCode( 3101 ); 144 $shipment->addAdditionalService($additional_service); 145 146 update_post_meta( $post_id, '_wc_pakettikauppa_cod', $cod); 147 update_post_meta( $post_id, '_wc_pakettikauppa_cod_amount', $cod_amount); 148 update_post_meta( $post_id, '_wc_pakettikauppa_cod_reference', $cod_reference); 149 } 150 151 $pickup_point = false; 152 if ( isset( $_REQUEST['wc_pakettikauppa_pickup_points'] ) && $_REQUEST['wc_pakettikauppa_pickup_points'] ) { 153 $pickup_point = true; 154 $pickup_point_id = intval( $_REQUEST['wc_pakettikauppa_pickup_point_id'] ); 155 156 $shipment->setPickupPoint( $pickup_point_id ); 157 158 update_post_meta( $post_id, '_wc_pakettikauppa_pickup_point', $pickup_point); 159 update_post_meta( $post_id, '_wc_pakettikauppa_pickup_point_id', $pickup_point_id); 160 } 161 162 try { 163 if ( $this->wc_pakettikauppa_client->createTrackingCode($shipment) ) { 164 $tracking_code = $shipment->getTrackingCode()->__toString(); 165 } 166 } catch ( Exception $e ) { 167 /* translators: %s: Error message */ 168 throw new Exception( wp_sprintf( __( 'WooCommerce Pakettikauppa: tracking code creation failed: %s', 'wc-pakettikauppa' ), $e->getMessage() ) ); 169 } 170 171 if ( ! empty( $tracking_code ) ) { 172 $this->wc_pakettikauppa_client->fetchShippingLabel( $shipment ); 173 $upload_dir = wp_upload_dir(); 174 $filepath = WC_PAKETTIKAUPPA_PRIVATE_DIR . '/' . $tracking_code . '.pdf'; 175 file_put_contents( $filepath, base64_decode( $shipment->getPdf() ) ); 176 177 update_post_meta( $post_id, '_wc_pakettikauppa_tracking_code', $tracking_code ); 178 } 179 180 update_post_meta( $post_id, '_wc_pakettikauppa_service_id', $service_id ); 181 182 return array( 183 'tracking_code' => $tracking_code, 184 'service_id' => $service_id, 185 ); 186 } 187 188 /** 189 * Return pickup points near a location specified by the parameters. 190 * 191 * @param int $postcode The postcode of the pickup point 192 * @param string $street_address The street address of the pickup point 193 * @param string $country The country in which the pickup point is located 194 * @param string $service_provider A service that should be provided by the pickup point 195 * @return array The pickup points based on the parameters, or empty array if none were found 196 */ 197 public function get_pickup_points( $postcode, $street_address = null, $country = null, $service_provider = null ) { 198 $pickup_point_limit = 5; // Default limit value for pickup point search 199 200 if ( isset( $this->wc_pakettikauppa_settings['pickup_points_search_limit'] ) && ! empty( $this->wc_pakettikauppa_settings['pickup_points_search_limit'] ) ) { 201 $pickup_point_limit = intval( $this->wc_pakettikauppa_settings['pickup_points_search_limit'] ); 202 } 203 204 $pickup_point_data = $this->wc_pakettikauppa_client->searchPickupPoints( $postcode, $street_address, $country, $service_provider, $pickup_point_limit); 205 if ( $pickup_point_data === 'Bad request' ) { 206 throw new Exception( __( 'WC_Pakettikauppa: An error occured when searching pickup points.', 'wc-pakettikauppa' ) ); 207 } 208 return $pickup_point_data; 209 } 210 211 /** 212 * Get all available shipping services. 213 * 214 * @return array Available shipping services 215 */ 216 public function services() { 217 $services = array(); 218 219 // @TODO: File bug upstream about result being string instead of object by default 220 $transient_name = 'wc_pakettikauppa_shipping_methods'; 221 $transent_time = 86400; // 24 hours 222 $all_shipping_methods = get_transient( $transient_name ); 223 224 if ( false === $all_shipping_methods ) { 225 $all_shipping_methods = json_decode( $this->wc_pakettikauppa_client->listShippingMethods() ); 226 set_transient( $transient_name, $all_shipping_methods, $transient_time ); 227 } 228 // List all available methods as shipping options on checkout page 229 if ( ! empty( $all_shipping_methods ) ) { 230 foreach ( $all_shipping_methods as $shipping_method ) { 231 $services[$shipping_method->shipping_method_code] = sprintf( '%1$s %2$s', $shipping_method->service_provider, $shipping_method->name ); 232 } 233 } 234 return $services; 235 } 236 237 /** 238 * Get the title of a service by providing its code. 239 * 240 * @param int $service_code The code of a service 241 * @return string The service title matching with the provided code, or false if not found 242 */ 243 public function service_title( $service_code ) { 244 $services = $this->services(); 245 if ( isset( $services[$service_code] ) ) { 246 return $services[$service_code]; 247 } 248 249 return false; 250 } 251 252 /** 253 * Get the provider of a service by providing its code. 254 * 255 * @param int $service_code The code of a service 256 * @return string The service provider matching with the provided code, or false if not found 257 */ 258 public function service_provider( $service_code ) { 259 $services = array(); 260 261 $transient_name = 'wc_pakettikauppa_shipping_methods'; 262 $transent_time = 86400; // 24 hours 263 $all_shipping_methods = get_transient( $transient_name ); 264 265 if ( false === $all_shipping_methods ) { 266 try { 267 $all_shipping_methods = json_decode( $this->wc_pakettikauppa_client->listShippingMethods() ); 268 set_transient( $transient_name, $all_shipping_methods, $transient_time ); 269 270 } catch ( Exception $e ) { 271 /* translators: %s: Error message */ 272 throw new Exception( wp_sprintf( __( 'WooCommerce Pakettikauppa: an error occured when accessing service providers: %s', 'wc-pakettikauppa' ), $e->getMessage() ) ); 273 } 274 } 275 276 if ( ! empty( $all_shipping_methods ) ) { 277 foreach ( $all_shipping_methods as $shipping_method ) { 278 if ( intval($service_code) === intval($shipping_method->shipping_method_code) ) { 279 return $shipping_method->service_provider; 280 } 281 } 282 } 283 return false; 284 } 285 286 /** 287 * Get the status text of a shipment that matches a specified status code. 288 * 289 * @param int $status_code A status code 290 * @return string The status text matching the provided code, or unknown status if the 291 * code is unknown. 292 */ 293 public static function get_status_text( $status_code ) { 294 $status = ''; 295 296 switch ( intval($status_code) ) { 297 case 13: 298 $status = __( 'Item is collected from sender - picked up', 'wc-pakettikauppa' ); 299 break; 300 case 20: 301 $status = __( 'Exception', 'wc-pakettikauppa' ); 302 break; 303 case 22: 304 $status = __( 'Item has been handed over to the recipient', 'wc-pakettikauppa' ); 305 break; 306 case 31: 307 $status = __( 'Item is in transport', 'wc-pakettikauppa' ); 308 break; 309 case 38: 310 $status = __( 'C.O.D payment is paid to the sender', 'wc-pakettikauppa' ); 311 break; 312 case 45: 313 $status = __( 'Informed consignee of arrival', 'wc-pakettikauppa' ); 314 break; 315 case 48: 316 $status = __( 'Item is loaded onto a means of transport', 'wc-pakettikauppa' ); 317 break; 318 case 56: 319 $status = __( 'Item not delivered – delivery attempt made', 'wc-pakettikauppa' ); 320 break; 321 case 68: 322 $status = __( 'Pre-information is received from sender', 'wc-pakettikauppa' ); 323 break; 324 case 71: 325 $status = __( 'Item is ready for delivery transportation', 'wc-pakettikauppa'); 326 break; 327 case 77: 328 $status = __( 'Item is returning to the sender', 'wc-pakettikauppa' ); 329 break; 330 case 91: 331 $status = __( 'Item is arrived to a post office', 'wc-pakettikauppa' ); 332 break; 333 case 99: 334 $status = __( 'Outbound', 'wc-pakettikauppa' ); 335 break; 336 default: 337 /* translators: %s: Status code */ 338 $status = wp_sprintf( __( 'Unknown status: %s', 'wc-pakettikauppa' ), $status_code ); 339 break; 340 } 341 342 return $status; 343 } 344 345 /** 346 * Calculate the total shipping weight of an order. 347 * 348 * @param WC_Order $order The order to calculate the weight of 349 * @return int The total weight of the order 350 */ 351 public static function order_weight( $order ) { 352 $weight = 0; 353 354 if ( count( $order->get_items() ) > 0 ) { 355 foreach ( $order->get_items() as $item ) { 356 if ( $item['product_id'] > 0 ) { 357 $product = $order->get_product_from_item( $item ); 358 if ( ! $product->is_virtual() ) { 359 $weight += wc_get_weight($product->get_weight() * $item['qty'], 'kg'); 360 } 361 } 362 } 363 } 364 365 return $weight; 366 } 367 368 /** 369 * Calculate the total shipping volume of an order in cubic meters. 370 * 371 * @param WC_Order $order The order to calculate the volume of 372 * @return int The total volume of the order (m^3) 373 */ 374 public static function order_volume( $order ) { 375 $volume = 0; 376 377 if ( count( $order->get_items() ) > 0 ) { 378 foreach ( $order->get_items() as $item ) { 379 if ( $item['product_id'] > 0 ) { 380 $product = $order->get_product_from_item( $item ); 381 if ( ! $product->is_virtual() ) { 382 // Ensure that the volume is in metres 383 $woo_dim_unit = strtolower( get_option('woocommerce_dimension_unit') ); 384 switch ( $woo_dim_unit ) { 385 case 'mm': 386 $dim_multiplier = 0.001; 387 break; 388 case 'cm': 389 $dim_multiplier = 0.01; 390 break; 391 case 'dm': 392 $dim_multiplier = 0.1; 393 break; 394 default: 395 $dim_multiplier = 1; 396 } 397 // Calculate total volume 398 $volume += pow($dim_multiplier, 3) * $product->get_width() 399 * $product->get_height() * $product->get_length() * $item['qty']; 400 } 401 } 402 } 403 } 404 405 return $volume; 406 } 407 408 /** 409 * Get the full-length tracking url of a shipment by providing its service id and tracking code. 410 * Use tracking url provided by pakettikauppa.fi. 411 * 412 * @param int $service_id The id of the service that is used for the shipment 413 * @param int $tracking_code The tracking code of the shipment 414 * @return string The full tracking url for the order 415 */ 416 public static function tracking_url( $service_id, $tracking_code ) { 417 $tracking_url = 'https://www.pakettikauppa.fi/seuranta/?' . $tracking_code; 418 return $tracking_url; 419 } 420 421 /** 422 * Calculate Finnish invoice reference from order ID 423 * http://tarkistusmerkit.teppovuori.fi/tarkmerk.htm#viitenumero 424 * 425 * @param int $id The id of the order to calculate the reference of 426 * @return int The reference number calculated from the id 427 */ 428 public static function calculate_reference( $id ) { 429 $weights = array( 7, 3, 1, 7, 3, 1, 7, 3, 1, 7, 3, 1, 7, 3, 1, 7, 3, 1, 7 ); 430 $base = str_split( strval( ( $id + 100 ) ) ); 431 $reversed_base = array_reverse( $base ); 432 $sum = 0; 433 $reversed_base_length = count( $reversed_base ); 434 435 for ( $i = 0; $i < $reversed_base_length; $i++ ) { 436 $coefficient = array_shift( $weights ); 437 $sum += $reversed_base[$i] * $coefficient; 438 } 439 440 $checksum = ( $sum % 10 === 0 ) ? 0 : ( 10 - $sum % 10 ); 441 442 $reference = implode( '', $base ) . $checksum; 443 return $reference; 444 } 445 446 /** 447 * Return the default shipping service if none has been specified 448 * 449 * @TODO: Does this method really need $post or $order, as the default service should 450 * not be order-specific? 451 */ 452 public static function get_default_service( $post, $order ) { 453 // @TODO: Maybe use an option in database so the merchant can set it in settings 454 $service = '2103'; 455 return $service; 456 } 457 458 /** 459 * Validate order details in wp-admin. Especially useful, when creating orders in wp-admin, 460 * 461 * @param WC_Order $order The order that needs its info to be validated 462 * @return True, if the details where valid, or false if not 463 */ 464 public static function validate_order_shipping_receiver( $order ) { 465 // Check shipping info first 466 $no_shipping_name = (bool) empty( $order->get_formatted_shipping_full_name() ); 467 $no_shipping_address = (bool) empty( $order->get_shipping_address_1() ) && empty( $order->get_shipping_address_2() ); 468 $no_shipping_postcode = (bool) empty( $order->get_shipping_postcode() ); 469 $no_shipping_city = (bool) empty( $order->get_shipping_city() ); 470 471 if ( $no_shipping_name || $no_shipping_address || $no_shipping_postcode || $no_shipping_city ) { 472 return false; 473 } 474 return true; 475 } 476 477 public static function service_has_pickup_points( $service_id ) { 478 // @TODO: Find out if the Pakettikauppa API can be used to check if the service uses 479 // pickup points instead of hard coding them here. 480 $services_with_pickup_points = array( 481 '2103', 482 '80010', 483 '90010', 484 '90080', 485 ); 486 487 if ( in_array( $service_id, $services_with_pickup_points, true ) ) { 488 return true; 489 } 490 return false; 491 } 65 66 $this->wc_pakettikauppa_client = new Pakettikauppa\Client($options_array); 67 } 68 69 /** 70 * Get the status of a shipment 71 * 72 * @param int $post_id The post id of the order to update status of 73 * @return int The status code of the shipment 74 */ 75 public function get_shipment_status($post_id) 76 { 77 $tracking_code = get_post_meta($post_id, '_wc_pakettikauppa_tracking_code', true); 78 79 if (!empty($tracking_code)) { 80 $result = $this->wc_pakettikauppa_client->getShipmentStatus($tracking_code); 81 82 $data = json_decode($result); 83 84 if (!empty($data) && isset($data[0])) { 85 return $data[0]->{'status_code'}; 86 } 87 return ''; 88 } 89 90 } 91 92 /** 93 * Create Pakettikauppa shipment from order 94 * 95 * @param int $post_id The post id of the order to ship 96 * @return array Shipment details 97 */ 98 public function create_shipment($post_id) 99 { 100 $shipment = new Shipment(); 101 $service_id = $_REQUEST['wc_pakettikauppa_service_id']; 102 $shipment->setShippingMethod($service_id); 103 104 $sender = new Sender(); 105 $sender->setName1($this->wc_pakettikauppa_settings['sender_name']); 106 $sender->setAddr1($this->wc_pakettikauppa_settings['sender_address']); 107 $sender->setPostcode($this->wc_pakettikauppa_settings['sender_postal_code']); 108 $sender->setCity($this->wc_pakettikauppa_settings['sender_city']); 109 $sender->setCountry('FI'); 110 $shipment->setSender($sender); 111 112 $order = new WC_Order($post_id); 113 114 $receiver = new Receiver(); 115 $receiver->setName1($order->get_formatted_shipping_full_name()); 116 $receiver->setAddr1($order->get_shipping_address_1()); 117 $receiver->setAddr2($order->get_shipping_address_2()); 118 $receiver->setPostcode($order->get_shipping_postcode()); 119 $receiver->setCity($order->get_shipping_city()); 120 $receiver->setCountry('FI'); 121 $receiver->setEmail($order->get_billing_email()); 122 $receiver->setPhone($order->get_billing_phone()); 123 $shipment->setReceiver($receiver); 124 125 $info = new Info(); 126 $info->setReference($order->get_order_number()); 127 $info->setCurrency(get_woocommerce_currency()); 128 $shipment->setShipmentInfo($info); 129 130 $parcel = new Parcel(); 131 $parcel->setWeight($this::order_weight($order)); 132 $parcel->setVolume($this::order_volume($order)); 133 134 if (isset($this->wc_pakettikauppa_settings['info_code']) && $this->wc_pakettikauppa_settings['info_code'] != null && $this->wc_pakettikauppa_settings['info_code'] != '') { 135 $parcel->setInfocode($this->wc_pakettikauppa_settings['info_code']); 136 } 137 $shipment->addParcel($parcel); 138 139 $cod = false; 140 141 if (isset($_REQUEST['wc_pakettikauppa_cod']) && $_REQUEST['wc_pakettikauppa_cod']) { 142 $cod = true; 143 $cod_amount = floatval(str_replace(',', '.', $_REQUEST['wc_pakettikauppa_cod_amount'])); 144 $cod_reference = trim($_REQUEST['wc_pakettikauppa_cod_reference']); 145 $cod_iban = $this->wc_pakettikauppa_settings['cod_iban']; 146 $cod_bic = $this->wc_pakettikauppa_settings['cod_bic']; 147 148 $additional_service = new AdditionalService(); 149 $additional_service->addSpecifier('amount', $cod_amount); 150 $additional_service->addSpecifier('account', $cod_iban); 151 $additional_service->addSpecifier('codbic', $cod_bic); 152 $additional_service->setServiceCode(3101); 153 $shipment->addAdditionalService($additional_service); 154 155 update_post_meta($post_id, '_wc_pakettikauppa_cod', $cod); 156 update_post_meta($post_id, '_wc_pakettikauppa_cod_amount', $cod_amount); 157 update_post_meta($post_id, '_wc_pakettikauppa_cod_reference', $cod_reference); 158 } 159 160 $pickup_point = false; 161 if (isset($_REQUEST['wc_pakettikauppa_pickup_points']) && $_REQUEST['wc_pakettikauppa_pickup_points']) { 162 $pickup_point = true; 163 $pickup_point_id = intval($_REQUEST['wc_pakettikauppa_pickup_point_id']); 164 165 $shipment->setPickupPoint($pickup_point_id); 166 167 update_post_meta($post_id, '_wc_pakettikauppa_pickup_point', $pickup_point); 168 update_post_meta($post_id, '_wc_pakettikauppa_pickup_point_id', $pickup_point_id); 169 } 170 171 try { 172 if ($this->wc_pakettikauppa_client->createTrackingCode($shipment)) { 173 $tracking_code = $shipment->getTrackingCode()->__toString(); 174 } 175 } catch (Exception $e) { 176 /* translators: %s: Error message */ 177 throw new Exception(wp_sprintf(__('WooCommerce Pakettikauppa: tracking code creation failed: %s', 'wc-pakettikauppa'), $e->getMessage())); 178 } 179 180 if (!empty($tracking_code)) { 181 update_post_meta($post_id, '_wc_pakettikauppa_tracking_code', $tracking_code); 182 } 183 184 update_post_meta($post_id, '_wc_pakettikauppa_service_id', $service_id); 185 186 return array( 187 'tracking_code' => $tracking_code, 188 'service_id' => $service_id, 189 ); 190 } 191 192 public function fetch_shipping_label($tracking_code) { 193 return $this->wc_pakettikauppa_client->fetchShippingLabels(array($tracking_code)); 194 195 } 196 197 /** 198 * Return pickup points near a location specified by the parameters. 199 * 200 * @param int $postcode The postcode of the pickup point 201 * @param string $street_address The street address of the pickup point 202 * @param string $country The country in which the pickup point is located 203 * @param string $service_provider A service that should be provided by the pickup point 204 * @return array The pickup points based on the parameters, or empty array if none were found 205 */ 206 public function get_pickup_points($postcode, $street_address = null, $country = null, $service_provider = null) 207 { 208 $pickup_point_limit = 5; // Default limit value for pickup point search 209 210 if (isset($this->wc_pakettikauppa_settings['pickup_points_search_limit']) && !empty($this->wc_pakettikauppa_settings['pickup_points_search_limit'])) { 211 $pickup_point_limit = intval($this->wc_pakettikauppa_settings['pickup_points_search_limit']); 212 } 213 214 $pickup_point_data = $this->wc_pakettikauppa_client->searchPickupPoints(trim($postcode), trim($street_address), trim($country), $service_provider, $pickup_point_limit); 215 if ($pickup_point_data === 'Bad request') { 216 throw new Exception(__('WC_Pakettikauppa: An error occured when searching pickup points.', 'wc-pakettikauppa')); 217 } 218 return $pickup_point_data; 219 } 220 221 /** 222 * Get all available shipping services. 223 * 224 * @return array Available shipping services 225 */ 226 public function services() 227 { 228 $services = array(); 229 230 if (WC()->customer != null) { 231 $shippingCountry = WC()->customer->get_shipping_country(); 232 } 233 234 if($shippingCountry == null || $shippingCountry == '') { 235 $shippingCountry = 'FI'; 236 } 237 238 // @TODO: File bug upstream about result being string instead of object by default 239 $transient_name = 'wc_pakettikauppa_shipping_methods'; 240 $transient_time = 86400; // 24 hours 241 $all_shipping_methods = get_transient($transient_name); 242 243 if (false === $all_shipping_methods) { 244 $all_shipping_methods = json_decode($this->wc_pakettikauppa_client->listShippingMethods()); 245 246 set_transient($transient_name, $all_shipping_methods, $transient_time); 247 } 248 249 // List all available methods as shipping options on checkout page 250 if (!empty($all_shipping_methods)) { 251 foreach ($all_shipping_methods as $shipping_method) { 252 if(in_array($shippingCountry, $shipping_method->supported_countries)) { 253 $services[$shipping_method->shipping_method_code] = sprintf('%1$s %2$s', $shipping_method->service_provider, $shipping_method->name); 254 } 255 } 256 } 257 return $services; 258 } 259 260 /** 261 * Get the title of a service by providing its code. 262 * 263 * @param int $service_code The code of a service 264 * @return string The service title matching with the provided code, or false if not found 265 */ 266 public function service_title($service_code) 267 { 268 $services = $this->services(); 269 if (isset($services[$service_code])) { 270 return $services[$service_code]; 271 } 272 273 return false; 274 } 275 276 /** 277 * Get the provider of a service by providing its code. 278 * 279 * @param int $service_code The code of a service 280 * @return string The service provider matching with the provided code, or false if not found 281 */ 282 public function service_provider($service_code) 283 { 284 $services = array(); 285 286 $transient_name = 'wc_pakettikauppa_shipping_methods'; 287 $transent_time = 86400; // 24 hours 288 $all_shipping_methods = get_transient($transient_name); 289 290 if (false === $all_shipping_methods) { 291 try { 292 $all_shipping_methods = json_decode($this->wc_pakettikauppa_client->listShippingMethods()); 293 set_transient($transient_name, $all_shipping_methods, $transient_time); 294 295 } catch (Exception $e) { 296 /* translators: %s: Error message */ 297 throw new Exception(wp_sprintf(__('WooCommerce Pakettikauppa: an error occured when accessing service providers: %s', 'wc-pakettikauppa'), $e->getMessage())); 298 } 299 } 300 301 if (!empty($all_shipping_methods)) { 302 foreach ($all_shipping_methods as $shipping_method) { 303 if (intval($service_code) === intval($shipping_method->shipping_method_code)) { 304 return $shipping_method->service_provider; 305 } 306 } 307 } 308 return false; 309 } 310 311 /** 312 * Get the status text of a shipment that matches a specified status code. 313 * 314 * @param int $status_code A status code 315 * @return string The status text matching the provided code, or unknown status if the 316 * code is unknown. 317 */ 318 public static function get_status_text($status_code) 319 { 320 $status = ''; 321 322 switch (intval($status_code)) { 323 case 13: 324 $status = __('Item is collected from sender - picked up', 'wc-pakettikauppa'); 325 break; 326 case 20: 327 $status = __('Exception', 'wc-pakettikauppa'); 328 break; 329 case 22: 330 $status = __('Item has been handed over to the recipient', 'wc-pakettikauppa'); 331 break; 332 case 31: 333 $status = __('Item is in transport', 'wc-pakettikauppa'); 334 break; 335 case 38: 336 $status = __('C.O.D payment is paid to the sender', 'wc-pakettikauppa'); 337 break; 338 case 45: 339 $status = __('Informed consignee of arrival', 'wc-pakettikauppa'); 340 break; 341 case 48: 342 $status = __('Item is loaded onto a means of transport', 'wc-pakettikauppa'); 343 break; 344 case 56: 345 $status = __('Item not delivered – delivery attempt made', 'wc-pakettikauppa'); 346 break; 347 case 68: 348 $status = __('Pre-information is received from sender', 'wc-pakettikauppa'); 349 break; 350 case 71: 351 $status = __('Item is ready for delivery transportation', 'wc-pakettikauppa'); 352 break; 353 case 77: 354 $status = __('Item is returning to the sender', 'wc-pakettikauppa'); 355 break; 356 case 91: 357 $status = __('Item is arrived to a post office', 'wc-pakettikauppa'); 358 break; 359 case 99: 360 $status = __('Outbound', 'wc-pakettikauppa'); 361 break; 362 default: 363 /* translators: %s: Status code */ 364 $status = wp_sprintf(__('Unknown status: %s', 'wc-pakettikauppa'), $status_code); 365 break; 366 } 367 368 return $status; 369 } 370 371 /** 372 * Calculate the total shipping weight of an order. 373 * 374 * @param WC_Order $order The order to calculate the weight of 375 * @return int The total weight of the order 376 */ 377 public static function order_weight($order) 378 { 379 $weight = 0; 380 381 if (count($order->get_items()) > 0) { 382 foreach ($order->get_items() as $item) { 383 if ($item['product_id'] > 0) { 384 $product = $order->get_product_from_item($item); 385 if (!$product->is_virtual()) { 386 $weight += wc_get_weight($product->get_weight() * $item['qty'], 'kg'); 387 } 388 } 389 } 390 } 391 392 return $weight; 393 } 394 395 /** 396 * Calculate the total shipping volume of an order in cubic meters. 397 * 398 * @param WC_Order $order The order to calculate the volume of 399 * @return int The total volume of the order (m^3) 400 */ 401 public static function order_volume($order) 402 { 403 $volume = 0; 404 405 if (count($order->get_items()) > 0) { 406 foreach ($order->get_items() as $item) { 407 if ($item['product_id'] > 0) { 408 $product = $order->get_product_from_item($item); 409 if (!$product->is_virtual()) { 410 // Ensure that the volume is in metres 411 $woo_dim_unit = strtolower(get_option('woocommerce_dimension_unit')); 412 switch ($woo_dim_unit) { 413 case 'mm': 414 $dim_multiplier = 0.001; 415 break; 416 case 'cm': 417 $dim_multiplier = 0.01; 418 break; 419 case 'dm': 420 $dim_multiplier = 0.1; 421 break; 422 default: 423 $dim_multiplier = 1; 424 } 425 // Calculate total volume 426 $volume += pow($dim_multiplier, 3) * $product->get_width() 427 * $product->get_height() * $product->get_length() * $item['qty']; 428 } 429 } 430 } 431 } 432 433 return $volume; 434 } 435 436 /** 437 * Get the full-length tracking url of a shipment by providing its service id and tracking code. 438 * Use tracking url provided by pakettikauppa.fi. 439 * 440 * @param int $service_id The id of the service that is used for the shipment 441 * @param int $tracking_code The tracking code of the shipment 442 * @return string The full tracking url for the order 443 */ 444 public static function tracking_url($service_id, $tracking_code) 445 { 446 $tracking_url = 'https://www.pakettikauppa.fi/seuranta/?' . $tracking_code; 447 return $tracking_url; 448 } 449 450 /** 451 * Calculate Finnish invoice reference from order ID 452 * http://tarkistusmerkit.teppovuori.fi/tarkmerk.htm#viitenumero 453 * 454 * @param int $id The id of the order to calculate the reference of 455 * @return int The reference number calculated from the id 456 */ 457 public static function calculate_reference($id) 458 { 459 $weights = array(7, 3, 1, 7, 3, 1, 7, 3, 1, 7, 3, 1, 7, 3, 1, 7, 3, 1, 7); 460 $base = str_split(strval(($id + 100))); 461 $reversed_base = array_reverse($base); 462 $sum = 0; 463 $reversed_base_length = count($reversed_base); 464 465 for ($i = 0; $i < $reversed_base_length; $i++) { 466 $coefficient = array_shift($weights); 467 $sum += $reversed_base[$i] * $coefficient; 468 } 469 470 $checksum = ($sum % 10 === 0) ? 0 : (10 - $sum % 10); 471 472 $reference = implode('', $base) . $checksum; 473 return $reference; 474 } 475 476 /** 477 * Return the default shipping service if none has been specified 478 * 479 * @TODO: Does this method really need $post or $order, as the default service should 480 * not be order-specific? 481 */ 482 public static function get_default_service($post, $order) 483 { 484 // @TODO: Maybe use an option in database so the merchant can set it in settings 485 $service = '2103'; 486 return $service; 487 } 488 489 /** 490 * Validate order details in wp-admin. Especially useful, when creating orders in wp-admin, 491 * 492 * @param WC_Order $order The order that needs its info to be validated 493 * @return True, if the details where valid, or false if not 494 */ 495 public static function validate_order_shipping_receiver($order) 496 { 497 // Check shipping info first 498 $no_shipping_name = (bool)empty($order->get_formatted_shipping_full_name()); 499 $no_shipping_address = (bool)empty($order->get_shipping_address_1()) && empty($order->get_shipping_address_2()); 500 $no_shipping_postcode = (bool)empty($order->get_shipping_postcode()); 501 $no_shipping_city = (bool)empty($order->get_shipping_city()); 502 503 if ($no_shipping_name || $no_shipping_address || $no_shipping_postcode || $no_shipping_city) { 504 return false; 505 } 506 return true; 507 } 508 509 public static function service_has_pickup_points($service_id) 510 { 511 // @TODO: Find out if the Pakettikauppa API can be used to check if the service uses 512 // pickup points instead of hard coding them here. 513 $services_with_pickup_points = array( 514 '2103', 515 '80010', 516 '90010', 517 '90080', 518 ); 519 520 if (in_array($service_id, $services_with_pickup_points, true)) { 521 return true; 522 } 523 return false; 524 } 492 525 493 526 } -
woo-pakettikauppa/trunk/includes/class-wc-pakettikauppa-shipping-method.php
r1908674 r1966477 2 2 3 3 // Prevent direct access to the script 4 if ( ! defined( 'ABSPATH' )) {5 exit;4 if (!defined('ABSPATH')) { 5 exit; 6 6 } 7 7 8 require_once plugin_dir_path( __FILE__) . '/class-wc-pakettikauppa.php';9 require_once plugin_dir_path(__FILE__ ) . '/class-wc-pakettikauppa-shipment.php';8 require_once plugin_dir_path(__FILE__) . '/class-wc-pakettikauppa.php'; 9 require_once plugin_dir_path(__FILE__) . '/class-wc-pakettikauppa-shipment.php'; 10 10 11 11 /** … … 18 18 * @author Seravo 19 19 */ 20 function wc_pakettikauppa_shipping_method_init() { 21 22 if ( ! class_exists( 'WC_Pakettikauppa_Shipping_Method' ) ) { 23 24 class WC_Pakettikauppa_Shipping_Method extends WC_Shipping_Method { 25 /** 26 * Required to access pakettikauppa client 27 */ 28 private $wc_pakettikauppa_shipment = null; 29 30 /** 31 * Default shipping fee. 32 * 33 * @var int 34 */ 35 public $fee = 5.95; 36 37 /** 38 * Constructor for Pakettikauppa shipping class 39 * 40 * @access public 41 * @return void 42 */ 43 public function __construct( $instance_id = 0 ) { 44 $this->id = 'WC_Pakettikauppa_Shipping_Method'; // ID for your shipping method. Should be unique. 45 $this->instance_id = absint( $instance_id ); 46 47 $this->method_title = 'Pakettikauppa'; // Title shown in admin 48 $this->method_description = __( 'All shipping methods with one contract. For more information visit <a href="https://www.pakettikauppa.fi/">Pakettikauppa</a>.', 'wc-pakettikauppa' ); // Description shown in admin 49 50 $this->enabled = 'yes'; 51 $this->title = 'Pakettikauppa'; 52 53 $this->init(); 54 } 55 56 public function validate_pkprice_field( $key, $value ) { 57 foreach( $value as $service_code => $service_settings ) { 58 $service_settings['price'] = wc_format_decimal( trim( stripslashes( $service_settings['price'] ) ) ); 59 $service_settings['price_free'] = wc_format_decimal( trim( stripslashes( $service_settings['price'] ) ) ); 20 function wc_pakettikauppa_shipping_method_init() 21 { 22 23 if (!class_exists('WC_Pakettikauppa_Shipping_Method')) { 24 25 class WC_Pakettikauppa_Shipping_Method extends WC_Shipping_Method 26 { 27 /** 28 * Required to access pakettikauppa client 29 */ 30 private $wc_pakettikauppa_shipment = null; 31 32 /** 33 * Default shipping fee. 34 * 35 * @var int 36 */ 37 public $fee = 5.95; 38 39 /** 40 * Constructor for Pakettikauppa shipping class 41 * 42 * @access public 43 * @return void 44 */ 45 public function __construct($instance_id = 0) 46 { 47 $this->id = 'WC_Pakettikauppa_Shipping_Method'; // ID for your shipping method. Should be unique. 48 $this->instance_id = absint($instance_id); 49 50 $this->method_title = 'Pakettikauppa'; // Title shown in admin 51 $this->method_description = __('All shipping methods with one contract. For more information visit <a href="https://www.pakettikauppa.fi/">Pakettikauppa</a>.', 'wc-pakettikauppa'); // Description shown in admin 52 53 $this->enabled = 'yes'; 54 $this->title = 'Pakettikauppa'; 55 56 $this->init(); 57 } 58 59 public function validate_pkprice_field($key, $value) 60 { 61 foreach ($value as $service_code => $service_settings) { 62 $service_settings['price'] = wc_format_decimal(trim(stripslashes($service_settings['price']))); 63 $service_settings['price_free'] = wc_format_decimal(trim(stripslashes($service_settings['price']))); 64 } 65 $values = json_encode($value); 66 67 return $values; 68 } 69 70 public function generate_pkprice_html($key, $value) 71 { 72 $field_key = $this->get_field_key($key); 73 74 if ($this->get_option($key) !== '') { 75 76 $values = $this->get_option($key); 77 if (is_string($values)) { 78 $values = json_decode($this->get_option($key), true); 79 } 80 } else { 81 $values = array(); 82 } 83 84 ob_start(); 85 ?> 86 87 <tr valign="top"> 88 <?php if (isset($value['title'])) : ?> 89 <th colspan="2"><label><?php esc_html($value['title']); ?></label></th> 90 <?php endif; ?> 91 </tr> 92 <tr> 93 <td colspan="2"> 94 <table> 95 <thead> 96 <tr> 97 <th><?php esc_attr_e('Service', 'wc-pakettikauppa'); ?></th> 98 <th style="width: 60px;"><?php esc_attr_e('Active', 'wc-pakettikauppa'); ?></th> 99 <th style="text-align: center;"><?php esc_attr_e('Price', 'wc-pakettikauppa'); ?></th> 100 <th style="text-align: center;"><?php esc_attr_e('Free shipping tier', 'wc-pakettikauppa'); ?></th> 101 </tr> 102 </thead> 103 <tbody> 104 <?php if (isset($value['options'])) : ?> 105 <?php foreach ($value['options'] as $service_code => $service_name) : ?> 106 <?php if (!isset($values[$service_code])) : ?> 107 <?php $values[$service_code]['active'] = false; ?> 108 <?php $values[$service_code]['price'] = $this->fee; ?> 109 <?php $values[$service_code]['price_free'] = '0'; ?> 110 <?php endif; ?> 111 112 <tr valign="top"> 113 <th scope="row" class="titledesc"> 114 <label><?php echo esc_html($service_name); ?></label> 115 </th> 116 <td> 117 <input type="hidden" 118 name="<?php echo esc_html($field_key) . '[' . esc_html($service_code) . '][active]'; ?>" 119 value="no"> 120 <input type="checkbox" 121 name="<?php echo esc_html($field_key) . '[' . esc_html($service_code) . '][active]'; ?>" 122 value="yes" <?php echo $values[$service_code]['active'] === 'yes' ? 'checked' : ''; ?>> 123 </td> 124 <td> 125 <input type="number" 126 name="<?php echo esc_html($field_key) . '[' . esc_html($service_code) . '][price]'; ?>" 127 step="0.01" 128 value="<?php echo esc_html($values[$service_code]['price']); ?>"> 129 </td> 130 <td> 131 <input type="number" 132 name="<?php echo esc_html($field_key) . '[' . esc_html($service_code) . '][price_free]'; ?>" 133 step="0.01" 134 value="<?php echo esc_html($values[$service_code]['price_free']); ?>"> 135 </td> 136 </tr> 137 <?php endforeach; ?> 138 <?php endif; ?> 139 140 </tbody> 141 </table> 142 </td> 143 </tr> 144 145 <?php 146 147 $html = ob_get_contents(); 148 ob_end_clean(); 149 150 return $html; 151 } 152 153 /** 154 * Initialize Pakettikauppa shipping 155 */ 156 public function init() 157 { 158 // Make Pakettikauppa API accessible via WC_Pakettikauppa_Shipment 159 $this->wc_pakettikauppa_shipment = new WC_Pakettikauppa_Shipment(); 160 $this->wc_pakettikauppa_shipment->load(); 161 162 // Save settings in admin if you have any defined 163 add_action('woocommerce_update_options_shipping_' . $this->id, array($this, 'process_admin_options')); 164 165 // Load the settings. 166 $this->init_form_fields(); 167 $this->init_settings(); 168 169 } 170 171 /** 172 * Init form fields. 173 */ 174 public function init_form_fields() 175 { 176 $this->form_fields = array( 177 'mode' => array( 178 'title' => __('Mode', 'wc-pakettikauppa'), 179 'type' => 'select', 180 'default' => 'test', 181 'options' => array( 182 'test' => __('Test environment', 'wc-pakettikauppa'), 183 'production' => __('Production environment', 'wc-pakettikauppa'), 184 ), 185 ), 186 187 'account_number' => array( 188 'title' => __('API key', 'wc-pakettikauppa'), 189 'desc' => __('API key provided by Pakettikauppa', 'wc-pakettikauppa'), 190 'type' => 'text', 191 'default' => '', 192 'desc_tip' => true, 193 ), 194 195 'secret_key' => array( 196 'title' => __('API secret', 'wc-pakettikauppa'), 197 'desc' => __('API Secret provided by Pakettikauppa', 'wc-pakettikauppa'), 198 'type' => 'text', 199 'default' => '', 200 'desc_tip' => true, 201 ), 202 203 /* Start new section */ 204 array( 205 'title' => __('Shipping settings', 'wc-pakettikauppa'), 206 'type' => 'title', 207 ), 208 209 'active_shipping_options' => array( 210 'type' => 'pkprice', 211 'options' => $this->wc_pakettikauppa_shipment->services(), 212 ), 213 214 'add_tracking_to_email' => array( 215 'title' => __('Add tracking link to the order completed email', 'wc-pakettikauppa'), 216 'type' => 'checkbox', 217 'default' => 'no', 218 ), 219 220 'pickup_points_search_limit' => array( 221 'title' => __('Pickup point search limit', 'wc-pakettikauppa'), 222 'type' => 'number', 223 'default' => 5, 224 'description' => __('Limit the amount of nearest pickup points shown.', 'wc-pakettikauppa'), 225 'desc_tip' => true, 226 ), 227 228 /* Start new section */ 229 array( 230 'title' => __('Store owner information', 'wc-pakettikauppa'), 231 'type' => 'title', 232 ), 233 234 'sender_name' => array( 235 'title' => __('Sender name', 'wc-pakettikauppa'), 236 'type' => 'text', 237 'default' => '', 238 ), 239 240 'sender_address' => array( 241 'title' => __('Sender address', 'wc-pakettikauppa'), 242 'type' => 'text', 243 'default' => '', 244 ), 245 246 'sender_postal_code' => array( 247 'title' => __('Sender postal code', 'wc-pakettikauppa'), 248 'type' => 'text', 249 'default' => '', 250 ), 251 252 'sender_city' => array( 253 'title' => __('Sender city', 'wc-pakettikauppa'), 254 'type' => 'text', 255 'default' => '', 256 ), 257 258 'cod_iban' => array( 259 'title' => __('Bank account number for Cash on Delivery (IBAN)', 'wc-pakettikauppa'), 260 'type' => 'text', 261 'default' => '', 262 ), 263 264 'cod_bic' => array( 265 'title' => __('BIC code for Cash on Delivery', 'wc-pakettikauppa'), 266 'type' => 'text', 267 'default' => '', 268 ), 269 270 'info_code' => array( 271 'title' => __('Info-code for shipments'), 272 'type' => 'text', 273 'default' => '', 274 ), 275 ); 276 } 277 278 /** 279 * Mostly copy pasted from WooCommerce: woocommerce/includes/abstracts/abstract-wc-shipping-method.php 280 * protected function get_taxes_per_item( $costs ) 281 * 282 * @param $shippingCost 283 * @return array 284 */ 285 private function calculate_shipping_tax($shippingCost) 286 { 287 $taxes = array(); 288 289 $taxesTotal = 0; 290 $cart_total = WC()->cart->get_cart_contents_total(); 291 292 $cart = WC()->cart->get_cart(); 293 294 $costs = array(); 295 296 foreach ($cart as $item) { 297 $costs[$item['key']] = $shippingCost * $item['line_total'] / $cart_total; 298 } 299 300 // If we have an array of costs we can look up each items tax class and add tax accordingly. 301 if (is_array($costs)) { 302 $cart = WC()->cart->get_cart(); 303 304 foreach ($costs as $cost_key => $amount) { 305 if (!isset($cart[$cost_key])) { 306 continue; 307 } 308 309 $taxObj = WC_Tax::get_shipping_tax_rates($cart[$cost_key]['data']->get_tax_class()); 310 $item_taxes = WC_Tax::calc_shipping_tax($amount, $taxObj); 311 312 // Sum the item taxes. 313 foreach (array_keys($taxes + $item_taxes) as $key) { 314 $taxes[$key] = round((isset($item_taxes[$key]) ? $item_taxes[$key] : 0) + (isset($taxes[$key]) ? $taxes[$key] : 0), 2); 315 } 316 } 317 } 318 319 foreach ($taxes as $_tax) { 320 $taxesTotal += $_tax; 321 } 322 return array('total' => $taxesTotal, 'taxes' => $taxes); 323 } 324 325 /** 326 * Called to calculate shipping rates for this method. Rates can be added using the add_rate() method. 327 * Return only active shipping methods. 328 * 329 * @uses WC_Shipping_Method::add_rate() 330 * 331 * @param array $package Shipping package. 332 */ 333 public function calculate_shipping($package = array()) 334 { 335 $cart_total = WC()->cart->cart_contents_total; 336 337 $shipping_settings = json_decode($this->get_option('active_shipping_options'), true); 338 339 foreach ($shipping_settings as $service_code => $service_settings) { 340 if ($service_settings['active'] === 'yes') { 341 $shipping_cost = $service_settings['price']; 342 343 if ($service_settings['price_free'] < $cart_total && $service_settings['price_free'] > 0) { 344 $shipping_cost = 0; 345 } 346 347 $taxes = $this->calculate_shipping_tax($shipping_cost); 348 349 $shipping_cost = $shipping_cost - $taxes['total']; 350 351 $this->add_rate( 352 array( 353 'id' => $this->id . ':' . $service_code, 354 'label' => $this->wc_pakettikauppa_shipment->service_title($service_code), 355 'cost' => (string)$shipping_cost, 356 'taxes' => $taxes['taxes'], 357 ) 358 ); 359 } 360 } 361 362 } 363 60 364 } 61 $values = json_encode( $value );62 63 return $values;64 }65 66 public function generate_pkprice_html( $key, $value ) {67 $field_key = $this->get_field_key( $key );68 69 if ( $this->get_option( $key ) !== '' ) {70 71 $values = $this->get_option( $key );72 if ( is_string( $values ) ) {73 $values = json_decode( $this->get_option( $key ), true );74 }75 } else {76 $values = array();77 }78 79 ob_start();80 ?>81 82 <tr valign="top">83 <?php if ( isset( $value['title'] ) ) : ?>84 <th colspan="2"><label><?php esc_html( $value['title'] ); ?></label></th>85 <?php endif; ?>86 </tr>87 <tr>88 <td colspan="2">89 <table>90 <thead>91 <tr>92 <th><?php esc_attr_e( 'Service', 'wc-pakettikauppa' ); ?></th>93 <th style="width: 60px;"><?php esc_attr_e( 'Active', 'wc-pakettikauppa' ); ?></th>94 <th style="text-align: center;"><?php esc_attr_e( 'Price', 'wc-pakettikauppa' ); ?></th>95 <th style="text-align: center;"><?php esc_attr_e( 'Free shipping tier', 'wc-pakettikauppa' ); ?></th>96 </tr>97 </thead>98 <tbody>99 <?php if ( isset( $value['options'] ) ) : ?>100 <?php foreach( $value['options'] as $service_code => $service_name ) : ?>101 <?php if ( ! isset( $values[$service_code] ) ) : ?>102 <?php $values[$service_code]['active'] = false; ?>103 <?php $values[$service_code]['price'] = $this->fee; ?>104 <?php $values[$service_code]['price_free'] = '0'; ?>105 <?php endif; ?>106 107 <tr valign="top">108 <th scope="row" class="titledesc">109 <label><?php echo esc_html( $service_name ); ?></label>110 </th>111 <td>112 <input type="hidden" name="<?php echo esc_html( $field_key ) . '[' . esc_html( $service_code ) . '][active]'; ?>" value="no">113 <input type="checkbox" name="<?php echo esc_html( $field_key ) . '[' . esc_html( $service_code ) . '][active]'; ?>" value="yes" <?php echo $values[$service_code]['active'] === 'yes' ? 'checked': ''; ?>>114 </td>115 <td>116 <input type="number" name="<?php echo esc_html( $field_key ) . '[' . esc_html( $service_code ) . '][price]'; ?>" step="0.01" value="<?php echo esc_html( $values[$service_code]['price']); ?>">117 </td>118 <td>119 <input type="number" name="<?php echo esc_html( $field_key ) . '[' . esc_html( $service_code ) . '][price_free]'; ?>" step="0.01" value="<?php echo esc_html( $values[$service_code]['price_free']); ?>">120 </td>121 </tr>122 <?php endforeach; ?>123 <?php endif; ?>124 125 </tbody>126 </table>127 </td>128 </tr>129 130 <?php131 132 $html = ob_get_contents();133 ob_end_clean();134 135 return $html;136 }137 138 /**139 * Initialize Pakettikauppa shipping140 */141 public function init() {142 // Make Pakettikauppa API accessible via WC_Pakettikauppa_Shipment143 $this->wc_pakettikauppa_shipment = new WC_Pakettikauppa_Shipment();144 $this->wc_pakettikauppa_shipment->load();145 146 // Save settings in admin if you have any defined147 add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );148 149 // Load the settings.150 $this->init_form_fields();151 $this->init_settings();152 153 }154 155 /**156 * Init form fields.157 */158 public function init_form_fields() {159 $this->form_fields = array(160 'mode' => array(161 'title' => __( 'Mode', 'wc-pakettikauppa' ),162 'type' => 'select',163 'default' => 'test',164 'options' => array(165 'test' => __( 'Test environment', 'wc-pakettikauppa' ),166 'production' => __( 'Production environment', 'wc-pakettikauppa' ),167 ),168 ),169 170 'account_number' => array(171 'title' => __( 'API key', 'wc-pakettikauppa' ),172 'desc' => __( 'API key provided by Pakettikauppa', 'wc-pakettikauppa' ),173 'type' => 'text',174 'default' => '',175 'desc_tip' => true,176 ),177 178 'secret_key' => array(179 'title' => __( 'API secret', 'wc-pakettikauppa' ),180 'desc' => __( 'API Secret provided by Pakettikauppa', 'wc-pakettikauppa' ),181 'type' => 'text',182 'default' => '',183 'desc_tip' => true,184 ),185 186 /* Start new section */187 array(188 'title' => __( 'Shipping settings', 'wc-pakettikauppa' ),189 'type' => 'title',190 ),191 192 'active_shipping_options' => array(193 'type' => 'pkprice',194 'options' => $this->wc_pakettikauppa_shipment->services(),195 ),196 197 'add_tracking_to_email' => array(198 'title' => __( 'Add tracking link to the order completed email', 'wc-pakettikauppa' ),199 'type' => 'checkbox',200 'default' => 'no',201 ),202 203 'pickup_points_search_limit' => array(204 'title' => __( 'Pickup point search limit', 'wc-pakettikauppa' ),205 'type' => 'number',206 'default' => 5,207 'description' => __( 'Limit the amount of nearest pickup points shown.', 'wc-pakettikauppa' ),208 'desc_tip' => true,209 ),210 211 /* Start new section */212 array(213 'title' => __( 'Store owner information', 'wc-pakettikauppa' ),214 'type' => 'title',215 ),216 217 'sender_name' => array(218 'title' => __( 'Sender name', 'wc-pakettikauppa' ),219 'type' => 'text',220 'default' => '',221 ),222 223 'sender_address' => array(224 'title' => __( 'Sender address', 'wc-pakettikauppa' ),225 'type' => 'text',226 'default' => '',227 ),228 229 'sender_postal_code' => array(230 'title' => __( 'Sender postal code', 'wc-pakettikauppa' ),231 'type' => 'text',232 'default' => '',233 ),234 235 'sender_city' => array(236 'title' => __( 'Sender city', 'wc-pakettikauppa' ),237 'type' => 'text',238 'default' => '',239 ),240 241 'cod_iban' => array(242 'title' => __( 'Bank account number for Cash on Delivery (IBAN)', 'wc-pakettikauppa' ),243 'type' => 'text',244 'default' => '',245 ),246 247 'cod_bic' => array(248 'title' => __( 'BIC code for Cash on Delivery', 'wc-pakettikauppa' ),249 'type' => 'text',250 'default' => '',251 ),252 253 );254 }255 256 /**257 * Called to calculate shipping rates for this method. Rates can be added using the add_rate() method.258 * Return only active shipping methods.259 *260 * @uses WC_Shipping_Method::add_rate()261 *262 * @param array $package Shipping package.263 */264 public function calculate_shipping( $package = array() ) {265 global $woocommerce;266 267 $cart_total = $woocommerce->cart->cart_contents_total;268 269 $shipping_settings = json_decode( $this->get_option( 'active_shipping_options' ), true );270 271 foreach( $shipping_settings as $service_code => $service_settings ) {272 273 if ( $service_settings['active'] === 'yes' ) {274 275 $shipping_cost = $service_settings['price'];276 277 if ( $service_settings['price_free'] < $cart_total && $service_settings['price_free'] > 0 ) {278 $shipping_cost = 0;279 }280 281 $this->add_rate(282 array(283 'id' => $this->id .':'. $service_code,284 'label' => $this->wc_pakettikauppa_shipment->service_title( $service_code ),285 'cost' => (string) $shipping_cost286 )287 );288 }289 }290 291 }292 293 365 } 294 }295 366 } 296 367 297 add_action( 'woocommerce_shipping_init', 'wc_pakettikauppa_shipping_method_init' ); 298 299 function add_wc_pakettikauppa_shipping_method( $methods ) { 300 301 $methods[] = 'WC_Pakettikauppa_Shipping_Method'; 302 return $methods; 368 add_action('woocommerce_shipping_init', 'wc_pakettikauppa_shipping_method_init'); 369 370 function add_wc_pakettikauppa_shipping_method($methods) 371 { 372 373 $methods[] = 'WC_Pakettikauppa_Shipping_Method'; 374 return $methods; 303 375 304 376 } 305 377 306 add_filter( 'woocommerce_shipping_methods', 'add_wc_pakettikauppa_shipping_method');378 add_filter('woocommerce_shipping_methods', 'add_wc_pakettikauppa_shipping_method'); -
woo-pakettikauppa/trunk/includes/class-wc-pakettikauppa.php
r1833640 r1966477 2 2 3 3 // Prevent direct access to this script 4 if ( ! defined( 'ABSPATH' )) {5 exit;4 if (!defined('ABSPATH')) { 5 exit; 6 6 } 7 7 … … 18 18 * @author Seravo 19 19 */ 20 class WC_Pakettikauppa { 21 private $wc_pakettikauppa_shipment = null; 22 private $errors = array(); 23 24 public function __construct() { 25 $this->id = 'wc_pakettikauppa'; 26 } 27 28 public function load() { 29 add_action( 'enqueue_scripts', array( $this, 'enqueue_scripts' ) ); 30 add_action( 'woocommerce_review_order_after_shipping', array( $this, 'pickup_point_field_html' ) ); 31 add_action( 'woocommerce_order_details_after_order_table', array( $this, 'display_order_data' ) ); 32 add_action( 'woocommerce_checkout_update_order_meta', array( $this, 'update_order_meta_pickup_point_field' ) ); 33 add_action('woocommerce_checkout_process', array( $this, 'validate_checkout_pickup_point' ) ); 34 35 try { 36 $this->wc_pakettikauppa_shipment = new WC_Pakettikauppa_Shipment(); 37 $this->wc_pakettikauppa_shipment->load(); 38 39 } catch ( Exception $e ) { 40 $this->add_error( $e->getMessage() ); 41 $this->display_error(); 42 } 43 } 44 45 /** 46 * Add an error with a specified error message. 47 * 48 * @param string $message A message containing details about the error. 49 */ 50 public function add_error( $message ) { 51 if ( ! empty( $message ) ) { 52 array_push( $this->errors, $message ); 53 error_log( $message ); 54 } 55 } 56 57 /** 58 * Display error in woocommerce 59 */ 60 public function display_error() { 61 wc_add_notice( __( 'An error occured. Please try again later.', 'wc-pakettikauppa' ), 'error' ); 62 } 63 64 /** 65 * Enqueue frontend-specific styles and scripts. 66 */ 67 public function enqueue_scripts() { 68 wp_enqueue_style( 'wc_pakettikauppa', plugin_dir_url( __FILE__ ) . '../assets/css/wc-pakettikauppa.css' ); 69 wp_enqueue_script( 'wc_pakettikauppa_js', plugin_dir_url( __FILE__ ) . '../assets/js/wc-pakettikauppa.js', array( 'jquery' ) ); 70 } 71 72 /** 73 * Update the order meta with pakettikauppa_pickup_point field value 74 * Example value from checkout page: "DB Schenker: R-KIOSKI TRE AMURI (#6681)" 75 * 76 * @param int $order_id The id of the order to update 77 */ 78 public function update_order_meta_pickup_point_field( $order_id ) { 79 if ( ! empty( $_POST['pakettikauppa_pickup_point'] ) ) { 80 update_post_meta( $order_id, '_pakettikauppa_pickup_point', sanitize_text_field( $_POST['pakettikauppa_pickup_point'] ) ); 81 // Find string like '(#6681)' 82 preg_match( '/\(#[0-9]+\)/', $_POST['pakettikauppa_pickup_point'], $matches); 83 // Cut the number out from a string of the form '(#6681)' 84 $pakettikauppa_pickup_point_id = intval( substr($matches[0], 2, -1) ); 85 update_post_meta( $order_id, '_pakettikauppa_pickup_point_id', $pakettikauppa_pickup_point_id ); 86 } 87 } 88 89 /* 90 * Customize the layout of the checkout screen so that there is a section 91 * where the pickup point can be defined. Don't use the woocommerce_checkout_fields 92 * filter, it only lists fields without values, and we need to know the postcode. 93 * Also the woocommerce_checkout_fields has separate billing and shipping address 94 * listings, when we want to have only one single pickup point per order. 95 */ 96 public function pickup_point_field_html() { 97 $shipping_method_name = explode(':', WC()->session->get( 'chosen_shipping_methods' )[0])[0]; 98 $shipping_method_id = explode(':', WC()->session->get( 'chosen_shipping_methods' )[0])[1]; 99 $shipping_method_provider = $this->wc_pakettikauppa_shipment->service_provider($shipping_method_id); 100 101 // Bail out if the shipping method is not one of the pickup point services 102 if ( ! WC_Pakettikauppa_Shipment::service_has_pickup_points( $shipping_method_id ) ) { 103 return; 104 } 105 106 $pickup_point_data = ''; 107 $shipping_postcode = WC()->customer->get_shipping_postcode(); 108 $shipping_address = WC()->customer->get_shipping_address(); 109 $shipping_country = WC()->customer->get_shipping_country(); 110 111 if ( empty( $shipping_country ) ) { 112 $shipping_country = 'FI'; 113 } 114 115 try { 116 $pickup_point_data = $this->wc_pakettikauppa_shipment->get_pickup_points( $shipping_postcode, $shipping_address, $shipping_country, $shipping_method_provider ); 117 118 } catch ( Exception $e ) { 119 $this->add_error( $e->getMessage() ); 120 $this->display_error(); 121 return; 122 } 123 124 $pickup_points = json_decode( $pickup_point_data ); 125 $options_array = array( '' => '- ' . __('Select a pickup point', 'wc-pakettikauppa') . ' -' ); 126 127 foreach ( $pickup_points as $key => $value ) { 128 $pickup_point_key = $value->provider . ': ' . $value->name . ' (#' . $value->pickup_point_id . ')'; 129 $pickup_point_value = $value->provider . ': ' . $value->name . ' (' . $value->street_address . ')'; 130 $options_array[ $pickup_point_key ] = $pickup_point_value; 131 } 132 133 echo ' 134 <tr class="shipping-pickup-point"> 135 <th>' . esc_attr__('Pickup point', 'wc-pakettikauppa') . '</th> 136 <td data-title="' . esc_attr__('Pickup point', 'wc-pakettikauppa') . '">'; 137 138 echo '<p>'; 139 // Return if the customer has not yet chosen a postcode 140 if ( empty( $shipping_postcode ) ) { 141 esc_attr_e( 'Insert your shipping details to view nearby pickup points', 'wc-pakettikauppa' ); 142 return; 143 } 144 printf( 145 /* translators: %s: Postcode */ 146 esc_html__( 'Choose one of the pickup points close to your postcode %s below:', 'wc-pakettikauppa' ), 147 '<span class="shipping_postcode_for_pickup">' . esc_attr( $shipping_postcode ) . '</span>' 148 ); 149 echo '</p>'; 150 151 woocommerce_form_field( 152 'pakettikauppa_pickup_point', array( 153 'clear' => true, 154 'type' => 'select', 155 'custom_attributes' => array( 'style' => 'max-width:18em;' ), 156 'options' => $options_array, 157 ), 158 null 159 ); 160 161 echo '</div>'; 162 163 } 164 165 /** 166 * Display pickup point to customer after order. 167 * 168 * @param WC_Order $order the order that was placed 169 */ 170 public function display_order_data( $order ) { 171 $pickup_point = $order->get_meta('_pakettikauppa_pickup_point'); 172 173 if ( ! empty( $pickup_point ) ) { 174 echo ' 175 <h2>' . esc_attr__('Pickup point', 'wc-pakettikauppa' ) . '</h2> 176 <p>' . esc_attr( $pickup_point ) . '</p>'; 177 } 178 } 179 180 public function validate_checkout_pickup_point() { 181 $shipping_method_id = explode(':', WC()->session->get( 'chosen_shipping_methods' )[0])[1]; 182 // Check if the service has a pickup point 183 try { 184 if ( $this->wc_pakettikauppa_shipment->service_has_pickup_points( $shipping_method_id ) && empty( $_POST['pakettikauppa_pickup_point'] ) ) { 185 wc_add_notice( __( 'Please choose a pickup point.', 'wc-pakettikauppa' ), 'error' ); 186 } 187 } catch ( Exception $e ) { 188 $this->add_error( $e->getMessage() ); 189 $this->display_error(); 190 return; 191 } 192 } 20 class WC_Pakettikauppa 21 { 22 private $wc_pakettikauppa_shipment = null; 23 private $errors = array(); 24 25 public function __construct() 26 { 27 $this->id = 'wc_pakettikauppa'; 28 } 29 30 public function load() 31 { 32 add_action('enqueue_scripts', array($this, 'enqueue_scripts')); 33 add_action('woocommerce_review_order_after_shipping', array($this, 'pickup_point_field_html')); 34 add_action('woocommerce_order_details_after_order_table', array($this, 'display_order_data')); 35 add_action('woocommerce_checkout_update_order_meta', array($this, 'update_order_meta_pickup_point_field')); 36 add_action('woocommerce_checkout_process', array($this, 'validate_checkout_pickup_point')); 37 38 try { 39 $this->wc_pakettikauppa_shipment = new WC_Pakettikauppa_Shipment(); 40 $this->wc_pakettikauppa_shipment->load(); 41 42 } catch (Exception $e) { 43 $this->add_error($e->getMessage()); 44 $this->display_error(); 45 } 46 } 47 48 /** 49 * Add an error with a specified error message. 50 * 51 * @param string $message A message containing details about the error. 52 */ 53 public function add_error($message) 54 { 55 if (!empty($message)) { 56 array_push($this->errors, $message); 57 error_log($message); 58 } 59 } 60 61 /** 62 * Display error in woocommerce 63 */ 64 public function display_error() 65 { 66 wc_add_notice(__('An error occured. Please try again later.', 'wc-pakettikauppa'), 'error'); 67 } 68 69 /** 70 * Enqueue frontend-specific styles and scripts. 71 */ 72 public function enqueue_scripts() 73 { 74 wp_enqueue_style('wc_pakettikauppa', plugin_dir_url(__FILE__) . '../assets/css/wc-pakettikauppa.css'); 75 wp_enqueue_script('wc_pakettikauppa_js', plugin_dir_url(__FILE__) . '../assets/js/wc-pakettikauppa.js', array('jquery')); 76 } 77 78 /** 79 * Update the order meta with pakettikauppa_pickup_point field value 80 * Example value from checkout page: "DB Schenker: R-KIOSKI TRE AMURI (#6681)" 81 * 82 * @param int $order_id The id of the order to update 83 */ 84 public function update_order_meta_pickup_point_field($order_id) 85 { 86 if (!empty($_POST['pakettikauppa_pickup_point'])) { 87 update_post_meta($order_id, '_pakettikauppa_pickup_point', sanitize_text_field($_POST['pakettikauppa_pickup_point'])); 88 // Find string like '(#6681)' 89 preg_match('/\(#[0-9]+\)/', $_POST['pakettikauppa_pickup_point'], $matches); 90 // Cut the number out from a string of the form '(#6681)' 91 $pakettikauppa_pickup_point_id = intval(substr($matches[0], 2, -1)); 92 update_post_meta($order_id, '_pakettikauppa_pickup_point_id', $pakettikauppa_pickup_point_id); 93 } 94 } 95 96 /* 97 * Customize the layout of the checkout screen so that there is a section 98 * where the pickup point can be defined. Don't use the woocommerce_checkout_fields 99 * filter, it only lists fields without values, and we need to know the postcode. 100 * Also the woocommerce_checkout_fields has separate billing and shipping address 101 * listings, when we want to have only one single pickup point per order. 102 */ 103 public function pickup_point_field_html() 104 { 105 $shipping_method_name = explode(':', WC()->session->get('chosen_shipping_methods')[0])[0]; 106 $shipping_method_id = explode(':', WC()->session->get('chosen_shipping_methods')[0])[1]; 107 $shipping_method_provider = $this->wc_pakettikauppa_shipment->service_provider($shipping_method_id); 108 109 // Bail out if the shipping method is not one of the pickup point services 110 if (!WC_Pakettikauppa_Shipment::service_has_pickup_points($shipping_method_id)) { 111 return; 112 } 113 114 $pickup_point_data = ''; 115 $shipping_postcode = WC()->customer->get_shipping_postcode(); 116 $shipping_address = WC()->customer->get_shipping_address(); 117 $shipping_country = WC()->customer->get_shipping_country(); 118 119 if (empty($shipping_country)) { 120 $shipping_country = 'FI'; 121 } 122 123 try { 124 $pickup_point_data = $this->wc_pakettikauppa_shipment->get_pickup_points($shipping_postcode, $shipping_address, $shipping_country, $shipping_method_provider); 125 126 } catch (Exception $e) { 127 $this->add_error($e->getMessage()); 128 $this->display_error(); 129 return; 130 } 131 132 $pickup_points = json_decode($pickup_point_data); 133 $options_array = array('' => '- ' . __('Select a pickup point', 'wc-pakettikauppa') . ' -'); 134 135 foreach ($pickup_points as $key => $value) { 136 $pickup_point_key = $value->provider . ': ' . $value->name . ' (#' . $value->pickup_point_id . ')'; 137 $pickup_point_value = $value->provider . ': ' . $value->name . ' (' . $value->street_address . ')'; 138 $options_array[$pickup_point_key] = $pickup_point_value; 139 } 140 141 echo '<tr class="shipping-pickup-point">'; 142 echo '<th>' . esc_attr__('Pickup point', 'wc-pakettikauppa') . '</th>'; 143 echo '<td data-title="' . esc_attr__('Pickup point', 'wc-pakettikauppa') . '">'; 144 145 // Return if the customer has not yet chosen a postcode 146 if (empty($shipping_postcode)) { 147 echo '<p>'; 148 esc_attr_e('Insert your shipping details to view nearby pickup points', 'wc-pakettikauppa'); 149 echo '</p>'; 150 } else { 151 printf( 152 /* translators: %s: Postcode */ 153 esc_html__('Choose one of the pickup points close to your postcode %s below:', 'wc-pakettikauppa'), 154 '<span class="shipping_postcode_for_pickup">' . esc_attr($shipping_postcode) . '</span>' 155 ); 156 157 woocommerce_form_field( 158 'pakettikauppa_pickup_point', 159 array( 160 'clear' => true, 161 'type' => 'select', 162 'custom_attributes' => array('style' => 'word-wrap: normal;'), 163 'options' => $options_array, 164 ), 165 null 166 ); 167 } 168 169 echo '</td></tr>'; 170 } 171 172 /** 173 * Display pickup point to customer after order. 174 * 175 * @param WC_Order $order the order that was placed 176 */ 177 public function display_order_data($order) 178 { 179 $pickup_point = $order->get_meta('_pakettikauppa_pickup_point'); 180 181 if (!empty($pickup_point)) { 182 echo ' 183 <h2>' . esc_attr__('Pickup point', 'wc-pakettikauppa') . '</h2> 184 <p>' . esc_attr($pickup_point) . '</p>'; 185 } 186 } 187 188 public function validate_checkout_pickup_point() 189 { 190 $shipping_method_id = explode(':', WC()->session->get('chosen_shipping_methods')[0])[1]; 191 // Check if the service has a pickup point 192 try { 193 if ($this->wc_pakettikauppa_shipment->service_has_pickup_points($shipping_method_id) && empty($_POST['pakettikauppa_pickup_point'])) { 194 wc_add_notice(__('Please choose a pickup point.', 'wc-pakettikauppa'), 'error'); 195 } 196 } catch (Exception $e) { 197 $this->add_error($e->getMessage()); 198 $this->display_error(); 199 return; 200 } 201 } 193 202 194 203 } -
woo-pakettikauppa/trunk/readme.txt
r1908674 r1966477 16 16 [Pakettikauppa](https://www.pakettikauppa.fi/) is a shipping service provider in Finland. This plugin integrates their service into WooCommerce. To start shipping, all your WooCommerce needs is this plugin and a merchant ID of your account registered with Pakettikauppa. 17 17 18 > *Note!* If you already have shipping contracts with Posti, Matkahuolto or DB Schenkerwith reduced prices, you can contact the customer support of Pakettikauppa to get those contracts via Pakettikauppa so you can use the WooCommerce Pakettikauppa plugin with your current shipping contracts.18 > *Note!* If you already have shipping contracts with Posti, Matkahuolto, DB Schenker or GLS with reduced prices, you can contact the customer support of Pakettikauppa to get those contracts via Pakettikauppa so you can use the WooCommerce Pakettikauppa plugin with your current shipping contracts. 19 19 20 20 # Features -
woo-pakettikauppa/trunk/vendor/pakettikauppa/api-library/src/Pakettikauppa/Client.php
r1759098 r1966477 8 8 private $secret; 9 9 private $base_uri; 10 private $user_agent = 'pk-client-lib/0.1'; 10 private $user_agent = 'pk-client-lib/0.2'; 11 private $comment = null; 12 private $response = null; 11 13 12 14 /** … … 34 36 $this->api_key = $params['api_key']; 35 37 $this->secret = $params['secret']; 36 $this->base_uri = 'https://api.pakettikauppa.fi'; 37 } 38 } 39 38 39 if(isset($params['base_uri'])) { 40 $this->base_uri = $params['base_uri']; 41 } else { 42 $this->base_uri = 'https://api.pakettikauppa.fi'; 43 } 44 } 45 } 46 47 /** 48 * Sets comment for the request. You can set there information for Pakettikauppa. Like 49 * "Generated from Foobar platform" 50 * 51 * @param string $comment 52 */ 53 public function setComment($comment) { 54 $this->comment = $comment; 55 } 40 56 /** 41 57 * Posts shipment data to Pakettikauppa, if request was successful … … 54 70 $shipment_xml->{"ROUTING"}->{"Routing.Id"} = $id; 55 71 $shipment_xml->{"ROUTING"}->{"Routing.Key"} = md5("{$this->api_key}{$id}{$this->secret}"); 72 if($this->comment != null) { 73 $shipment_xml->{"ROUTING"}->{"Routing.Comment"} = $this->comment; 74 } 56 75 57 76 $response = $this->doPost('/prinetti/create-shipment', null, $shipment_xml->asXML()); … … 63 82 } 64 83 84 $this->response = $response_xml; 85 65 86 if($response_xml->{'response.status'} != 0) { 66 87 throw new \Exception("Error: {$response_xml->{'response.status'}}, {$response_xml->{'response.message'}}"); … … 74 95 75 96 /** 97 * Returns latest response as XML 98 * 99 * @return \SimpleXMLElement 100 */ 101 public function getResponse() { 102 return $this->response; 103 } 104 /**a 76 105 * Fetches the shipping label pdf for a given Shipment and 77 106 * saves it as base64 encoded string to $pdf parameter on the Shipment. … … 105 134 } 106 135 136 $this->response = $response_xml; 137 107 138 if($response_xml->{'response.status'} != 0) { 108 139 throw new \Exception("Error: {$response_xml->{'response.status'}}, {$response_xml->{'response.message'}}"); … … 115 146 116 147 /** 148 * Fetches the shipping labels in one pdf for a given tracking_codes and 149 * saves it as base64 encoded string inside XML. 150 * 151 * @param array $trackingCodes 152 * @return xml 153 * @throws \Exception 154 */ 155 public function fetchShippingLabels($trackingCodes) 156 { 157 $id = str_replace('.', '', microtime(true)); 158 $xml = new \SimpleXMLElement('<eChannel/>'); 159 160 $routing = $xml->addChild('ROUTING'); 161 $routing->addChild('Routing.Account', $this->api_key); 162 $routing->addChild('Routing.Id', $id); 163 $routing->addChild('Routing.Key', md5("{$this->api_key}{$id}{$this->secret}")); 164 165 $label = $xml->addChild('PrintLabel'); 166 $label['responseFormat'] = 'File'; 167 168 foreach($trackingCodes as $trackingCode) { 169 $label->addChild('TrackingCode', $trackingCode); 170 } 171 172 $response = $this->doPost('/prinetti/get-shipping-label', null, $xml->asXML()); 173 174 $response_xml = @simplexml_load_string($response); 175 176 if(!$response_xml) { 177 throw new \Exception("Failed to load response xml"); 178 } 179 180 $this->response = $response_xml; 181 182 if($response_xml->{'response.status'} != 0) { 183 throw new \Exception("Error: {$response_xml->{'response.status'}}, {$response_xml->{'response.message'}}"); 184 } 185 186 return $response_xml; 187 } 188 189 /** 117 190 * @param $tracking_code 118 * @return \Psr\Http\Message\StreamInterface191 * @return mixed 119 192 */ 120 193 public function getShipmentStatus($tracking_code) … … 124 197 125 198 /** 126 * @return \Psr\Http\Message\StreamInterface199 * @return mixed 127 200 */ 128 201 public function listAdditionalServices() … … 132 205 133 206 /** 134 * @return \Psr\Http\Message\StreamInterface207 * @return mixed 135 208 */ 136 209 public function listShippingMethods() … … 140 213 141 214 /** 142 * @param $postcode 143 * @param null $street_address 144 * @param null $country 145 * @param null $service_provider 146 * @param $limit 147 * @return \Psr\Http\Message\StreamInterface 148 */ 149 public function searchPickupPoints($postcode, $street_address = null, $country = null, $service_provider = null, $limit = 5) 150 { 215 * Search pickup points. 216 * 217 * @param int $postcode 218 * @param string $street_address 219 * @param string $country 220 * @param string $service_provider Limits results for to certain providers possible values: Posti, Matkahuolto, Db Schenker. 221 * @param int $limit 1 - 15 222 * @return mixed 223 */ 224 public function searchPickupPoints($postcode = null, $street_address = null, $country = null, $service_provider = null, $limit = 5) 225 { 226 if ( ($postcode == null && $street_address == null) || (trim($postcode) == '' && trim($street_address) == '') ) { 227 return '[]'; 228 } 229 151 230 $post_params = array( 152 231 'postcode' => (string) $postcode, 153 232 'address' => (string) $street_address, 154 233 'country' => (string) $country, 234 'service_provider' => (string) $service_provider, 235 'limit' => (int) $limit 236 ); 237 238 return $this->doPost('/pickup-points/search', $post_params); 239 } 240 241 /** 242 * Searches pickup points with a text query. For best results the query should contain a full address 243 * 244 * @param $query_text Text containing the full address, for example: "Keskustori 1, 33100 Tampere" 245 * @param string $service_provider $service_provider Limits results for to certain providers possible values: Posti, Matkahuolto, Db Schenker. 246 * @param int $limit 1 - 15 247 * @return mixed 248 */ 249 public function searchPickupPointsByText($query_text, $service_provider = null, $limit = 5) 250 { 251 if ( $query_text == null || trim($query_text) == '' ) { 252 return '[]'; 253 } 254 255 $post_params = array( 256 'query' => (string) $query_text, 155 257 'service_provider' => (string) $service_provider, 156 258 'limit' => (int) $limit -
woo-pakettikauppa/trunk/vendor/pakettikauppa/api-library/src/Pakettikauppa/Shipment.php
r1759098 r1966477 180 180 * Builds the xml from given data 181 181 * 182 * @return \SimpleXMLElement182 * @return SimpleXMLElement 183 183 */ 184 184 private function toXml() 185 185 { 186 $xml = new \SimpleXMLElement('<eChannel/>');186 $xml = new SimpleXMLElement('<eChannel/>'); 187 187 188 188 $routing = $xml->addChild('ROUTING'); -
woo-pakettikauppa/trunk/vendor/pakettikauppa/api-library/src/Pakettikauppa/Shipment/Parcel.php
r1759098 r1966477 114 114 public function setVolume($volume) 115 115 { 116 $this->volume = $volume;116 $this->volume = round($volume, 4); 117 117 } 118 118 -
woo-pakettikauppa/trunk/wc-pakettikauppa.php
r1908674 r1966477 2 2 /** 3 3 * Plugin Name: WooCommerce Pakettikauppa 4 * Version: 1. 0.84 * Version: 1.1.0 5 5 * Plugin URI: https://github.com/Seravo/woocommerce-pakettikauppa 6 6 * Description: Pakettikauppa shipping service for WooCommerce. Integrates Prinetti, Matkahuolto, DB Schenker and others. … … 21 21 // Prevent direct access to this script 22 22 if ( ! defined( 'ABSPATH' ) ) { 23 exit;23 exit; 24 24 } 25 25 … … 27 27 define( 'WC_PAKETTIKAUPPA_BASENAME', plugin_basename( __FILE__ ) ); 28 28 define( 'WC_PAKETTIKAUPPA_DIR', plugin_dir_path( __FILE__ ) ); 29 $upload_dir = wp_upload_dir();30 define( 'WC_PAKETTIKAUPPA_PRIVATE_DIR', $upload_dir['basedir'] . '/wc-pakettikauppa' );31 // @TODO: Now the location is unprotected. In future, allow users to customize this32 // location and use techniques like X-Sendfile to limit access to logged in users.33 34 /**35 * Prepare private directory for pakettikauppa documents.36 */37 function wc_pakettikauppa_prepare_directory() {38 // Create directory for plugin documents if does not yet exist39 if ( ! file_exists( WC_PAKETTIKAUPPA_PRIVATE_DIR ) ) {40 @wp_mkdir_p( WC_PAKETTIKAUPPA_PRIVATE_DIR );41 chmod( WC_PAKETTIKAUPPA_PRIVATE_DIR, 0755 );42 }43 44 // Create index.php to disallow directory listing in some incorrectly configured servers45 $index_file = WC_PAKETTIKAUPPA_PRIVATE_DIR . '/index.php';46 if ( ! file_exists( $index_file ) ) {47 touch( $index_file );48 }49 }50 add_action( 'init', 'wc_pakettikauppa_prepare_directory' );51 29 52 30 /** … … 56 34 */ 57 35 function wc_pakettikauppa_load_textdomain() { 58 load_plugin_textdomain( 'wc-pakettikauppa', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );36 load_plugin_textdomain( 'wc-pakettikauppa', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' ); 59 37 } 38 60 39 add_action( 'plugins_loaded', 'wc_pakettikauppa_load_textdomain' ); 61 40 … … 66 45 */ 67 46 function wc_pakettikauppa_load() { 68 if ( ! is_admin() ) {69 require_once plugin_dir_path( __FILE__ ) . 'includes/class-wc-pakettikauppa.php';70 $wc_pakettikauppa = new WC_Pakettikauppa();71 $wc_pakettikauppa->load();72 }47 if ( ! is_admin() ) { 48 require_once plugin_dir_path( __FILE__ ) . 'includes/class-wc-pakettikauppa.php'; 49 $wc_pakettikauppa = new WC_Pakettikauppa(); 50 $wc_pakettikauppa->load(); 51 } 73 52 } 53 74 54 add_action( 'init', 'wc_pakettikauppa_load' ); 75 55 … … 78 58 */ 79 59 function wc_pakettikauppa_load_admin() { 80 require_once plugin_dir_path( __FILE__ ) . 'includes/class-wc-pakettikauppa-admin.php';81 $wc_pakettikauppa_admin = new WC_Pakettikauppa_Admin();82 $wc_pakettikauppa_admin->load();60 require_once plugin_dir_path( __FILE__ ) . 'includes/class-wc-pakettikauppa-admin.php'; 61 $wc_pakettikauppa_admin = new WC_Pakettikauppa_Admin(); 62 $wc_pakettikauppa_admin->load(); 83 63 } 64 84 65 add_action( 'admin_init', 'wc_pakettikauppa_load_admin' );
Note: See TracChangeset
for help on using the changeset viewer.