phpformswoocommerceproductshortcode

Replace Add to cart button with a contact form if product is out of stock in WooCommerce


With help from the community, I have successfully gotten the behavior I want with variable products, but I can't seem to duplicate it on simple products.

The code used is inserted into the functions.php file of my theme:

function woocommerce_get_custom_availability( $data, $product ) { 
    switch( $product->stock_status ) { 
        case 'readytoship': 
            $data = array( 'availability' => __( 'In Stock','woocommerce' ), 'class' => 'ready-to-ship' );
        break; 
        case 'outofstock': 
            $data = array( 'availability' => __( 'Call for Quote', 'woocommerce' ), 'class' => 'out-of-stock' ); 
        break; 
        case 'onbackorder': 
            $data = array( 'availability' => __( 'On Backorder', 'woocommerce' ), 'class' => 'onbackorder' ); 
        break; 
    } 
    return $data;
}

add_action('woocommerce_get_availability', 'woocommerce_get_custom_availability', 10, 2);

add_filter( 'woocommerce_available_variation', 'variation_out_of_stock_show_form', 10, 3 );
function variation_out_of_stock_show_form( $data, $product, $variation ) {
    if( ! $data['is_in_stock'] )
    {
        $data['availability_html'] = '<div class="contact-outofstock">';
        $data['availability_html'] .= 'Please contact us for a quote and availability.';
        $data['availability_html'] .= do_shortcode('[gravityform id="10" title="false" description="false" ajax="true"]'); // Replace with your own contact form shortcode
        $data['availability_html'] .= '</div>';
    }
    return $data;
}

add_action( 'woocommerce_single_product_summary', 'hide_add_to_cart_button_variable_product', 1, 0 );
function hide_add_to_cart_button_variable_product() {

    // Removing add to cart button and quantities only
    remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
}

But all variations on the above that I have tried for a simple product, both in the functions.php file and /woocommerce/templates/single-product/add-to-cart/simple.php just provide the default behavior (or produce a critical error).

Any suggestions?


Solution

  • There are some mistakes, missing things and also unnecessary things, to make it work for simple and variable products.

    Replace all your current related code with the following:

    add_action( 'woocommerce_get_availability', 'filter_wc_product_availability', 20, 2 );
    function filter_wc_product_availability( $data, $product ) { 
        switch( $product->get_stock_status() ) { 
            case 'readytoship': 
                $data = array( 'availability' => __( 'In Stock','woocommerce' ), 'class' => 'ready-to-ship' );
            break; 
            case 'outofstock': 
                $data = array( 'availability' => __( 'Call for Quote', 'woocommerce' ), 'class' => 'out-of-stock' ); 
            break; 
            case 'onbackorder': 
                $data = array( 'availability' => __( 'On Backorder', 'woocommerce' ), 'class' => 'onbackorder' ); 
            break; 
        } 
        return $data;
    }
    
    add_filter( 'woocommerce_get_stock_html', 'filter_wc_get_stock_html', 10, 2 );
    function filter_wc_get_stock_html( $html, $product ) {
        if (  $product->get_stock_status() === 'outofstock' ) {
            $html .= sprintf('<div class="contact-outofstock">%s</div>', 
            esc_html__('Please contact us for a quote and availability.') .
            do_shortcode('[gravityform id="10" title="false" description="false" ajax="true"]') );
            $html .= '<style>.woocommerce-variation-add-to-cart-disabled {display:none;}</style>';
        }
        return $html;
    }
    

    It should better work.