WooCommerce inquiry if no price available

I’m helping a good friend setting up a WooCommerce shop. Since the shop is going to be bigger and the products are pretty variable and customizable we are not able to provide/configure all prizes from the beginning.

However we would like all products to be in the shop and ad an inquiry lead form in case no price is available.

Read More

Since I never programmed with WooCommerce I was wondering that is the right hook to implement such an functionality?

Related posts

Leave a Reply

3 comments

  1. Had the exact same issue and couldn’t find a plugin or a solution anywhere so I figured a workaround myself:

    You need to edit file
    /wp-content/themes/your-theme-name/woocommerce/single-product/add-to-cart/simple.php
    (if it’s not there just copy it from woocommerce plugin
    /wp-content/plugins/woocommerce/templates/single-product/add-to-cart/simple.php)

    and on line 14 where it says

        if ( ! $product->is_purchasable() ) return;
    

    you need to comment it out and write something like

        if ( ! $product->is_purchasable() ) {
        // put your code here 
        return; 
        }   
    

    and in the //put your code here line you can enter for example a shortcode for a form or a more complicated solution would be to put code for a button that when clicked will open up a popup form.

    Still working on that 😉

  2. Maybe too late, but I have had same issue currently.

    Here is there code, Woocommerce uses to check if a product can be purchased:
    https://github.com/woocommerce/woocommerce/blob/master/includes/abstracts/abstract-wc-product.php#L1404

    Notice about: && '' !== $this->get_price()

    /**
     * Returns false if the product cannot be bought.
     *
     * @return bool
     */
    public function is_purchasable() {
        return apply_filters( 'woocommerce_is_purchasable', $this->exists() && ( 'publish' === $this->get_status() || current_user_can( 'edit_post', $this->get_id() ) ) && '' !== $this->get_price(), $this );
    }
    

    So you need to write a filter like this to override default:

    add_filter( 'woocommerce_is_purchasable', function ( $is_purchasable, $product ) {
        return $product->exists() && ( 'publish' === $product->get_status() || current_user_can( 'edit_post', $product->get_id() ) );
    }, 10, 2 );
    
  3. Try the following code snippet. You just have to put it in your functions.php. You don’t need a plugin or to overwrite WooCommerce files in your child theme.

    // Inquiry link if no price available
    function add_inquiry_link_instead_price( $price, $product ) {
        if ( '' === $product->get_price() || 0 == $product->get_price() ) :
            return '<a href="'.get_permalink( $any_custom_page_id ).'" class="single_add_to_cart_button button alt">'.__( 'Jetzt anfragen' ).'</a>';
        endif;
    }
    add_filter( 'woocommerce_get_price_html', 'add_inquiry_link_instead_price', 100, 2 );