skip to Main Content

I need your help. I want to rename the word “Free shipping” option on woocommerce when wholesale checkbox is checked with “Hello” and when it is unchecked I want to display “Hi”. Below is the html I took from the site.

<td data-children-count="1">
        <input type="checkbox" name="wcs_wholesale_customer" id="wcs_wholesale_customer" value="1" checked="checked">Check this option to set this user to receive your wholesale pricing<br>
        <!-- <span class="description"></span> -->
</td>

and this is the function I added on functions.php

function sww_wc_free_shipping_label( $label ) {

  if(isset( $_POST['wcs_wholesale_customer']) && is_cart() && is_checkout() ) {
    $label = 'Hello';
   } else {
        $label = 'Hi';
    }

    return $label;
}
add_filter( 'woocommerce_cart_shipping_method_full_label', 'sww_wc_free_shipping_label', 10, 2 ); 

Please can you check what needs to be changed on these code above?

2

Answers


  1. Chosen as BEST ANSWER

    I finally sort out with the php code below:

    function sww_wc_free_shipping_label( $label ) {
        global $current_user;
        $wholesale_customer = get_user_meta( $current_user->ID, 'wcs_wholesale_customer', true );
    
       if( ( $wholesale_customer ) ) {
    
           if( WC()->cart->subtotal >= 80 ) {
            $label =  'WHOLESALE Shipping & GST to be added at dispatch';     
           } else {
               $label = 'WHOLESALE Shipping & GST to be added at dispatch + Flat rate of $10';
           }
    
       }
    
        return $label;
    }
    add_filter( 'woocommerce_cart_shipping_method_full_label', 'sww_wc_free_shipping_label', 10, 2 ); 
    

    Thanks guys.


  2. This is possible with HTML and CSS. check out what I did here:

    .message {
       display: none;
    }
    input[type=checkbox]:checked ~ .message {
       display: block;
    }
    
    .message2 {
       display: block;
    }
    input[type=checkbox]:checked ~ .message2 {
       display: none;
    }
    <td data-children-count="1">
            <input type="checkbox" name="wcs_wholesale_customer" id="wcs_wholesale_customer" value="1" checked="checked">Check this option to set this user to receive your wholesale pricing<br> <span class="message">hi</span> <span class="message2">hello</span> 
            <!-- <span class="description"></span> -->
    </td>

    As for doing it the PHP way, you can check if the checkbox was checked using code like this:

    <?php
    if( empty($_POST["wcs_wholesale_customer"]) ) { echo "Checkbox was left unchecked."; }
    else { echo "Checkbox was checked."; }
    ?>

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search