ID : ''; $package_volumetric = !empty ( $shipment ) ? wpcargo_package_volumetric( $shipment->ID ) : '0.00'; $package_actual_weight = !empty ( $shipment ) ? wpcargo_package_actual_weight( $shipment->ID ) : '0.00'; $package_volume = !empty ( $shipment ) ? wpcargo_package_volume( $shipment->ID ) : '0.00'; $dim_enable = wpcargo_package_settings()->dim_unit_enable; $fm_class = $dim_enable ? 'wpcargo-col-md-4' : 'wpcargo-col-md-12'; $ad_class = $dim_enable ? 'one-third' : ''; $class = is_admin() ? $ad_class : $fm_class ; $style = is_admin() ? 'style="display:block;overflow:hidden;margin-bottom:36px;"' : '' ; ?>
>
'. esc_html( wpcargo_package_settings()->weight_unit ); ?>.
'. esc_html( wpcargo_volume_unit_label() ); ?>
'.esc_html( wpcargo_package_settings()->weight_unit ); ?>.
ID : false; $divisor = apply_filters( 'wpcargo_volumetric_divisor', wpcargo_package_settings()->divisor, $shipment_id ); $decimal = apply_filters( 'wpcargo_package_decimal_place', 2 ); $dimesion_unit = trim( strtolower( wpcargo_package_settings()->dim_unit ) ); $cubic_divisor = wpcargo_volume_divisor(); $cubic_operator = $dimesion_unit != 'yd' ? '/' : '*' ; ?> dim_unit = $wpc_mp_dimension_unit; $package_settings->peice_types = $wpc_mp_peice_type; $package_settings->weight_unit = $wpc_mp_weight_unit; $package_settings->dim_unit_enable = $wpc_mp_enable_dimension_unit; $package_settings->admin_enable = apply_filters( 'wpcargo_package_admin_enable', $wpc_mp_enable_admin ); $package_settings->frontend_enable = apply_filters( 'wpcargo_package_frontend_enable', $wpc_mp_enable_frontend ); $package_settings->divisor = $woointeg_dim_divisor; $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.' ); return apply_filters( 'wpcargo_package_settings', $package_settings); } function wpcargo_package_dim_meta( ){ $dim_meta = array( 'wpc-pm-length', 'wpc-pm-width', 'wpc-pm-height' ); return apply_filters( 'wpcargo_package_dim_meta', $dim_meta ); } function wpcargo_package_qty_meta( ){ return apply_filters( 'wpcargo_package_qty_meta', 'wpc-pm-qty' ); } function wpcargo_package_weight_meta( ){ return apply_filters( 'wpcargo_package_weight_meta', 'wpc-pm-weight' ); } function wpcargo_package_fields(){ $package_fields = array( 'wpc-pm-qty' => array( 'label' => esc_html__('Qty.', 'wpcargo'), 'field' => 'number', 'required' => false, 'options' => array() ), 'wpc-pm-piece-type' => array( 'label' => esc_html__('Piece Type', 'wpcargo'), 'field' => 'select', 'required' => false, 'options' => wpcargo_package_settings()->peice_types ), 'wpc-pm-description' => array( 'label' => esc_html__('Description', 'wpcargo'), 'field' => 'textarea', 'required' => false, 'options' => array() ), ); if( wpcargo_package_settings()->dim_unit_enable ){ $package_fields['wpc-pm-length'] = array( 'label' => esc_html__('Length', 'wpcargo').'('.wpcargo_package_settings()->dim_unit.')', 'field' => 'number', 'required' => false, 'options' => array() ); $package_fields['wpc-pm-width'] = array( 'label' => esc_html__('Width', 'wpcargo').'('.wpcargo_package_settings()->dim_unit.')', 'field' => 'number', 'required' => false, 'options' => array() ); $package_fields['wpc-pm-height'] = array( 'label' => esc_html__('Height', 'wpcargo').'('.wpcargo_package_settings()->dim_unit.')', 'field' => 'number', 'required' => false, 'options' => array() ); } $package_fields['wpc-pm-weight'] = array( 'label' => esc_html__('Weight ', 'wpcargo').'('.wpcargo_package_settings()->weight_unit.')', 'field' => 'number', 'required' => false, 'options' => array() ); return apply_filters( 'wpcargo_package_fields', $package_fields ); } function wpcargo_get_package_data( $shipment_id, $meta_key = WPCARGO_PACKAGE_POSTMETA ){ $packages = get_post_meta( (int)$shipment_id, $meta_key, true) ? maybe_unserialize( get_post_meta( (int)$shipment_id, $meta_key, true) ) : array(); return apply_filters( 'wpcargo_get_package_data', $packages, $shipment_id, $meta_key ); } function wpcargo_volume_unit_label(){ $dimesion_unit = trim( strtolower( wpcargo_package_settings()->dim_unit ) ); $volume_meter = array( 'cm', 'm', 'mm', ); $volume_feet = array( 'ft', 'in', 'yd', ); if( in_array( $dimesion_unit, $volume_meter ) ){ return apply_filters( 'wpcargo_convert_volume_unit_cm', __('cu. m.', 'wpcargo' ) ); }elseif( in_array( $dimesion_unit, $volume_feet ) ){ return apply_filters( 'wpcargo_convert_volume_unit_in', __('cu. ft.', 'wpcargo' ) ); }else{ return $dimesion_unit; } } function wpcargo_volume_divisor( $shipment_id = false ){ $unit = wpcargo_package_settings()->dim_unit; switch ($unit) { case 'mm': $divisor = 1000000000; break; case 'cm': $divisor = 1000000; break; case 'm': $divisor = 1; break; case 'in': $divisor = 1728; break; case 'ft': $divisor = 1; break; case 'yd': $divisor = 27; break; default: $divisor = 1; break; } return apply_filters( 'wpcargo_volume_divisor', $divisor, $shipment_id ); } function wpcargo_package_volumetric( $shipment_id ){ $packages = wpcargo_get_package_data( $shipment_id ); $volumetric = wpcargo_calculate_volumetric( $packages, $shipment_id ); $decimal = wpcargo_dynamic_decimal( $volumetric ); return apply_filters( 'wpcargo_package_volumetric', number_format( $volumetric, $decimal, '.', ''), $shipment_id, $volumetric ); } function wpcargo_package_volume( $shipment_id ){ $packages = wpcargo_get_package_data( $shipment_id ); $volume = wpcargo_calculate_volume( $packages, $shipment_id ); $decimal = wpcargo_dynamic_decimal( $volume ); return apply_filters( 'wpcargo_package_volume', number_format( $volume, $decimal, '.', ''), $shipment_id, $volume ); } function wpcargo_package_actual_weight( $shipment_id ){ $packages = wpcargo_get_package_data( $shipment_id ); $weight = wpcargo_calculate_weight( $packages, $shipment_id ); $decimal = wpcargo_dynamic_decimal( $weight ); return apply_filters( 'wpcargo_package_actual_weight', number_format( $weight, $decimal, '.', ''), $shipment_id ); } // Package Calculator function wpcargo_calculate_volumetric( $packages, $shipment_id = false ){ $divisor = apply_filters( 'wpcargo_volumetric_divisor', wpcargo_package_settings()->divisor, $shipment_id ); $volumetric = 0; if( !empty( $packages ) ){ foreach ($packages as $key => $value) { $multiplier = 1; $qty = array_key_exists( wpcargo_package_qty_meta(), $value ) ? $value[wpcargo_package_qty_meta()] : 1 ; foreach ( wpcargo_package_dim_meta() as $dim_meta ) { if( !array_key_exists( $dim_meta, $value ) ){ continue; } $multiplier *= floatval($value[$dim_meta]); } $volumetric += ( floatval($multiplier) / floatval($divisor) ) * floatval($qty); } } return apply_filters( 'wpcargo_calculate_volumetric', $volumetric, $shipment_id ); } function wpcargo_calculate_volume( $packages, $shipment_id = false ){ $unit = wpcargo_package_settings()->dim_unit; $divisor = wpcargo_volume_divisor( $shipment_id ); $volume = 0; if( !empty( $packages ) ){ foreach ($packages as $key => $value) { $multiplier = 1; $qty = array_key_exists( wpcargo_package_qty_meta(), $value ) ? $value[wpcargo_package_qty_meta()] : 1 ; foreach ( wpcargo_package_dim_meta() as $dim_meta ) { if( !array_key_exists( $dim_meta, $value ) ){ continue; } $multiplier *= floatval($value[$dim_meta]); } if( $unit == 'yd' ){ $volume += ( floatval($multiplier) * floatval($divisor) ) * floatval($qty); }else{ $volume += ( floatval($multiplier) / floatval($divisor) ) * floatval($qty); } } } return apply_filters( 'wpcargo_calculate_volume', $volume, $shipment_id ); } function wpcargo_calculate_weight( $packages, $shipment_id = false ){ $weight = 0; if( !empty( $packages ) ){ foreach ($packages as $key => $value) { $qty = array_key_exists( wpcargo_package_qty_meta(), $value ) ? $value[wpcargo_package_qty_meta()] : 0 ; $weight_meta = array_key_exists( wpcargo_package_weight_meta(), $value ) ? $value[wpcargo_package_weight_meta()] : 0 ; $weight_meta = apply_filters( 'wpcargo_calculate_weight_meta', $weight_meta ); $weight += floatval( $weight_meta ) * floatval( $qty ); } } return apply_filters( 'wpcargo_calculate_weight', $weight, $shipment_id ); } function multiple_package_status(){ $status = false; if( wpcargo_package_settings()->frontend_enable && !empty( wpcargo_package_fields() ) ) { $status = true; } return apply_filters( 'multiple_package_status', $status ); } // Checked dynamic decimal places function wpcargo_dynamic_decimal( $value ){ if(!$value){ return 2; } $zero_decimal = "0.0"; $decimal_place = 2; if( $value < 1 ){ for( $i=2; $i<10; $i++ ){ $new_decimal = str_pad( $zero_decimal, $i, "0", STR_PAD_RIGHT ); $zero_value = $new_decimal."1"; if( $value < $zero_value ){ $decimal_place = $i+1; } } } return $decimal_place; } // Save Shipment Package function wpcargo_save_package_callback( $shipment_id, $data ){ if( !isset( $data[WPCARGO_PACKAGE_POSTMETA] ) ){ return false; } $packages = array(); if( is_array( $_POST[WPCARGO_PACKAGE_POSTMETA] ) ){ $packages = array_map( function( $value ){ return array_map( 'sanitize_text_field', $value ); }, $_POST[WPCARGO_PACKAGE_POSTMETA] ); } update_post_meta( $shipment_id, WPCARGO_PACKAGE_POSTMETA, $packages ); } add_action( 'wpcargo_after_save_shipment', 'wpcargo_save_package_callback', 10, 2 );