php – WooCommerce问题 – 只返回产品大小的整数部分

例如,在WooCommerce中,我有一个定义宽度=’1.75’且高度= ’15 .75’的产品.

我使用以下功能:

$product = new WC_Product($id);

public function get_sizes($product) {
  $w_size = floatval($product->get_width());
  $h_size = floatval($product->get_height());

  $w_M = intval($w_size);
  $h_M = intval($h_size);
  $w_CM = $w_size - intval($w_size);
  $h_CM = $h_size - intval($h_size);

  return array($w_M, $h_M, $w_CM, $h_CM); 
}

但$product-> get_width()和$product-> get_height()仅返回’1’和’15’.

为什么woocomrce只返回大小的整数部分?

谢谢.

最佳答案 我已经在woocommerce中设置了一个带有尺寸的产品的测试(在我的情况下,这个产品的ID为99).这是设置的屏幕截图:

《php – WooCommerce问题 – 只返回产品大小的整数部分》

然后我以这种方式回显每个值(在我的情况下使用productID 99):

$product = new WC_Product(99);

// You don't need floatval() function here.
$w_size = $product->get_width(); echo '$w_size is ' . $w_size; echo "<br>";
$h_size = $product->get_height(); echo '$h_size is ' . $h_size; echo "<br>";
$w_M = intval($w_size); echo '$w_M is ' . $w_M; echo "<br>";
$h_M = intval($h_size); echo '$h_M is ' . $h_M; echo "<br>";

// Here I have made some little changes (in both lines below) is the same as you
$w_CM = $w_size - $w_M; echo '$w_CM is ' . $w_CM; echo "<br>";
$h_CM = $h_size - $h_M; echo '$h_CM is ' . $h_CM; echo "<br>";

$arr = array($w_M, $h_M, $w_CM, $h_CM); print_r($arr);

我得到这个输出:

$w_size is 15.75
$h_size is 1.75
$w_M is 15
$h_M is 1
$w_CM is 0.75
$h_CM is 0.75
Array ( [0] => 15 [1] => 1 [2] => 0.75 [3] => 0.75 )

我真的不明白你的问题是什么,因为我使用$product-> get_width()和$product-> get_height()方法获得了正确的值.

点赞