例如,在Woocomerce中,我有一个产品的定义宽度为“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只返回大小的整数部分?
谢谢。

最佳答案

我已经做了一个测试设置与您的规模在伍Commerce产品(在我的情况下,这个产品有ID 99)。以下是设置的屏幕截图:
php - WooCommerce问题-仅返回产品尺寸的整数部分-LMLPHP
然后我以这种方式回显每个值(在我的示例中使用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()方法得到了正确的值。

09-25 16:32
查看更多