Maged Mohamed
Maged Mohamed

Reputation: 561

Change order button text on checkout based on user role and cart total in WooCommerce

We need to check in the function below if the user role is customer.

The function below changes text of place order button if the order total is 0, we need it to check if the user role is customer too and the total is 0.

The code we use so far

function mishaa_custom_button_text($button_text) {
    global $woocommerce;
    $total = $woocommerce->cart->total;
    if ($total == 0 ) {
        $button_text = "Submit Registration";
    }
    return $button_text;
} 
add_filter( 'woocommerce_order_button_text', 'mishaa_custom_button_text' );

Upvotes: 2

Views: 712

Answers (1)

7uc1f3r
7uc1f3r

Reputation: 29614

https://github.com/woocommerce/woocommerce/blob/4.1.0/includes/wc-template-functions.php#L2240

  • Output the Payment Methods on the checkout.

You can use wp_get_current_user();

function filter_woocommerce_order_button_text( $button_text ) { 
    // Get cart total
    $cart_total = WC()->cart->get_cart_contents_total();

    // Get current user role
    $user = wp_get_current_user();
    $roles = ( array ) $user->roles;

    // Check
    if ( $cart_total == 0 && in_array( 'customer', $roles ) ) {
        $button_text = __('Submit Registration', 'woocommerce');
    }

    return $button_text;

}
add_filter( 'woocommerce_order_button_text', 'filter_woocommerce_order_button_text', 10, 1 );

Upvotes: 2

Related Questions