Combine one action and one filter

Question

I want to show an error message in Woocommerce checkout page, and remove the order button.
I can use one action and one filter:

add_action( 'woocommerce_before_checkout_form', 'add_checkout_error', 9 );
function add_checkout_error() {
    wc_print_notice( __( 'An error message.', 'woocommerce' ), 'error' );
}

add_filter('woocommerce_order_button_html', 'remove_order_button_html' );
function remove_order_button_html( $button ) {
    $button = '';
    return $button;
}

How can I combine them? If I put the filter inside the action function, it doesn’t work.

add_action( 'woocommerce_before_checkout_form', 'add_checkout_error', 9 );
function add_checkout_error() {

    $error=1;
    if ($error==1){
    wc_print_notice( __( 'An error message.', 'woocommerce' ), 'error' );
    add_filter('woocommerce_order_button_html', 'remove_order_button_html' );
    }
}

function remove_order_button_html( $button ) {
    $button = '';
    return $button;
}
0
franz877 2 months 0 Answers 8 views 0

Leave an answer