Jump to content

Forcing user to select a state


Guest kmdev

Recommended Posts

Rather than having a default state selection (e.g. Alabama) these changes make the default state one that is not valid, forcing the customer to choose one. Avoids the issue where customers may not select a state, just leaving it at Alabama regardless of where they actually live.

CAVEAT: I am by no means an expert in PHP or CubeCart (having been using both for less than 2 weeks...) but this solution is something that has worked for me. Use at your own risk!!

It is a multi-step solution, as follows:

Step 1:

Create a new zone using the admin control panel. Go to the 'Settings, Countries/Zones' page, and then click on the 'Add Zone' tab.

Set the Zone Country to United States, and enter the name as something that will alphabetically appear before Alabama, something like ---select--- would do it.

Set the abbreviation to be -- (two dashes) and save this zone.

Step 2:

Edit the file functions.inc.php in the includes directory. Find the function state_json and make the following two changes.

First change is to change the sort order of the states from the order that they were added to the database (so new entries always appear last) to actually being sorted by name.

change:

$counties = $GLOBALS['db']->select('CubeCart_geo_zone');


to:


$counties = $GLOBALS['db']->select('CubeCart_geo_zone',false,false,'name');




Now change the part that assembles the state list so that any invalid states (those with an abbreviation that is two dashes) so that they don't have any ID associated with them.



change:


foreach ($counties as $state) {

        $json_array[getCountryFormat($state['country_id'], 'id', 'numcode')][] = array('id' => $state['id'], 'name' => $state['name']);

}


to:


foreach ($counties as $state) {

        if (strcmp($state['abbrev'],'--')==0) {

                $state['id']='';

        }

        $json_array[getCountryFormat($state['country_id'], 'id', 'numcode')][] = array('id' => $state['id'], 'name' => $state['name']);

}




Step 3:

In whichever skin you are using, locate the file content.checkout.confirm.php (e.g. /skins/kurouto/templates/content.checkout.confirm.php ) and change the two state selectors to be handled as required fields. This is done by changing the class from "textbox" to "textbox required". The two selectors are for the billing and delivery address sections.



change:


<div><label for="state-list">{$LANG.address.state}</label></span><input type="text" name="billing[state]" id="state-list" class="textbox" value="{$BILLING.state}" /> *</span></div>


and


<div><label for="delivery_state">{$LANG.address.state}</label></span><input type="text" name="delivery[state]" id="delivery_state" class="textbox" value="{$DELIVERY.state}" /> *</span></div>




to:


<div><label for="state-list">{$LANG.address.state}</label></span><input type="text" name="billing[state]" id="state-list" class="textbox required" value="{$BILLING.state}" /> *</span></div>


and


<div><label for="delivery_state">{$LANG.address.state}</label></span><input type="text" name="delivery[state]" id="delivery_state" class="textbox required" value="{$DELIVERY.state}" /> *</span></div>

Step 4:

Empty your cache folder to force the system to reload the zones from the database and rebuild the zone selection list.

This works (well, it does for me...) by making the new zone have no ID associated with it, and by marking the input field as 'required' it won't allow the field to be empty. By not having any ID for the new zone, that field is viewed as empty if the zone is selected. Changing the sort order for the zones to be alphabetical ensures that this new zone is the first in the list, and therefore the default zone. This forces the user to select a different zone before being able to complete the purchase.

As mentioned at the top - I am new to all of this, and there may be a better way of doing this but I got it working this way. Please make sure you backup everything before even thinking about using this!

Link to comment
Share on other sites

These changes were done in response to this question on another topic.

As mentioned, I am new to all of this so my solution may not be the best way of doing this, but it seems to work.

I should also point out that as these are changes to files that shipped as part of CubeCart, any updates/upgrades etc could easily overwrite these files meaning you will have to make the same (or similar) changes again in order to keep this working.

Link to comment
Share on other sites

These changes were done in response to this question on another topic.

As mentioned, I am new to all of this so my solution may not be the best way of doing this, but it seems to work.

I should also point out that as these are changes to files that shipped as part of CubeCart, any updates/upgrades etc could easily overwrite these files meaning you will have to make the same (or similar) changes again in order to keep this working.

Thanks a lot for your solution! With all your caveats - I'll hold off on trying it to give someone knowledgeable a chance to comment on this. I had actually tried to add a "*Choose" zone to the geo-zone table myself, but couldn't get it to show as the default and didn't know how to keep it from being accepted by the code as a real place. Having to change so many core files is a problem with your method. If you don't mind, I'd like to request this Feature in the Bug Reports and refer them to your solution. Maybe they can add it to the next release???

Link to comment
Share on other sites

Thanks a lot for your solution! With all your caveats - I'll hold off on trying it to give someone knowledgeable a chance to comment on this. I had actually tried to add a "*Choose" zone to the geo-zone table myself, but couldn't get it to show as the default and didn't know how to keep it from being accepted by the code as a real place. Having to change so many core files is a problem with your method. If you don't mind, I'd like to request this Feature in the Bug Reports and refer them to your solution. Maybe they can add it to the next release???

More than happy for this to be used in any way that helps, and for this to be a possible hint to anyone as a way to get this working then that's great.

I thought it was only best to make all the caveats etc clear on this - given that I'm new to all of this and it only took about 45 minutes mucking about in the code to get it working I'm sure it can't be the best solution! Perhaps it's possible to do this using hooks and the like, but I've no idea how to do any of that yet.

An ideal solution obviously would be for there to be an option in the admin system to enable/disable the use of '---select---' type options for the state list, and maybe other lists as well like country. Would also have to figure out a way of dealing with countries where the state option is not applicable and should be allowed to be left blank.

Link to comment
Share on other sites

I've been thinking about your fix. Wouldn't the ---select--- zone need to be added to every country that had zone choices show up, not just the US? I found if you make it a required field, and a country does NOT have any zones, it causes a problem.

There has been one note added to the bug report I filed with your solution. They agreed this needed to be implemented in some way.

Link to comment
Share on other sites

I've been thinking about your fix. Wouldn't the ---select--- zone need to be added to every country that had zone choices show up, not just the US? I found if you make it a required field, and a country does NOT have any zones, it causes a problem.

There has been one note added to the bug report I filed with your solution. They agreed this needed to be implemented in some way.

Yep, you would need a '--select--' entry for each country that has zones. For countries that don't have zones defined, it may still be that they have zones in that country and the customer has to type it in - or it may be that zones are not applicable in which case it does not to allow for that field to be blank in those cases.

If all this attempt does is prompt cubecart into implementing this in a way that works with all cases, then that's more than good enough!! If it was a admin side option to make the 'select' option a default for the field, then it probably could be done without needing to have those options in the database - they would just be the first default field in any select box created. There was only one html field on the page for them so I couldn't see how to add the 'required' element for just the drop-down list version and not the text entry version. Again, I'm sure that is something that can be done, I just don't know how.

Link to comment
Share on other sites

  • 4 weeks later...

I've been thinking about your fix. Wouldn't the ---select--- zone need to be added to every country that had zone choices show up, not just the US? I found if you make it a required field, and a country does NOT have any zones, it causes a problem.

There has been one note added to the bug report I filed with your solution. They agreed this needed to be implemented in some way.

Yep, you would need a '--select--' entry for each country that has zones. For countries that don't have zones defined, it may still be that they have zones in that country and the customer has to type it in - or it may be that zones are not applicable in which case it does not to allow for that field to be blank in those cases.

If all this attempt does is prompt cubecart into implementing this in a way that works with all cases, then that's more than good enough!! If it was a admin side option to make the 'select' option a default for the field, then it probably could be done without needing to have those options in the database - they would just be the first default field in any select box created. There was only one html field on the page for them so I couldn't see how to add the 'required' element for just the drop-down list version and not the text entry version. Again, I'm sure that is something that can be done, I just don't know how.

There's a bug fix listed for this, but I've tried it on 2 stores, and it didn't work properly for me. How about giving it a try to see if it works for you???

3094

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...