How to prevent a customer from entering PO Box address as a shipping address in checkout

Some products can’t be shipped to PO Boxes so we can prevent the customers to entering shipping address as PO Box address before placing order.

Entrypoint(JavaScript):

_.extend(OrderWizardModuleAddress.prototype, {
					events: _.extend(OrderWizardModuleAddress.prototype.events, {

						'click [data-action="select"]': 'selectAddress',
					}),
					selectAddress: function(e) {
						jQuery('.wizard-content .alert-error').hide();
						// Grabs the address id and sets it to the model
						// on the position in which our sub class is managing (billaddress or shipaddress)
						var selectedAddr1 = this.addresses._byId[jQuery(e.target).data('id').toString()].attributes.addr1;
						var poBoxPattern = /^ *((#\d+)|((box|bin)[-. \/\\]?\d+)|(.*p[ \.]? ?(o|0)[-. \/\\]? *-?((box|bin)|b|(#|n|num|number)?\d+))|(p(ost|ostal)? *(o(ff(ice)?)?)? *((box|bin)|b)? *(#|n|num|number)*\d+)|(p *-?\/?(o)? *-?box)|post office box|((box|bin)|b) *(#|n|num|number)? *\d+|(#|n|num|number) *\d+)/i;
						if (this.$el[0].attributes[0].value.indexOf('OrderWizard.Module.Address.Shipping')> -1) {
							if (poBoxPattern.test(selectedAddr1)) {
								const global_view_message = new GlobalViewsMessageView({
									message: 'You cannot select a PO Box address as Shipping Address',
									type: 'error',
									closable: true
								});
								this.$('[data-type="alert-placeholder-module"]').html(
									global_view_message.render().$el.html()
								);
						
								this.error = null;
								$("html, body").animate({
									scrollTop: $('[data-type="alert-placeholder-module"]').offset().top - 70
	
								}, 10);
							} else {
								this.setAddress(
									jQuery(e.target)
									.data('id')
									.toString()
									);
									
									// re render so if there is changes to be shown they are represented in the view
									this.render();
									
									// As we already set the address, we let the step know that we are ready
									this.trigger('ready', true);

							}
						} else {
							this.setAddress(
								jQuery(e.target)
								.data('id')
								.toString()
								);
								
								// re render so if there is changes to be shown they are represented in the view
								this.render();
								
								// As we already set the address, we let the step know that we are ready
								this.trigger('ready', true);
						}
						},
					getContext: _.wrap(OrderWizardModuleAddress.prototype.getContext, function (fn) {
						var original_Ret = fn.apply(this, _.toArray(arguments).slice(1));
						var selectedAddr1 = this.address.attributes.addr1;
						var poBoxPattern = /^ *((#\d+)|((box|bin)[-. \/\\]?\d+)|(.*p[ \.]? ?(o|0)[-. \/\\]? *-?((box|bin)|b|(#|n|num|number)?\d+))|(p(ost|ostal)? *(o(ff(ice)?)?)? *((box|bin)|b)? *(#|n|num|number)*\d+)|(p *-?\/?(o)? *-?box)|post office box|((box|bin)|b) *(#|n|num|number)? *\d+|(#|n|num|number) *\d+)/i;
						if (this.$el[0].attributes[0].value.indexOf('OrderWizard.Module.Address.Shipping')> -1) {
							if (poBoxPattern.test(selectedAddr1)) {
								setTimeout(() => {
									const global_view_message = new GlobalViewsMessageView({
											message: 'You cannot select a PO Box address as Shipping Address',
											type: 'error',
											closable: true
										});
										this.$('[data-type="alert-placeholder-module"]').html(
												global_view_message.render().$el.html()
									);
									
									this.error = null;
									$('#billing_address_continue_button').attr('disabled', true);
									$("html, body").animate({
										scrollTop: $('[data-type="alert-placeholder-module"]').offset().top - 70
		
									}, 10);
								}, 250);
								
							} else {
								$('#billing_address_continue_button').attr('disabled', false);
								
							}
						}
						original_Ret.ShippingVariable = this.wizard.selectedShipping;
						original_Ret.BillingVariable = this.wizard.selectedBilling;

						return original_Ret;
					})
				});

Results:

Leave a comment

Your email address will not be published. Required fields are marked *