{"id":19,"date":"2019-11-06T21:47:11","date_gmt":"2019-11-07T03:47:11","guid":{"rendered":"https:\/\/travisolson.net\/code\/?p=19"},"modified":"2019-11-06T22:02:06","modified_gmt":"2019-11-07T04:02:06","slug":"prevent-po-boxes-in-woocommerce-shipping-addresses","status":"publish","type":"post","link":"https:\/\/www.travisolson.net\/code\/prevent-po-boxes-in-woocommerce-shipping-addresses\/","title":{"rendered":"Prevent PO Boxes in WooCommerce Shipping Addresses"},"content":{"rendered":"<p>In some cases PO Boxes are not allowed when shipping certain products or when using specific carriers.\u00a0 The code below disables PO Boxes for <strong>all<\/strong> shipping addresses.\u00a0 You can add additional logic if you only need to disallow PO Boxes in certain cases.<\/p>\n<p>Create a function for checking if a string is a PO Box. This uses a regular expression that should catch most of the possible formats that a customer would use, but of course there&#8217;s always some edge case that may not be caught.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">function myprefix_is_po_box($s) {\r\n  return preg_match(\"\/^\\s*((P(OST)?.?\\s*(O(FF(ICE)?)?)?.?\\s+(B(IN|OX))?)|B(IN|OX))\/i\", $s);\r\n}<\/pre>\n<p>Now create a function that adds extra validation to the WooCommerce registration form. In the example below, it is showing an error message specific to a winery since alcohol is not able to be sent to a PO Box.\u00a0 This function can be expanded to include any other custom validations you want to add for the registration form, such as custom password restrictions or required fields.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">function myprefix_validate_extra_register_fields( $username, $email, $validation_errors ) {\r\n  $errors = array();\r\n  \r\n  foreach(array('shipping_address_1', 'shipping_address_2') as $fld) {\r\n    if (myprefix_is_po_box($_POST[$fld])) {\r\n      $errors[] = 'A PO Box address was detected in the shipping address. Wine cannot be shipped to PO Boxes.';\r\n    }\r\n  }\r\n  \r\n  if ( !empty ($errors) ) {\r\n    $validation_errors-&gt;add( 'custom_field_errors', 'Please correct the following issues:&lt;ul&gt;&lt;li&gt;' . implode('&lt;\/li&gt;&lt;li&gt;', $errors) . '&lt;\/li&gt;&lt;\/ul&gt;' );\r\n  }\r\n}\r\nadd_action( 'woocommerce_register_post', 'myprefix_validate_extra_register_fields', 10, 3 );<\/pre>\n<p>The last thing is to create a function to perform the same validations when a user edits their shipping address for their account.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">function myprefix_woocommerce_after_save_address_validation($user_id, $load_address, $address) {\r\n  if (isset($_POST['action']) &amp;&amp; $_POST['action'] == 'edit_address') {\r\n    if (isset($_POST['shipping_address_1']) &amp;&amp; myprefix_is_po_box($_POST['shipping_address_1'])) {\r\n      wc_add_notice('A PO Box address was detected in the shipping address. Wine cannot be shipped to PO Boxes.', 'error');\r\n    }\r\n    if (isset($_POST['shipping_address_2']) &amp;&amp; myprefix_is_po_box($_POST['shipping_address_2'])) {\r\n      wc_add_notice('A PO Box address was detected in the shipping address. Wine cannot be shipped to PO Boxes.', 'error');\r\n    }\r\n  }\r\n}\r\nadd_action( 'woocommerce_after_save_address_validation', 'myprefix_woocommerce_after_save_address_validation', 10, 3);<\/pre>\n<p>That is all that is required to prevent adding a PO Box to a customer&#8217;s address.\u00a0 Additional validations will be necessary during the checkout process if your shipping service doesn&#8217;t already perform those validations.\u00a0 If you&#8217;re shipping with FedEx, for example, it will automatically fail validation as long as you have properly indicated that your order contains alcohol.\u00a0 If you need to do custom validation during checkout you can use the <code class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">woocommerce_checkout_process<\/code> action hook.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In some cases PO Boxes are not allowed when shipping certain products or when using specific carriers.\u00a0 The code below disables PO Boxes for all shipping addresses.\u00a0 You can add additional logic if you only need to disallow PO Boxes in certain cases. Create a function for checking if a string is a PO Box&#8230;.  <a class=\"excerpt-read-more\" href=\"https:\/\/www.travisolson.net\/code\/prevent-po-boxes-in-woocommerce-shipping-addresses\/\" title=\"Read Prevent PO Boxes in WooCommerce Shipping Addresses\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,2],"tags":[],"class_list":["post-19","post","type-post","status-publish","format-standard","hentry","category-woocommerce","category-wordpress"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Prevent PO Boxes in WooCommerce Shipping Addresses - Travis Olson&#039;s Code Snippets<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.travisolson.net\/code\/prevent-po-boxes-in-woocommerce-shipping-addresses\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Prevent PO Boxes in WooCommerce Shipping Addresses - Travis Olson&#039;s Code Snippets\" \/>\n<meta property=\"og:description\" content=\"In some cases PO Boxes are not allowed when shipping certain products or when using specific carriers.\u00a0 The code below disables PO Boxes for all shipping addresses.\u00a0 You can add additional logic if you only need to disallow PO Boxes in certain cases. Create a function for checking if a string is a PO Box.... Read more &raquo;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.travisolson.net\/code\/prevent-po-boxes-in-woocommerce-shipping-addresses\/\" \/>\n<meta property=\"og:site_name\" content=\"Travis Olson&#039;s Code Snippets\" \/>\n<meta property=\"article:published_time\" content=\"2019-11-07T03:47:11+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-11-07T04:02:06+00:00\" \/>\n<meta name=\"author\" content=\"Travis Olson\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Travis Olson\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.travisolson.net\/code\/prevent-po-boxes-in-woocommerce-shipping-addresses\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.travisolson.net\/code\/prevent-po-boxes-in-woocommerce-shipping-addresses\/\"},\"author\":{\"name\":\"Travis Olson\",\"@id\":\"https:\/\/www.travisolson.net\/code\/#\/schema\/person\/0170ab5b2e7a3d88436375cc50336934\"},\"headline\":\"Prevent PO Boxes in WooCommerce Shipping Addresses\",\"datePublished\":\"2019-11-07T03:47:11+00:00\",\"dateModified\":\"2019-11-07T04:02:06+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.travisolson.net\/code\/prevent-po-boxes-in-woocommerce-shipping-addresses\/\"},\"wordCount\":260,\"publisher\":{\"@id\":\"https:\/\/www.travisolson.net\/code\/#\/schema\/person\/0170ab5b2e7a3d88436375cc50336934\"},\"articleSection\":[\"WooCommerce\",\"WordPress\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.travisolson.net\/code\/prevent-po-boxes-in-woocommerce-shipping-addresses\/\",\"url\":\"https:\/\/www.travisolson.net\/code\/prevent-po-boxes-in-woocommerce-shipping-addresses\/\",\"name\":\"Prevent PO Boxes in WooCommerce Shipping Addresses - Travis Olson&#039;s Code Snippets\",\"isPartOf\":{\"@id\":\"https:\/\/www.travisolson.net\/code\/#website\"},\"datePublished\":\"2019-11-07T03:47:11+00:00\",\"dateModified\":\"2019-11-07T04:02:06+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.travisolson.net\/code\/prevent-po-boxes-in-woocommerce-shipping-addresses\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.travisolson.net\/code\/prevent-po-boxes-in-woocommerce-shipping-addresses\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.travisolson.net\/code\/prevent-po-boxes-in-woocommerce-shipping-addresses\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.travisolson.net\/code\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Prevent PO Boxes in WooCommerce Shipping Addresses\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.travisolson.net\/code\/#website\",\"url\":\"https:\/\/www.travisolson.net\/code\/\",\"name\":\"Travis Olson&#039;s Code Snippets\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/www.travisolson.net\/code\/#\/schema\/person\/0170ab5b2e7a3d88436375cc50336934\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.travisolson.net\/code\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/www.travisolson.net\/code\/#\/schema\/person\/0170ab5b2e7a3d88436375cc50336934\",\"name\":\"Travis Olson\",\"logo\":{\"@id\":\"https:\/\/www.travisolson.net\/code\/#\/schema\/person\/image\/\"},\"url\":\"https:\/\/www.travisolson.net\/code\/author\/travis-olson\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Prevent PO Boxes in WooCommerce Shipping Addresses - Travis Olson&#039;s Code Snippets","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.travisolson.net\/code\/prevent-po-boxes-in-woocommerce-shipping-addresses\/","og_locale":"en_US","og_type":"article","og_title":"Prevent PO Boxes in WooCommerce Shipping Addresses - Travis Olson&#039;s Code Snippets","og_description":"In some cases PO Boxes are not allowed when shipping certain products or when using specific carriers.\u00a0 The code below disables PO Boxes for all shipping addresses.\u00a0 You can add additional logic if you only need to disallow PO Boxes in certain cases. Create a function for checking if a string is a PO Box.... Read more &raquo;","og_url":"https:\/\/www.travisolson.net\/code\/prevent-po-boxes-in-woocommerce-shipping-addresses\/","og_site_name":"Travis Olson&#039;s Code Snippets","article_published_time":"2019-11-07T03:47:11+00:00","article_modified_time":"2019-11-07T04:02:06+00:00","author":"Travis Olson","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Travis Olson","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.travisolson.net\/code\/prevent-po-boxes-in-woocommerce-shipping-addresses\/#article","isPartOf":{"@id":"https:\/\/www.travisolson.net\/code\/prevent-po-boxes-in-woocommerce-shipping-addresses\/"},"author":{"name":"Travis Olson","@id":"https:\/\/www.travisolson.net\/code\/#\/schema\/person\/0170ab5b2e7a3d88436375cc50336934"},"headline":"Prevent PO Boxes in WooCommerce Shipping Addresses","datePublished":"2019-11-07T03:47:11+00:00","dateModified":"2019-11-07T04:02:06+00:00","mainEntityOfPage":{"@id":"https:\/\/www.travisolson.net\/code\/prevent-po-boxes-in-woocommerce-shipping-addresses\/"},"wordCount":260,"publisher":{"@id":"https:\/\/www.travisolson.net\/code\/#\/schema\/person\/0170ab5b2e7a3d88436375cc50336934"},"articleSection":["WooCommerce","WordPress"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.travisolson.net\/code\/prevent-po-boxes-in-woocommerce-shipping-addresses\/","url":"https:\/\/www.travisolson.net\/code\/prevent-po-boxes-in-woocommerce-shipping-addresses\/","name":"Prevent PO Boxes in WooCommerce Shipping Addresses - Travis Olson&#039;s Code Snippets","isPartOf":{"@id":"https:\/\/www.travisolson.net\/code\/#website"},"datePublished":"2019-11-07T03:47:11+00:00","dateModified":"2019-11-07T04:02:06+00:00","breadcrumb":{"@id":"https:\/\/www.travisolson.net\/code\/prevent-po-boxes-in-woocommerce-shipping-addresses\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.travisolson.net\/code\/prevent-po-boxes-in-woocommerce-shipping-addresses\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.travisolson.net\/code\/prevent-po-boxes-in-woocommerce-shipping-addresses\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.travisolson.net\/code\/"},{"@type":"ListItem","position":2,"name":"Prevent PO Boxes in WooCommerce Shipping Addresses"}]},{"@type":"WebSite","@id":"https:\/\/www.travisolson.net\/code\/#website","url":"https:\/\/www.travisolson.net\/code\/","name":"Travis Olson&#039;s Code Snippets","description":"","publisher":{"@id":"https:\/\/www.travisolson.net\/code\/#\/schema\/person\/0170ab5b2e7a3d88436375cc50336934"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.travisolson.net\/code\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/www.travisolson.net\/code\/#\/schema\/person\/0170ab5b2e7a3d88436375cc50336934","name":"Travis Olson","logo":{"@id":"https:\/\/www.travisolson.net\/code\/#\/schema\/person\/image\/"},"url":"https:\/\/www.travisolson.net\/code\/author\/travis-olson\/"}]}},"_links":{"self":[{"href":"https:\/\/www.travisolson.net\/code\/wp-json\/wp\/v2\/posts\/19","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.travisolson.net\/code\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.travisolson.net\/code\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.travisolson.net\/code\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.travisolson.net\/code\/wp-json\/wp\/v2\/comments?post=19"}],"version-history":[{"count":4,"href":"https:\/\/www.travisolson.net\/code\/wp-json\/wp\/v2\/posts\/19\/revisions"}],"predecessor-version":[{"id":23,"href":"https:\/\/www.travisolson.net\/code\/wp-json\/wp\/v2\/posts\/19\/revisions\/23"}],"wp:attachment":[{"href":"https:\/\/www.travisolson.net\/code\/wp-json\/wp\/v2\/media?parent=19"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.travisolson.net\/code\/wp-json\/wp\/v2\/categories?post=19"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.travisolson.net\/code\/wp-json\/wp\/v2\/tags?post=19"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}