2

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' );
7uc1f3r
  • 28,449
  • 17
  • 32
  • 50
Maged Mohamed
  • 511
  • 5
  • 27

1 Answers1

2

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 );
7uc1f3r
  • 28,449
  • 17
  • 32
  • 50
  • @7ucf3r thank you so much for the help and time .. we have tested another solution can you tell me what is the difference if we just changed in the line if ($total == 0 ) to if ($total == 0 && current_user_can( 'customer' ) ) .. because it worked too .. what is the difference between that and between your answer ? – Maged Mohamed Jun 11 '20 at 19:23
  • 1
    **1)** I am not using the global $woocommerce variable, see: [Stop using `global` in PHP](https://stackoverflow.com/questions/12445972/stop-using-global-in-php). **2)** [current_user_can( string $capability, mixed $args )](https://developer.wordpress.org/reference/functions/current_user_can/) - Returns whether the current user has the specified capability – 7uc1f3r Jun 11 '20 at 19:28
  • So its much better to use your solution rather than mine right ?, also why do you say at the begining of the answer: Output the Payment Methods on the checkout. – Maged Mohamed Jun 11 '20 at 19:34
  • 1
    because the hook is in the `woocommerce_checkout_payment()` function, if you click on the link this should be clear, also see: https://docs.woocommerce.com/wc-apidocs/function-wc_get_template.html – 7uc1f3r Jun 11 '20 at 19:36
  • Yes but in the code the hook woocommerce_checkout_payment() didn't used .. right ? – Maged Mohamed Jun 11 '20 at 19:42