Support » Plugin: WooCommerce » In Cart, hide “Shipping to …” when local pickup selected

  • Resolved pablopaul

    (@pablopaul)


    In our WooCommerce store, customers get confused when they select “Local Pickup” for a product, but then see in their cart that there is text which says, “Shipping to …” followed by their address. We would like to hide that text for local pickup orders.

    I can’t find any specific hooks or filters to accomplish this in the WC docs.

    Similar posts in this forum seemed to be focused on the checkout page, but our checkout page looks fine. Our issue is only with the Cart page.

    Thank you

Viewing 15 replies - 1 through 15 (of 19 total)
  • following

    Here’s something I implemented this morning – you’ll need to inspect your html in the cart if you’re using a different local pickup plugin than we are to see what it is called in the cart session or input name

    I’m hooking onto the woocommerce_after_cart action

    /**
         * Hide the "shipping to city, state zip" on cart if local pickup
         */
        public function remove_shipping_to_cart_local() {
            // part 1: check on page load and hide if local pickup
            $chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
            $chosen_shipping = $chosen_methods[0];
            if ( 0 === strpos( $chosen_shipping, 'local_pickup' ) ) {
                ?>
                <script type="text/javascript">
                    $(document).ready(function () {
                        jQuery('p.woocommerce-shipping-destination').css("display", "none");
                    });
                </script>
                <?php
            }
            ?>
            <script type="text/javascript">
    
                jQuery('form.woocommerce-cart-form').on('change', 'input[name^="shipping_method"]', function () {
                    var val = jQuery('input[name^="shipping_method"]:checked').val(); // If it changed, then it must be the radio options so check the one that's selected
                    if (typeof val !== 'undefined' && typeof val.match !== 'undefined') {
                        if (val.match("^local_pickup")) {
                            jQuery('p.woocommerce-shipping-destination').css("display", "none");
                        }
                    }
                });
    
                jQuery(document).ajaxComplete(function () {
                    if (jQuery('input[name^="shipping_method"]').attr('type') === 'hidden') { // There's only one option so check the hidden input field with the value
                        var val = jQuery('input[name^="shipping_method"]').val();
                    } else { // Otherwise, it must be the radio options so check the one that's selected
                        var val = jQuery('input[name^="shipping_method"]:checked').val();
                    }
                    if (typeof val !== 'undefined' && typeof val.match !== 'undefined') {
                        if (val.match("^local_pickup")) {
                            jQuery('p.woocommerce-shipping-destination').css("display", "none");
                        }
                    }
                });
    
            </script>
            <?php
        }
    Thread Starter pablopaul

    (@pablopaul)

    @dunkoh Thank you for sharing that function. I hooked it into the woocommerce_after_cart action, and it works when the customer selects “Calculate Shipping” or “Change Address”, but it doesn’t run when you add a product and visit the cart. Returning customers, who already have their address stored in the browser cache, still get the “Shipping to…” message.

    • This reply was modified 1 year, 5 months ago by pablopaul.

    @pablopaul depending on what you’re using for local pickup, I recommend on dev or local dev, debugging to see what is coming through in this code by writing output to error log or something. You’ll have to them modify that if statement to account for your local pickup

    $chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
     $chosen_shipping = $chosen_methods[0];
     if ( 0 === strpos( $chosen_shipping, 'local_pickup' ) ) {
    

    That is the code that runs on initial cart page load.

    • This reply was modified 1 year, 5 months ago by dunkoh.
    Thread Starter pablopaul

    (@pablopaul)

    @dunkoh The problem turned out to be the jQuery shorthand dollar sign in $(document).ready which needed to be changed to jQuery(document).ready. With that little edit, your code works perfectly. Thank you!

    dang it! those $ instead of jQuery trip me up every time because I copy stuff over from another project that uses $

    Now I need to go check my custom plugin to see if it’s working right on page load!

    Glad you got it going. I have a similar function that does this same thing but unchecked the “ship to other address” checkbox if local pickup is the only option available.

    I have the same issue of shipping options appearing in the cart for pickup only products. I have put this code into Code Snippets but its making no change. I have also added a line to try to make the form.woocommerce-shipping-calculator not display and have tried making display none!important. My jQuery is rusty but could sure use some help trying to get this to work.
    https://marchestaging.wpengine.com/cart/ is the URL and Party Boards are Local Pickup only. If I am logged in or have previously entered address info, it automatically shows the options/costs.

    Any help would be greatly appreciated! Thanks in advance.

    @richterworks the difference here is that you are using a product that is pickup only I think, instead of where my usage is based on the user’s location triggering the pickup only. What determines the “Party Boards are for in-store pickup only. Please choose your pickup time at checkout.” notice to show? maybe you can get a value from whatever does that and trigger the jQuery changes then?

    Also – are you hooking onto the action in either your functions.php or in a custom plugin?

    I am using a custom plugin so I have this:

    $this->loader->add_action( 'woocommerce_after_cart', $public_class, 'remove_shipping_to_cart_local' );

    if you are putting the code into functions.php you’ll have to call it something like this:

    add_action( 'woocommerce_after_cart', 'remove_shipping_to_cart_local' );

    If you have the code running on cart load, what is the value of $chosen_shipping after it gets defined in these two lines?

    $chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
    $chosen_shipping = $chosen_methods[0]

    Wow, thanks for getting back to me so quickly.
    I am trying to help another web company with this. I know that pickup only products are not given another shipping method but I don’t know what causes that message. I will ask and see if they know. They didn’t create the site either so they may not know.

    I am putting the code into a plugin called Code Snippets which is where I put all my php function changes.

    I tried putting the following

    $this->loader->add_action( ‘woocommerce_after_cart’, $public_class, ‘remove_shipping_to_cart_local’ );

    before the rest of the code snippet above but it threw a fatal error. I dont know what I’m doing wrong.
    As for this part: “If you have the code running on cart load, what is the value of $chosen_shipping after it gets defined in these two lines?” I dont know/understand enough to answer this.

    so in your case using the Code Snippets plugin, after the function, you would call the action like this:
    add_action( 'woocommerce_after_cart', 'remove_shipping_to_cart_local' );

    If you wanted to see the value of the $chosen_shipping variable, you could do this after it – and then check it in the error log:

    error_log(print_r($chosen_shipping,true));

    the value of that on cart page load is what you’d probably want to use in this line instead of ‘local_pickup’ as long as it is related to the local pickup plugin you’re using.
    if ( 0 === strpos( $chosen_shipping, 'local_pickup' ) ) {

    Thanks for your help so far!
    OK so added the hook but still not working. Is there any way to print the $chosen_shipping to screen or console rather than error log (just while troubleshooting and on the dev site)?

    you could echo it to screen

    echo '<pre>';
     echo print_r($chosen_shipping, true);
     echo '</pre>';

    OK so I have added that after the hook after the code, but it is not printing anything to screen. Can you look at this code and see if I messed something up?

    function remove_shipping_to_cart_local() {
            // part 1: check on page load and hide if local pickup
            $chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
            $chosen_shipping = $chosen_methods[0];
            if ( 0 === strpos( $chosen_shipping, 'local_pickup' ) ) {
                ?>
                <script type="text/javascript">
                    jQuery(document).ready(function () {
                        jQuery('p.woocommerce-shipping-destination').css("display", "none");
    					jQuery('form.woocommerce-shipping-calculator').css("display", "none");
                    });
                </script>
                <?php
            }
            ?>
            <script type="text/javascript">
    
                jQuery('form.woocommerce-cart-form').on('change', 'input[name^="shipping_method"]', function () {
                    var val = jQuery('input[name^="shipping_method"]:checked').val(); // If it changed, then it must be the radio options so check the one that's selected
                    if (typeof val !== 'undefined' && typeof val.match !== 'undefined') {
                        if (val.match("^local_pickup")) {
                            jQuery('p.woocommerce-shipping-destination').css("display", "none");
    						jQuery('form.woocommerce-shipping-calculator').css("display", "none");
                        }
                    }
                });
    
                jQuery(document).ajaxComplete(function () {
                    if (jQuery('input[name^="shipping_method"]').attr('type') === 'hidden') { // There's only one option so check the hidden input field with the value
                        var val = jQuery('input[name^="shipping_method"]').val();
                    } else { // Otherwise, it must be the radio options so check the one that's selected
                        var val = jQuery('input[name^="shipping_method"]:hidden').val();
                    }
                    if (typeof val !== 'undefined' && typeof val.match !== 'undefined') {
                        if (val.match("^local_pickup")) {
                            jQuery('p.woocommerce-shipping-destination').css("display", "none");
    						jQuery('form.woocommerce-shipping-calculator').css("display", "none");
                        }
                    }
                });
    
            </script>
            <?php
        }
    add_action('woocommerce_after_cart', 'remove_shipping_to_cart_local');
    echo '<pre>';
     echo print_r($chosen_shipping, true);
     echo '</pre>';

    Thanks again!

    you’ll want to add the echo’s right after this line where the value of $chosen_shipping is set

    function remove_shipping_to_cart_local() {
            // part 1: check on page load and hide if local pickup
            $chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
            $chosen_shipping = $chosen_methods[0];

    I believe it resolved. At what point I am not sure, it just was suddenly working. I think there may have been a server cache issue.
    The chosen_shipping was local_pickup:7, which also didn’t work any better. After I put it back to local_pickup it finally worked.
    Client has yet to test with a user login (as opposed to admin login), but I think we’re good. Thank you for all the patience and help.

Viewing 15 replies - 1 through 15 (of 19 total)
  • The topic ‘In Cart, hide “Shipping to …” when local pickup selected’ is closed to new replies.