最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

woocommerce offtopic - Combine one action and one filter

programmeradmin3浏览0评论

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;
}

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;
}
Share Improve this question asked Nov 29, 2020 at 18:16 franz877franz877 312 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

the filter function must be inside the action function, because if it isnt it is not recognize. So I think. At least with variables is like this.

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;
    }
}
发布评论

评论列表(0)

  1. 暂无评论