In woocommerce, using contact Form 7 plugin, I'm trying to replace the product quantity field, in the product summary, with a form, when a product is out of stock.
It works fine on variable products but on simple products it still shows the form and the quantity box.
It feels like I'm overlooking something very basic.
I've replaced the different echo
with "simple" and "variable" to find out which form is shown, but on simple products it still shows the 'variable' form.
Here is my code:
add_action( 'woocommerce_single_product_summary', 'add_form' );
function add_form() {
global $product;
if( $product->is_type( 'simple' ) ){
// a simple product
if(!$product->is_in_stock( )) {
echo do_shortcode('[contact-form-7 id="304" title="Contact stock"]');
//echo "simple";
}
} elseif( $product->is_type( 'variable' ) ){
// a variable product
$count_in_stock == 0;
$variation_ids = $product->get_children(); // Get product variation IDs
foreach( $variation_ids as $variation_id ){
$variation = wc_get_product($variation_id);
if( $variation->is_in_stock() )
$count_in_stock++;
}
}
if( $count_in_stock == 0 ) {
echo do_shortcode('[contact-form-7 id="304" title="Contact stock"]');
//echo "variable";
}
}