I am using Woocommerce with the Woocommerce Composite Products extension (Which allows you to make a product out of a combination of other products).
I am trying to figure out if a composite product is 'in stock', but the usual function is_in_stock() doesn't work on composites.
If there is a more appropriate function, I would like to know what it is.
Alternatrively, The function 'wc_get_product()' below returns the below, including componentproduct IDs (assigned_ids):
$product = wc_get_product($productid);
var_dump($product);
Returns:
object(WC_Product_Composite)#11305 (23) {
["extended_data":"WC_Product_Composite":private]=>
array(7) {
["add_to_cart_form_location"]=>
string(7) "default"
["shop_price_calc"]=>
string(7) "default"
}
["composite_meta":"WC_Product_Composite":private]=>
array(4) {
[1477435352]=>
array(19) {
["component_id"]=>
string(10) "1477435352"
["query_type"]=>
string(11) "product_ids"
["assigned_ids"]=>
array(2) {
[0]=>
int(541)
[1]=>
int(588)
}
}
}
Basically, what I want is to extract and loop through the 'assigned_ids' subarray checking if each is in stock, but aside from printing this to the screen, I'm unable to access this object's properties because they are 'private' (something I'm completely unfamiliar with). I believe I can possibly figure this out by hunting down the class in Wordpress / the plugin files but I have no idea how to find it. Any help would be much appreciated.
Stock (much more complicated for composite products):
Regarding Stock and stock management in Composite products, apparently there is WC_CP_Stock_Manager
and WC_CP_Stock_Manager_Item
dedicated classes for that, so it doesn't seem to work in a classic way with some concepts of "items" and "collections".
This methods and functionalities are available in includes/class-wc-cp-stock-manager.php
core files.
Accessing the data:
As you can see, to get the WC_Product_Composite
object protected properties, you should need to use the inherited WC_Product
methods or more specifically WC_Product_Composite
methods documented in the core code class.
Now, The easiest way to access (unprotected) data is to use the following methods inherited from WC_Data
Class:
// Get an instance of the WC_Product_Composite object
$product = wc_get_product( $product_id );
// Get the data in an unprotected array
$product_data = $product->get_data();
// Output the raw data (for test)
echo '<pre>'; print_r( $product_data ); echo '</pre>';
// Get special meta data in an unprotected array (if it exist)
$product_meta_data = $product->get_meta_data();
// Output the raw special meta data (for test)
echo '<pre>'; print_r( $product_meta_data ); echo '</pre>';
Or you could use also some WC_Product_Composite
methods as:
// Get an instance of the WC_Product_Composite object
$product = wc_get_product( $product_id );
// Get the component Id
$component_data = $product->get_component_id();
// Get the component
$component_data = $product->get_component();
// Get the raw component data
$component_data = $product->get_component_data();
Any way you will need to get into core files code to understand how things works and to get the right methods for Composite products.