Robert Bavington
Robert Bavington

Reputation: 43

Woocommerce Return False with message

I am using a function to remove the ability to purchase a product if its price is equal to, or greater than £50,000.

I would like to add a message where the add to cart button usually is, to say "this product is over £50,000 - please contact us"

add_filter( 'woocommerce_is_purchasable', 'disable_cart_for_over_fifty_k', 10, 2 );

function disable_cart_for_over_fifty_k( $purchasable, $product ) {
    if( $product->get_price() >= 50000 )
        return false;
}

Upvotes: 0

Views: 718

Answers (2)

helgatheviking
helgatheviking

Reputation: 26319

You can display a message on any of the action hooks.

add_action( 'woocommerce_single_product_summary', 'so_contact_notice_30876596', 25 );
function so_contact_notice_30876596(){
    global $product;
    if( $product->get_price() >= 50000 ){
        echo "this product is over £50,000 - please contact us";
    }
}

Upvotes: 1

Spencer Wieczorek
Spencer Wieczorek

Reputation: 21565

You can have a $message variable that is empty for true and contains a messages (in HTML) for false:

PHP

$message = "";
...
function disable_cart_for_over_fifty_k( $purchasable, $product ){
    global $message;
    if( $product->get_price() >= 50000 ) {
        $message = "<p class='errorMessage'>This product is over £50,000 - please contact us</p>";
        return false;
    }
}

HTML (example)

...
<?php echo $message; ?>
<button id="addCart">Add Cart</button>
...

You can also use a flag and choose what to echo if you want to hide "Add Cart" all together.

Upvotes: 0

Related Questions