تابع ووکامرسی wc_get_weight تبدیل وزن محصول به واحد مورد نظر

تابع ووکامرسی wc_get_weight – تبدیل وزن محصول به واحد مورد نظر
Syntax – سینتکس
(float) wc_get_weight( (int|float) $weight, (string) $to_unit, (string) $from_unit = '' );
Parameters – پارامتر ها (3)
- 1- $weight (int|float)
- 2- $to_unit (string)
- 3- $from_unit (string)
Usage – نحوه استفاده
if ( !function_exists( 'wc_get_weight' ) ) {
require_once ABSPATH . PLUGINDIR . 'woocommerce/includes/wc-formatting-functions.php';
}
// The weight.
$weight = null;
// 'g', 'kg', 'lbs', 'oz'
$to_unit = '';
// (optional) 'g', 'kg', 'lbs', 'oz'
$from_unit = '';
// NOTICE! Understand what this does before running.
$result = wc_get_weight($weight, $to_unit, $from_unit);
Defined – محل تعریف
/includes/wc-formatting-functions.php
function wc_get_weight( $weight, $to_unit, $from_unit = '' ) {
$weight = (float) $weight;
$to_unit = strtolower( $to_unit );
if ( empty( $from_unit ) ) {
$from_unit = strtolower( get_option( 'woocommerce_weight_unit' ) );
}
// Unify all units to kg first.
if ( $from_unit !== $to_unit ) {
switch ( $from_unit ) {
case 'g' :
$weight *= 0.001;
break;
case 'lbs' :
$weight *= 0.453592;
break;
case 'oz' :
$weight *= 0.0283495;
break;
}
// Output desired unit.
switch ( $to_unit ) {
case 'g' :
$weight *= 1000;
break;
case 'lbs' :
$weight *= 2.20462;
break;
case 'oz' :
$weight *= 35.274;
break;
}
}
return ( $weight < 0 ) ? 0 : $weight;
}
versions – نسخه ها
از نسخه : 3.0.2
نسخه فعلی : 3.0.6
دیگر نسخه ها : 3.0.6 , 3.0.5 , 3.0.4 , 3.0.3 , 3.0.2
ارسال نظر