free – Show only if certain shipping method is used – PHP-ThrowExceptions

Exception or error:

Certain users get notified that they can get free shipping if they spend $500.
However, we hand deliver our product to locals for free and therefore do not need this notice to show if they are a local customer.
I tried to set up the PHP script to only show if a certain shipping method is being used, but it is not working.

add_action( 'woocommerce_before_cart', 'bbloomer_free_shipping_cart_notice' );

function bbloomer_free_shipping_cart_notice() {

   $min_amount = 500; //change this to your free shipping threshold

   $current = WC()->cart->subtotal;

   if ( is_user_logged_in() && current_user_can('wholesale_user') || current_user_can('wholesale_terms' ) || current_user_can('craft_brewery_user') && $chosen_shipping ='flexible_shipping_7_1' && $current < $min_amount ) {
      $added_text = 'Get free shipping if you order ' . wc_price( $min_amount - $current ) . ' more!';
      $return_to = wc_get_page_permalink( 'shop' );
      $notice = sprintf( '<a href="%s" class="button wc-forward">%s</a> %s', esc_url( $return_to ), 'Continue Shopping', $added_text );
      wc_print_notice( $notice, 'notice' );

How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *