Skip to content
Advertisement

Set WooCommerce billing postcode from product custom input field for guests

I am trying to add an input field which allows the customer (guest) to enter their postcode on the product page before ‘add to cart’ which then sets/prepopulates the postcode in the basket & checkout without them having to enter their details or login/create an account.

I’ve seen some resources saying the shipping calculator on the basket page is complicated to prepopulate, but I’m unfamiliar with how the shipping calculator works. Is this true?

Based on Add a product note field in single product pages in Woocommerce answer code, I have successfully managed to achieve passing the postcode from the product page to the checkout page using the following code which allows a guest to add their postcode to the product page, and the checkout correctly shows the guest postcode with the shipping rates correctly showing:

JavaScript

While this works for the checkout page, does anyone have any knowledge of how to implement this with the cart/basket shipping calculator?

Advertisement

Answer

You don’t need any Javascript code and neither to set that “delivery postcode” as custom cart item data. Simply use the following simplified code replacement, that will allow guests to set their “delivery postcode” from product page:

The code

JavaScript

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

Note: This “delivery postcode” field is only displayed to guests. Once the “delivery postcode” has ben submitted once, the field is not displayed anymore.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement