plugin development – How to get the values of the wc_get_products Array or any Array in php?

Question

Woocommerce product objects don’t contain any properies to output, notice the suffix :protected.
This means that the properties you currently see when using a debug PHP function like, print_r or var_export, will be shown, but if you try to directly request one you’ll get an error.
This is also the reason why json_encode created an array of empty objects, as protected and private properties are ignored, same goes for object methods.

There is a great site for when you need to work with Woocommerce objects, like use, product, order, etc, called businessbloomer.
https://www.businessbloomer.com/woocommerce-easily-get-product-info-title-sku-desc-product-object/
This link, for example, show how to “get” each property of the product object.
Now that you know this one you can search for other examples for the types of objects available in Woocommerce.

There are also PHP functions that you can use to check what properties and methods are available in 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

Gets the class methods names.

This can accept the class or the object


get_object_vars

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

get_object_vars(object $object): array

Gets the accessible non-static properties of the given object according to scope.


get_class_vars

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

get_class_vars(string $class): array

Get the default properties of the given class.

0
user3037960 3 weeks 2022-11-07T05:19:05-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse