| 1 |
<?php |
| 2 |
if (!defined('ABSPATH')){ |
| 3 |
exit; // Exit if accessed directly |
| 4 |
} |
| 5 |
define( 'WPCARGO_PACKAGE_POSTMETA', apply_filters( 'wpcargo_package_postmeta', 'wpc-multiple-package' ) ); |
| 6 |
function wpcargo_package_weight_units(){ |
| 7 |
$units = array( |
| 8 |
_x( 'kg', 'Weigt units', 'wpcargo' ), |
| 9 |
_x( 'g', 'Weigt units', 'wpcargo' ), |
| 10 |
_x( 'lbs', 'Weigt units', 'wpcargo' ), |
| 11 |
_x( 'oz', 'Weigt units', 'wpcargo' ), |
| 12 |
); |
| 13 |
return apply_filters( 'wpcargo_package_weight_units', $units); |
| 14 |
} |
| 15 |
function wpcargo_package_dim_units(){ |
| 16 |
$units = array( |
| 17 |
_x( 'mm', 'Dimension units', 'wpcargo' ), |
| 18 |
_x( 'cm', 'Dimension units', 'wpcargo' ), |
| 19 |
_x( 'm', 'Dimension units', 'wpcargo' ), |
| 20 |
_x( 'in', 'Dimension units', 'wpcargo' ), |
| 21 |
_x( 'ft', 'Dimension units', 'wpcargo' ), |
| 22 |
_x( 'yd', 'Dimension units', 'wpcargo' ) |
| 23 |
); |
| 24 |
return apply_filters( 'wpcargo_package_dim_units', $units); |
| 25 |
} |
| 26 |
function wpcargo_multiple_package_details_tbl_responsive(){ |
| 27 |
?> |
| 28 |
<style> |
| 29 |
/* |
| 30 |
Max width before this PARTICULAR table gets nasty. This query will take effect for any screen smaller than 760px and also iPads specifically. |
| 31 |
*/ |
| 32 |
#wpcsr-rate-section .wpcsr-payment-details th, |
| 33 |
#wpcsr-rate-section .wpcsr-details th { |
| 34 |
background: #eee; |
| 35 |
} |
| 36 |
#wpcsr-rate-section .wpcsr-payment-details tr th, |
| 37 |
#wpcsr-rate-section .wpcsr-payment-details tr td, |
| 38 |
#wpcsr-rate-section .wpcsr-details tr th, |
| 39 |
#wpcsr-rate-section .wpcsr-details tr td { |
| 40 |
border-color: #bdbdbd; |
| 41 |
border: 1px solid #bdbdbd; |
| 42 |
padding: 6px; |
| 43 |
} |
| 44 |
@media |
| 45 |
only screen |
| 46 |
and (max-width: 760px), (min-device-width: 768px) |
| 47 |
and (max-device-width: 1024px) { |
| 48 |
/* Force table to not be like tables anymore */ |
| 49 |
/* Shipping rate pacakge */ |
| 50 |
#wpcsr-rate-packages table.wpcargo-table, |
| 51 |
#wpcsr-rate-packages table.wpcargo-table thead, |
| 52 |
#wpcsr-rate-packages table.wpcargo-table tbody, |
| 53 |
#wpcsr-rate-packages table.wpcargo-table th, |
| 54 |
#wpcsr-rate-packages table.wpcargo-table td, |
| 55 |
#wpcsr-rate-packages table.wpcargo-table tr, |
| 56 |
/* Normal package */ |
| 57 |
#wpc-multiple-package table.wpcargo-table, |
| 58 |
#wpc-multiple-package table.wpcargo-table thead, |
| 59 |
#wpc-multiple-package table.wpcargo-table tbody, |
| 60 |
#wpc-multiple-package table.wpcargo-table th, |
| 61 |
#wpc-multiple-package table.wpcargo-table td, |
| 62 |
#wpc-multiple-package table.wpcargo-table tr { |
| 63 |
display: block; |
| 64 |
} |
| 65 |
/* Shipping rate pacakge */ |
| 66 |
#wpcsr-rate-packages table.wpcargo-table thead tr, |
| 67 |
/* Normal package */ |
| 68 |
#wpc-multiple-package table.wpcargo-table thead tr { |
| 69 |
position: absolute; |
| 70 |
top: -9999px; |
| 71 |
left: -9999px; |
| 72 |
} |
| 73 |
#wpcsr-rate-packages table.wpcargo-table tr:nth-of-type(odd), |
| 74 |
#wpc-multiple-package table.wpcargo-table tr:nth-of-type(odd) { |
| 75 |
background: #eee; |
| 76 |
} |
| 77 |
/* Shipping rate pacakge */ |
| 78 |
#wpcsr-rate-packages table.wpcargo-table tr, |
| 79 |
/* Normal package */ |
| 80 |
#wpc-multiple-package table.wpcargo-table tr { |
| 81 |
border: 1px solid #ccc; |
| 82 |
} |
| 83 |
/* Shipping rate pacakge */ |
| 84 |
#wpcsr-rate-packages table.wpcargo-table td, |
| 85 |
/* Normal package */ |
| 86 |
#wpc-multiple-package table.wpcargo-table td { |
| 87 |
/* Behave like a "row" */ |
| 88 |
border: none; |
| 89 |
border-bottom: 1px solid #eee; |
| 90 |
position: relative; |
| 91 |
padding-left: 50%; |
| 92 |
} |
| 93 |
/* Shipping rate pacakge */ |
| 94 |
#wpcsr-rate-packages table.wpcargo-table td:before, |
| 95 |
/* Normal package */ |
| 96 |
#wpc-multiple-package table.wpcargo-table td:before { |
| 97 |
position: absolute; |
| 98 |
top: 6px; |
| 99 |
left: 6px; |
| 100 |
width: 45%; |
| 101 |
padding-right: 10px; |
| 102 |
white-space: nowrap; |
| 103 |
} |
| 104 |
/* |
| 105 |
Label the data |
| 106 |
*/ |
| 107 |
<?php $counter = 1; ?> |
| 108 |
<?php foreach ( wpcargo_package_fields() as $key => $value ): ?> |
| 109 |
<?php if( in_array( $key, wpcargo_package_dim_meta() ) && !wpcargo_package_settings()->dim_unit_enable ){ continue; } ?> |
| 110 |
/* Shipping rate pacakge */ |
| 111 |
#wpcsr-rate-packages table.wpcargo-table td:nth-of-type(<?php echo esc_html( $counter ); ?>):before { content: "<?php echo esc_html( $value['label'] ); ?>"; } |
| 112 |
/* Normal package */ |
| 113 |
#wpc-multiple-package table.wpcargo-table td:nth-of-type(<?php echo esc_html( $counter ); ?>):before { content: "<?php echo esc_html( $value['label'] ); ?>"; } |
| 114 |
<?php $counter++; endforeach; ?> |
| 115 |
} |
| 116 |
</style> |
| 117 |
<?php |
| 118 |
} |
| 119 |
add_action('wpcargo_after_package_details', 'wpcargo_multiple_package_details_tbl_responsive', 10, 1); |
| 120 |
function wpcargo_multiple_package_after_track_details( $shipment ){ |
| 121 |
if( multiple_package_status() ) { |
| 122 |
$template = wpcargo_include_template( 'package.tpl' ); |
| 123 |
require( $template ); |
| 124 |
} |
| 125 |
} |
| 126 |
add_action('wpcargo_after_package_details', 'wpcargo_multiple_package_after_track_details', 5, 1 ); |
| 127 |
function wpcargo_after_package_details_callback( $shipment ){ |
| 128 |
$shipment_id = !empty ( $shipment ) ? $shipment->ID : ''; |
| 129 |
$package_volumetric = !empty ( $shipment ) ? wpcargo_package_volumetric( $shipment->ID ) : '0.00'; |
| 130 |
$package_actual_weight = !empty ( $shipment ) ? wpcargo_package_actual_weight( $shipment->ID ) : '0.00'; |
| 131 |
$package_volume = !empty ( $shipment ) ? wpcargo_package_volume( $shipment->ID ) : '0.00'; |
| 132 |
$dim_enable = wpcargo_package_settings()->dim_unit_enable; |
| 133 |
$fm_class = $dim_enable ? 'wpcargo-col-md-4' : 'wpcargo-col-md-12'; |
| 134 |
$ad_class = $dim_enable ? 'one-third' : ''; |
| 135 |
$class = is_admin() ? $ad_class : $fm_class ; |
| 136 |
$style = is_admin() ? 'style="display:block;overflow:hidden;margin-bottom:36px;"' : '' ; |
| 137 |
?> |
| 138 |
<div id="package-weight-info" class="wpcargo-container" <?php echo esc_html( $style ); ?>> |
| 139 |
<div class="wpcargo-row"> |
| 140 |
<?php if( $dim_enable ): ?> |
| 141 |
<section class="<?php echo esc_html( $class ); ?> first" style="text-align: center; font-weight: bold;"> |
| 142 |
<?php echo esc_html(apply_filters( 'wpcargo_package_volumetric_label', __('Total Volumetric Weight :', 'wpcargo') )); ?> <span id="package_volumetric"><?php echo esc_html( $package_volumetric ).'</span>'. esc_html( wpcargo_package_settings()->weight_unit ); ?>. |
| 143 |
</section> |
| 144 |
<section class="<?php echo esc_html( $class ); ?>" style="text-align: center; font-weight: bold;"> |
| 145 |
<?php echo esc_html(apply_filters( 'wpcargo_package_volume_label', __('Total Volume :', 'wpcargo') )); ?> <span id="package_volume"><?php echo esc_html( $package_volume ).'</span>'. esc_html( wpcargo_volume_unit_label() ); ?> |
| 146 |
</section> |
| 147 |
<?php endif; ?> |
| 148 |
<section class="<?php echo esc_html( $class ); ?>" style="text-align: center; font-weight: bold;"> |
| 149 |
<?php echo esc_html(apply_filters( 'wpcargo_package_actual_weight_label', __('Total Actual Weight :', 'wpcargo') )); ?> <span id="package_actual_weight"><?php echo esc_html( $package_actual_weight ).'</span>'.esc_html( wpcargo_package_settings()->weight_unit ); ?>. |
| 150 |
</section> |
| 151 |
</div> |
| 152 |
</div> |
| 153 |
<?php |
| 154 |
do_action('wpcargo_after_package_details_script', $shipment); |
| 155 |
} |
| 156 |
add_action('wpcargo_after_package_totals', 'wpcargo_after_package_details_callback', 10, 1 ); |
| 157 |
add_action('wpcargo_after_package_details_script', 'wpcargo_after_package_details_script_callback', 10, 1 ); |
| 158 |
function wpcargo_after_package_details_script_callback( $shipment ){ |
| 159 |
$dim_meta = wpcargo_package_dim_meta(); |
| 160 |
$qty_meta = wpcargo_package_qty_meta(); |
| 161 |
$weight_meta = wpcargo_package_weight_meta(); |
| 162 |
$shipment_id = $shipment ? $shipment->ID : false; |
| 163 |
$divisor = apply_filters( 'wpcargo_volumetric_divisor', wpcargo_package_settings()->divisor, $shipment_id ); |
| 164 |
$decimal = apply_filters( 'wpcargo_package_decimal_place', 2 ); |
| 165 |
$dimesion_unit = trim( strtolower( wpcargo_package_settings()->dim_unit ) ); |
| 166 |
$cubic_divisor = wpcargo_volume_divisor(); |
| 167 |
$cubic_operator = $dimesion_unit != 'yd' ? '/' : '*' ; |
| 168 |
?> |
| 169 |
<script> |
| 170 |
var mainContainer = 'table tbody[data-repeater-list="<?php echo esc_html(WPCARGO_PACKAGE_POSTMETA); ?>"]'; |
| 171 |
var divisor = <?php echo esc_html( $divisor ); ?>; |
| 172 |
var dimMeta = <?php echo json_encode( $dim_meta ); ?>; |
| 173 |
var qtyMeta = "<?php echo esc_html( $qty_meta ); ?>"; |
| 174 |
var weightMeta = "<?php echo esc_html( $weight_meta ); ?>"; |
| 175 |
var cubic_divisor = <?php echo esc_html( $cubic_divisor ); ?>; |
| 176 |
var cubic_operator = "<?php echo esc_html( $cubic_operator ); ?>"; |
| 177 |
jQuery(document).ready(function($){ |
| 178 |
<?php do_action( 'wpcargo_package_details_script' ); ?> |
| 179 |
function getDecimal( value = 0 ){ |
| 180 |
value = parseFloat( value ); |
| 181 |
if( value >= 1 ){ |
| 182 |
return <?php echo esc_html($decimal); ?>; |
| 183 |
} |
| 184 |
var str = "0."; |
| 185 |
var dPlace = 1; |
| 186 |
for (i = 1; i < 10; i++) { |
| 187 |
str = str+'0'; |
| 188 |
var z_value = str+"1"; |
| 189 |
if( parseFloat(value) > parseFloat( z_value ) ){ |
| 190 |
dPlace = i+2; |
| 191 |
break; |
| 192 |
} |
| 193 |
} |
| 194 |
return dPlace; |
| 195 |
} |
| 196 |
function calculatePackage(){ |
| 197 |
var totalQTY = 0; |
| 198 |
var totalWeight = 0; |
| 199 |
var totalVolumetric = 0; |
| 200 |
var totalVolume = 0; |
| 201 |
$( mainContainer + ' tr' ).each(function(){ |
| 202 |
var currentVolumetric = 1; |
| 203 |
var currentQTY = 0; |
| 204 |
var packageWeight = 0; |
| 205 |
$(this).find('input').each(function(){ |
| 206 |
var currentField = $(this); |
| 207 |
var className = $( currentField ).attr('name'); |
| 208 |
// Exclude in the loop field without name attribute |
| 209 |
if ( typeof className === "undefined" ){ |
| 210 |
return; |
| 211 |
} |
| 212 |
// Get the QTY |
| 213 |
if ( className.indexOf(qtyMeta) > -1 ){ |
| 214 |
var pQty = $( currentField ).val() == '' ? 0 : $( currentField ).val() ; |
| 215 |
totalQTY += parseFloat( pQty ); |
| 216 |
currentQTY = parseFloat( pQty ); |
| 217 |
} |
| 218 |
// Get the weight |
| 219 |
if ( className.indexOf(weightMeta) > -1 ){ |
| 220 |
var pWeight = $( currentField ).val() == '' ? 0 : $( currentField ).val() ; |
| 221 |
packageWeight += parseFloat( pWeight ); |
| 222 |
} |
| 223 |
|
| 224 |
// Calculate the volumetric |
| 225 |
$.each( dimMeta, function( index, value ){ |
| 226 |
|
| 227 |
if ( className.indexOf(value) == -1 ){ |
| 228 |
return; |
| 229 |
} |
| 230 |
currentVolumetric *= $( currentField ).val(); |
| 231 |
} ); |
| 232 |
}); |
| 233 |
totalVolumetric += currentQTY * ( currentVolumetric / divisor ); |
| 234 |
totalWeight += currentQTY * packageWeight; |
| 235 |
// totalVolume += currentQTY * currentVolumetric; |
| 236 |
// Calculate Volume |
| 237 |
if( cubic_operator == '*' ){ |
| 238 |
totalVolume += currentQTY * ( currentVolumetric * cubic_divisor ); |
| 239 |
}else{ |
| 240 |
totalVolume += currentQTY * ( currentVolumetric / cubic_divisor ); |
| 241 |
} |
| 242 |
}); |
| 243 |
|
| 244 |
|
| 245 |
$('#package-weight-info #package_volume').text( totalVolume.toFixed( getDecimal(totalVolume) ) ); |
| 246 |
$('#package-weight-info #package_volumetric').text( totalVolumetric.toFixed( getDecimal(totalVolumetric) ) ); |
| 247 |
$('#package-weight-info #package_actual_weight').text( totalWeight.toFixed( getDecimal(totalWeight) ) ); |
| 248 |
} |
| 249 |
if( mainContainer.length > 0 ){ |
| 250 |
$( mainContainer ).on( 'change keyup', 'input', function(){ |
| 251 |
calculatePackage(); |
| 252 |
}); |
| 253 |
$('body').on('DOMSubtreeModified', mainContainer, function(){ |
| 254 |
setTimeout(() => { |
| 255 |
calculatePackage(); |
| 256 |
}, 1000 ); |
| 257 |
}); |
| 258 |
} |
| 259 |
}); |
| 260 |
</script> |
| 261 |
<?php |
| 262 |
} |
| 263 |
function wpcargo_package_settings(){ |
| 264 |
$options = get_option( 'wpc_mp_settings' ); |
| 265 |
$options = $options ? $options : array(); |
| 266 |
// Dimension compatibility |
| 267 |
$dimension_divisor = array_key_exists( 'wpcargo_dim_divisor', $options ) ? $options['wpcargo_dim_divisor'] : false ; |
| 268 |
|
| 269 |
// Depreciatred |
| 270 |
$woointeg_dim_divisor = get_option( 'woointeg_dim_divisor' ) ? floatval( get_option( 'woointeg_dim_divisor' ) ) : 5000 ; |
| 271 |
$woointeg_dim_divisor = apply_filters( 'wpcargo_package_divisor_cm', $woointeg_dim_divisor ); |
| 272 |
$woointeg_dim_divisor_inc = get_option( 'woointeg_dim_divisor_inc' ) ? floatval( get_option( 'woointeg_dim_divisor_inc' ) ) : 138.4 ; |
| 273 |
$woointeg_dim_divisor_inc = apply_filters( 'wpcargo_package_divisor_inc', $woointeg_dim_divisor_inc ); |
| 274 |
|
| 275 |
// New Dimesion Divisor |
| 276 |
$woointeg_dim_divisor = $dimension_divisor ? $dimension_divisor : $woointeg_dim_divisor; |
| 277 |
|
| 278 |
$wpc_mp_dimension_unit = array_key_exists( 'wpc_mp_dimension_unit', $options ) ? $options['wpc_mp_dimension_unit'] : 'cm'; |
| 279 |
$wpc_mp_peice_type = array_key_exists( 'wpc_mp_piece_type', $options ) ? array_filter( array_map('trim', explode(",", $options['wpc_mp_piece_type']) ) ) : array(); |
| 280 |
$wpc_mp_weight_unit = array_key_exists( 'wpc_mp_weight_unit', $options ) ? $options['wpc_mp_weight_unit'] : 'lbs'; |
| 281 |
$wpc_mp_enable_dimension_unit = array_key_exists( 'wpc_mp_enable_dimension_unit', $options ) ? $options['wpc_mp_enable_dimension_unit'] : false; |
| 282 |
$wpc_mp_enable_admin = array_key_exists( 'wpc_mp_enable_admin', $options ) ? $options['wpc_mp_enable_admin'] : false; |
| 283 |
$wpc_mp_enable_frontend = array_key_exists( 'wpc_mp_enable_frontend', $options ) ? $options['wpc_mp_enable_frontend'] : false; |
| 284 |
$package_settings = new stdClass(); |
| 285 |
$package_settings->dim_unit = $wpc_mp_dimension_unit; |
| 286 |
$package_settings->peice_types = $wpc_mp_peice_type; |
| 287 |
$package_settings->weight_unit = $wpc_mp_weight_unit; |
| 288 |
$package_settings->dim_unit_enable = $wpc_mp_enable_dimension_unit; |
| 289 |
$package_settings->admin_enable = apply_filters( 'wpcargo_package_admin_enable', $wpc_mp_enable_admin ); |
| 290 |
$package_settings->frontend_enable = apply_filters( 'wpcargo_package_frontend_enable', $wpc_mp_enable_frontend ); |
| 291 |
$package_settings->divisor = $woointeg_dim_divisor; |
| 292 |
$package_settings->volume_unit = $wpc_mp_dimension_unit == 'cm' ? apply_filters( 'wpcargo_package_cm_volume_unit', 'kg.' ) : apply_filters( 'wpcargo_package_inc_volume_unit', 'lbs.' ); |
| 293 |
return apply_filters( 'wpcargo_package_settings', $package_settings); |
| 294 |
} |
| 295 |
function wpcargo_package_dim_meta( ){ |
| 296 |
$dim_meta = array( 'wpc-pm-length', 'wpc-pm-width', 'wpc-pm-height' ); |
| 297 |
return apply_filters( 'wpcargo_package_dim_meta', $dim_meta ); |
| 298 |
} |
| 299 |
function wpcargo_package_qty_meta( ){ |
| 300 |
return apply_filters( 'wpcargo_package_qty_meta', 'wpc-pm-qty' ); |
| 301 |
} |
| 302 |
function wpcargo_package_weight_meta( ){ |
| 303 |
return apply_filters( 'wpcargo_package_weight_meta', 'wpc-pm-weight' ); |
| 304 |
} |
| 305 |
function wpcargo_package_fields(){ |
| 306 |
$package_fields = array( |
| 307 |
'wpc-pm-qty' => array( |
| 308 |
'label' => esc_html__('Qty.', 'wpcargo'), |
| 309 |
'field' => 'number', |
| 310 |
'required' => false, |
| 311 |
'options' => array() |
| 312 |
), |
| 313 |
'wpc-pm-piece-type' => array( |
| 314 |
'label' => esc_html__('Piece Type', 'wpcargo'), |
| 315 |
'field' => 'select', |
| 316 |
'required' => false, |
| 317 |
'options' => wpcargo_package_settings()->peice_types |
| 318 |
), |
| 319 |
'wpc-pm-description' => array( |
| 320 |
'label' => esc_html__('Description', 'wpcargo'), |
| 321 |
'field' => 'textarea', |
| 322 |
'required' => false, |
| 323 |
'options' => array() |
| 324 |
), |
| 325 |
); |
| 326 |
if( wpcargo_package_settings()->dim_unit_enable ){ |
| 327 |
$package_fields['wpc-pm-length'] = array( |
| 328 |
'label' => esc_html__('Length', 'wpcargo').'('.wpcargo_package_settings()->dim_unit.')', |
| 329 |
'field' => 'number', |
| 330 |
'required' => false, |
| 331 |
'options' => array() |
| 332 |
); |
| 333 |
$package_fields['wpc-pm-width'] = array( |
| 334 |
'label' => esc_html__('Width', 'wpcargo').'('.wpcargo_package_settings()->dim_unit.')', |
| 335 |
'field' => 'number', |
| 336 |
'required' => false, |
| 337 |
'options' => array() |
| 338 |
); |
| 339 |
$package_fields['wpc-pm-height'] = array( |
| 340 |
'label' => esc_html__('Height', 'wpcargo').'('.wpcargo_package_settings()->dim_unit.')', |
| 341 |
'field' => 'number', |
| 342 |
'required' => false, |
| 343 |
'options' => array() |
| 344 |
); |
| 345 |
} |
| 346 |
$package_fields['wpc-pm-weight'] = array( |
| 347 |
'label' => esc_html__('Weight ', 'wpcargo').'('.wpcargo_package_settings()->weight_unit.')', |
| 348 |
'field' => 'number', |
| 349 |
'required' => false, |
| 350 |
'options' => array() |
| 351 |
); |
| 352 |
return apply_filters( 'wpcargo_package_fields', $package_fields ); |
| 353 |
} |
| 354 |
function wpcargo_get_package_data( $shipment_id, $meta_key = WPCARGO_PACKAGE_POSTMETA ){ |
| 355 |
$packages = get_post_meta( (int)$shipment_id, $meta_key, true) ? maybe_unserialize( get_post_meta( (int)$shipment_id, $meta_key, true) ) : array(); |
| 356 |
return apply_filters( 'wpcargo_get_package_data', $packages, $shipment_id, $meta_key ); |
| 357 |
} |
| 358 |
function wpcargo_volume_unit_label(){ |
| 359 |
$dimesion_unit = trim( strtolower( wpcargo_package_settings()->dim_unit ) ); |
| 360 |
$volume_meter = array( 'cm', 'm', 'mm', ); |
| 361 |
$volume_feet = array( 'ft', 'in', 'yd', ); |
| 362 |
if( in_array( $dimesion_unit, $volume_meter ) ){ |
| 363 |
return apply_filters( 'wpcargo_convert_volume_unit_cm', __('cu. m.', 'wpcargo' ) ); |
| 364 |
}elseif( in_array( $dimesion_unit, $volume_feet ) ){ |
| 365 |
return apply_filters( 'wpcargo_convert_volume_unit_in', __('cu. ft.', 'wpcargo' ) ); |
| 366 |
}else{ |
| 367 |
return $dimesion_unit; |
| 368 |
} |
| 369 |
} |
| 370 |
function wpcargo_volume_divisor( $shipment_id = false ){ |
| 371 |
$unit = wpcargo_package_settings()->dim_unit; |
| 372 |
switch ($unit) { |
| 373 |
case 'mm': |
| 374 |
$divisor = 1000000000; |
| 375 |
break; |
| 376 |
case 'cm': |
| 377 |
$divisor = 1000000; |
| 378 |
break; |
| 379 |
case 'm': |
| 380 |
$divisor = 1; |
| 381 |
break; |
| 382 |
case 'in': |
| 383 |
$divisor = 1728; |
| 384 |
break; |
| 385 |
case 'ft': |
| 386 |
$divisor = 1; |
| 387 |
break; |
| 388 |
case 'yd': |
| 389 |
$divisor = 27; |
| 390 |
break; |
| 391 |
default: |
| 392 |
$divisor = 1; |
| 393 |
break; |
| 394 |
} |
| 395 |
return apply_filters( 'wpcargo_volume_divisor', $divisor, $shipment_id ); |
| 396 |
} |
| 397 |
function wpcargo_package_volumetric( $shipment_id ){ |
| 398 |
$packages = wpcargo_get_package_data( $shipment_id ); |
| 399 |
$volumetric = wpcargo_calculate_volumetric( $packages, $shipment_id ); |
| 400 |
$decimal = wpcargo_dynamic_decimal( $volumetric ); |
| 401 |
return apply_filters( 'wpcargo_package_volumetric', number_format( $volumetric, $decimal, '.', ''), $shipment_id, $volumetric ); |
| 402 |
} |
| 403 |
function wpcargo_package_volume( $shipment_id ){ |
| 404 |
$packages = wpcargo_get_package_data( $shipment_id ); |
| 405 |
$volume = wpcargo_calculate_volume( $packages, $shipment_id ); |
| 406 |
$decimal = wpcargo_dynamic_decimal( $volume ); |
| 407 |
return apply_filters( 'wpcargo_package_volume', number_format( $volume, $decimal, '.', ''), $shipment_id, $volume ); |
| 408 |
} |
| 409 |
function wpcargo_package_actual_weight( $shipment_id ){ |
| 410 |
$packages = wpcargo_get_package_data( $shipment_id ); |
| 411 |
$weight = wpcargo_calculate_weight( $packages, $shipment_id ); |
| 412 |
$decimal = wpcargo_dynamic_decimal( $weight ); |
| 413 |
return apply_filters( 'wpcargo_package_actual_weight', number_format( $weight, $decimal, '.', ''), $shipment_id ); |
| 414 |
} |
| 415 |
// Package Calculator |
| 416 |
function wpcargo_calculate_volumetric( $packages, $shipment_id = false ){ |
| 417 |
$divisor = apply_filters( 'wpcargo_volumetric_divisor', wpcargo_package_settings()->divisor, $shipment_id ); |
| 418 |
$volumetric = 0; |
| 419 |
if( !empty( $packages ) ){ |
| 420 |
foreach ($packages as $key => $value) { |
| 421 |
$multiplier = 1; |
| 422 |
$qty = array_key_exists( wpcargo_package_qty_meta(), $value ) ? $value[wpcargo_package_qty_meta()] : 1 ; |
| 423 |
foreach ( wpcargo_package_dim_meta() as $dim_meta ) { |
| 424 |
if( !array_key_exists( $dim_meta, $value ) ){ |
| 425 |
continue; |
| 426 |
} |
| 427 |
$multiplier *= floatval($value[$dim_meta]); |
| 428 |
} |
| 429 |
$volumetric += ( floatval($multiplier) / floatval($divisor) ) * floatval($qty); |
| 430 |
} |
| 431 |
} |
| 432 |
return apply_filters( 'wpcargo_calculate_volumetric', $volumetric, $shipment_id ); |
| 433 |
} |
| 434 |
function wpcargo_calculate_volume( $packages, $shipment_id = false ){ |
| 435 |
$unit = wpcargo_package_settings()->dim_unit; |
| 436 |
$divisor = wpcargo_volume_divisor( $shipment_id ); |
| 437 |
$volume = 0; |
| 438 |
if( !empty( $packages ) ){ |
| 439 |
foreach ($packages as $key => $value) { |
| 440 |
$multiplier = 1; |
| 441 |
$qty = array_key_exists( wpcargo_package_qty_meta(), $value ) ? $value[wpcargo_package_qty_meta()] : 1 ; |
| 442 |
foreach ( wpcargo_package_dim_meta() as $dim_meta ) { |
| 443 |
if( !array_key_exists( $dim_meta, $value ) ){ |
| 444 |
continue; |
| 445 |
} |
| 446 |
$multiplier *= floatval($value[$dim_meta]); |
| 447 |
} |
| 448 |
if( $unit == 'yd' ){ |
| 449 |
$volume += ( floatval($multiplier) * floatval($divisor) ) * floatval($qty); |
| 450 |
}else{ |
| 451 |
$volume += ( floatval($multiplier) / floatval($divisor) ) * floatval($qty); |
| 452 |
} |
| 453 |
} |
| 454 |
} |
| 455 |
return apply_filters( 'wpcargo_calculate_volume', $volume, $shipment_id ); |
| 456 |
} |
| 457 |
function wpcargo_calculate_weight( $packages, $shipment_id = false ){ |
| 458 |
$weight = 0; |
| 459 |
if( !empty( $packages ) ){ |
| 460 |
foreach ($packages as $key => $value) { |
| 461 |
$qty = array_key_exists( wpcargo_package_qty_meta(), $value ) ? $value[wpcargo_package_qty_meta()] : 0 ; |
| 462 |
$weight_meta = array_key_exists( wpcargo_package_weight_meta(), $value ) ? $value[wpcargo_package_weight_meta()] : 0 ; |
| 463 |
$weight_meta = apply_filters( 'wpcargo_calculate_weight_meta', $weight_meta ); |
| 464 |
$weight += floatval( $weight_meta ) * floatval( $qty ); |
| 465 |
} |
| 466 |
} |
| 467 |
return apply_filters( 'wpcargo_calculate_weight', $weight, $shipment_id ); |
| 468 |
} |
| 469 |
function multiple_package_status(){ |
| 470 |
$status = false; |
| 471 |
if( wpcargo_package_settings()->frontend_enable && !empty( wpcargo_package_fields() ) ) { |
| 472 |
$status = true; |
| 473 |
} |
| 474 |
return apply_filters( 'multiple_package_status', $status ); |
| 475 |
} |
| 476 |
// Checked dynamic decimal places |
| 477 |
function wpcargo_dynamic_decimal( $value ){ |
| 478 |
if(!$value){ |
| 479 |
return 2; |
| 480 |
} |
| 481 |
$zero_decimal = "0.0"; |
| 482 |
$decimal_place = 2; |
| 483 |
if( $value < 1 ){ |
| 484 |
for( $i=2; $i<10; $i++ ){ |
| 485 |
$new_decimal = str_pad( $zero_decimal, $i, "0", STR_PAD_RIGHT ); |
| 486 |
$zero_value = $new_decimal."1"; |
| 487 |
if( $value < $zero_value ){ |
| 488 |
$decimal_place = $i+1; |
| 489 |
} |
| 490 |
} |
| 491 |
} |
| 492 |
return $decimal_place; |
| 493 |
} |
| 494 |
// Save Shipment Package |
| 495 |
function wpcargo_save_package_callback( $shipment_id, $data ){ |
| 496 |
if( !isset( $data[WPCARGO_PACKAGE_POSTMETA] ) ){ |
| 497 |
return false; |
| 498 |
} |
| 499 |
$packages = array(); |
| 500 |
if( is_array( $_POST[WPCARGO_PACKAGE_POSTMETA] ) ){ |
| 501 |
$packages = array_map( function( $value ){ |
| 502 |
return array_map( 'sanitize_text_field', $value ); |
| 503 |
}, $_POST[WPCARGO_PACKAGE_POSTMETA] ); |
| 504 |
} |
| 505 |
update_post_meta( $shipment_id, WPCARGO_PACKAGE_POSTMETA, $packages ); |
| 506 |
} |
| 507 |
add_action( 'wpcargo_after_save_shipment', 'wpcargo_save_package_callback', 10, 2 ); |