Search code examples
phpwordpresswoocommercecarthook-woocommerce

Replace WooCommerce cart shipping section with a custom text message


I already cleared billing fields on checkout page, but unfortunately they are still remembered on cart page.

add_filter( 'woocommerce_checkout_get_value', 'reigel_empty_checkout_billing_fields', 10, 2 );

function reigel_empty_checkout_billing_fields( $value, $input ) {

    if ( in_array( $input, $billing_fields ) ) {
        $value = '';
    }

    return $value;
}

I just want to display on cart page an information, that the shipping costs will show in the next step after filling shipping fields. Can anybody help me with it?


Solution

  • The following will remove the shipping section on cart, replacing it by a custom message:

    // Remove "shipping" section from cart page only
    add_filter( 'woocommerce_cart_needs_shipping', 'filter_cart_needs_shipping' );
    function filter_cart_needs_shipping( $needs_shipping ) {
        return is_cart() ? false : $needs_shipping;
    }
    
    // Add a custom shipping message row
    add_action( 'woocommerce_cart_totals_before_order_total', 'cart_custom_shipping_message_row' );
    function cart_custom_shipping_message_row() {
        if ( ! WC()->cart->needs_shipping() ) :
    
        $shipping_message = __("Costs will be calculated on next step.", "woocommerce");
        ?>
        <tr class="shipping">
            <th><?php _e( 'Shipping', 'woocommerce' ); ?></th>
            <td class="message" data-title="<?php esc_attr_e( 'Shipping', 'woocommerce' ); ?>"><em><?php echo $shipping_message; ?></em></td>
        </tr>
        <?php endif;
    }
    

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

    enter image description here