Replies: 0
It would be really handy to be able to get the stock level of a bundled product. The function would have to work out the stock level of the bundled product by checking the stock level of the items that make up the bundle.
I needed this for a website I manage so wrote a function for it.
I achieved it by adding this to the following file:-
includes/class.yith-wc-product-bundle.php
/**
* Returns bundled stock quantity
* Sort through the bundled products and work out the maximum
* possible number of bundles based on stock levels.
*
* @return int
* @author Leanza Francesco <leanzafrancesco@gmail.com>
*/
public function get_bundle_stock_quantity() {
$bundled_items = !empty( $this->bundled_items ) ? $this->bundled_items : false;
if ( $bundled_items ) {
$maxPossibleBundles = array();
foreach ( $bundled_items as $bundled_item ) {
$stockQty = $bundled_item->get_product()->get_stock_quantity();
$qtyForOneBundle = $bundled_item->get_quantity();
$maxPossibleBundles[] = floor($stockQty / $qtyForOneBundle);
}
sort($maxPossibleBundles); // sort low to high
}
return $maxPossibleBundles[0];
}
Then in my template I could do:-
$_product->get_bundle_stock_quantity();
I actually posted about this 3 years ago:- https://wordpress.org/support/topic/showing-stock-level-of-bundled-products/
I still need it so every time I update the plugin I just keep adding it back in. Would be really cool if we could add it as part of the core plugin code.
Thanks,
Carl