Hide WooCommerce Displayed Shipping Cost When Local Pickup is Selected

66 Views Asked by At

If i select Local Pickup in the woocommerce cart page the shipping costs on the right sides are confusing my customers. Any idea how to hide ONLY the amount if Local Pickup is checked?

enter image description here

Here the current HTML of the shipping Checkboxes:

    <tr class="woocommerce-shipping-totals shipping">
    <th>Shipping</th>
        <td data-title="Shipping" colspan="2">
        <p class="nm-shipping-th-title">Shipping</p>
            <ul id="shipping_method" class="woocommerce-shipping-methods">
                <li>
                <input type="radio" name="shipping_method[0]" data-index="0" id="shipping_method_0_flat_rate1" value="flat_rate:1" class="shipping_method"><label for="shipping_method_0_flat_rate1">Shipping<span class="woocommerce-Price-amount amount"><bdi>15&nbsp;<span class="woocommerce-Price-currencySymbol">TL</span></bdi></span></label>   
                </li>
                <li>
                <input type="radio" name="shipping_method[0]" data-index="0" id="shipping_method_0_local_pickup8" value="local_pickup:8" class="shipping_method" checked="checked"><label for="shipping_method_0_local_pickup8">PickUp<span class="woocommerce-Price-amount amount"><bdi>1&nbsp;<span class="woocommerce-Price-currencySymbol">TL</span></bdi></span></label>
                </li>
            </ul>
        </td>
    </tr>
1

There are 1 best solutions below

0
On BEST ANSWER

You can use the following filter hook, to remove displayed costs from other shipping methods, when Local Pickup is the selected shipping method:

add_filter( 'woocommerce_cart_shipping_method_full_label', 'custom_cart_shipping_method_full_label', 10, 2 );
function custom_cart_shipping_method_full_label($label, $method){
    $chosen_methods = WC()->session->get('chosen_shipping_methods');
    $chosen_method = explode(':', reset($chosen_methods) );
    $chosen_method = reset($chosen_method); // Get the chosen shipping method

    if ( $method->get_method_id() !== 'local_pickup' && $chosen_method === 'local_pickup' && $method->cost > 0 ) {
        $label = $method->get_label();
    }
    return $label;
}

Code goes in functions.php file of your child theme (or in a plugin). Tested and works.


To remove all displayed shipping costs, even for Local Pickup, when Local Pickup is selected use instead the following:

add_filter( 'woocommerce_cart_shipping_method_full_label', 'custom_cart_shipping_method_full_label', 10, 2 );
function custom_cart_shipping_method_full_label($label, $method){
    $chosen_methods = WC()->session->get('chosen_shipping_methods');
    $chosen_method = explode(':', reset($chosen_methods) );
    $chosen_method = reset($chosen_method); // Get the chosen shipping method

    if ( $chosen_method === 'local_pickup' && $method->cost > 0 ) {
        $label = $method->get_label();
    }
    return $label;
}