Plugin Development – How to get the values ​​of wc_get_products Array or any Array in php?

Woocommerce product items have no proprieties for output, note the suffix :protected,
This means that the properties you currently see when using debug PHP functions such as print_r or var_export will be shown, but you will get an error if you try to make the request directly.
This is why json_encode created an array of empty objects, because protected and private properties are ignored, the same goes for object methods.

When you need to work with Woocommerce items like uses, products, orders, etc. There is a great site called BusinessBloomer.
https://www.businessbloomer.com/woocommerce-easily-get-product-info-title-sku-desc-product-object/
This link, for example, shows how to “get” each property of a Product object.
Now that you know this you can find other examples for the types of items available in Woocommerce.

There are also PHP functions that you can use to check what properties and methods are available to a class or object.

get_class_methods

https://www.php.net/manual/en/function.get-class-methods.php

get_class_methods(object|string $object_or_class): array

Get the names of class methods.

It can accept class or object


get_object_vars

https://www.php.net/manual/en/function.get-object-vars.php

get_object_vars(object $object): array

Gets the non-static properties of a given object by scope.


get_class_vars

https://www.php.net/manual/en/function.get-class-vars.php

get_class_vars(string $class): array

Gets the default properties of the given class.

Leave a Comment