Dmitrii K
Dmitrii K

Reputation: 267

WooCommerce issue - Returning only integer part of sizes for products

In WooCommerce, for example, I have a product with defined width = '1.75' and height = '15.75'.

I use 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 $product->get_width() and $product->get_height() returning only '1' and '15'.

Why woocomrce returns only integer part of sizes?

Thanks.

Upvotes: 4

Views: 251

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253921

I have make a test setting a product with your dimensions in woocommerce (in my case this product have the ID 99). here is the screen shot of the settings:

enter image description here

Then I echo each value this way (using in my case 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);

And I get this output:

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

I don't understand really what is your problem, as I get the correct values using $product->get_width() and $product->get_height() methods.

Upvotes: 2

Related Questions