WooCommerce problem - returning integer portion of sizes for products

In WooCommerce, for example, I have a product with a specific width = '1.75' and height = '15 .75 '.

I am using the following function:

$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); 
}

But , and only returned to '1' and '15'. $product->get_width() $product->get_height()

Why does woocomrce return only the integer part of the dimensions?

Thank.

+4
source share
1 answer

I did a test setup of the product with your measurements in woocommerce (in my case, this product has the identifier 99). here is a screenshot of the settings:

enter image description here

Then I repeat each value in this way (using productID 99 in my case):

$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().

+2

Source: https://habr.com/ru/post/1655147/


All Articles