On a variable product with inventory controlled by a raw material, if the raw material is out of stock, the product id changes to the variable BOM product id. If the product is in stock, the product id remains the actual product id.
How can I get the actual product id on an out of stock variation controlled by a raw material?
CODE:
function dynamic_backorders_by_tag( $backorder_status, $product ) {
// Define the tag(s) that should trigger backorders
$target_tags = array( 'preorder', 'pre-order', 'allow-backorder' );
// Get the product ID (handling variations by checking the parent)
$product_id = $product->get_id();
if ( $product->is_type( 'variation' ) ) {
$product_id = $product->get_parent_id();
}
// Check if the product has any of the target tags
// NOT WORKING FOR QUANTITY = 0
/*
Only when variation quantity 0, the product id becomes the BOM variation instead of the acutal product.
*/
if ( has_term( $target_tags, 'product_tag', $product_id ) ) {
// Options: 'yes' (Allow), 'notify' (Allow but notify), 'no' (Do not allow)
echo('in stock variable parent $product_id: ' . $product->get_parent_id() . '<br/>'); // works!
return 'notify';
} else {
echo('out of stock variable parent $product_id: ' . $product->get_parent_id() . '<br/>'); // product id is BOM product instead of actual product
echo 'Atum BOM Product<br/>';
print_r($product);
}
return $backorder_status;
}
add_filter( 'woocommerce_product_get_backorders', 'dynamic_backorders_by_tag', 10, 2 );
add_filter( 'woocommerce_product_variation_get_backorders', 'dynamic_backorders_by_tag', 10, 2 );`