Page 2 of 5

Re: [MOD] GeoZone - Add All Countries Button

Posted: Tue Sep 14, 2010 3:18 am
by Qphoria
it needs to loop 280ish times for all the countries. Define crash? it should sit for about 30 secs unresponsive and then come back

Re: [MOD] GeoZone - Add All Countries Button

Posted: Tue Sep 14, 2010 4:08 am
by PaulineNMU
In Firefox it looked like it was just looping and looping forever.. a solid 5 minutes at least before I force-quit it cause Firefox was bogged down so badly I couldn't do anything. The scroll bar on the side just kept getting smaller and smaller.

In IE it just hung... I had to End Task to get it to stop.

Again, this was using the original code you posted at the beginning of the thread. With the updated code for 1.4.8 nothing happens at all, just a script error.

--

So, I tried it again and let it sit for ten minutes. One core on the processor is working hard bouncing between 85-95%, but again, it doesn't finish. I attached a screenshot (for what it's worth). If I try to do anything in firefox while it's running the script FF is really sluggish to respond (3-5 seconds) but it did let me close the tab when I finally stopped it.

--

I'm assuming the way it's supposed to work is, it adds them all in, then the list collapses back down when it's done with "all zones" set to each country? Maybe it's just my computer.. who knows?

Re: [MOD] GeoZone - Add All Countries Button

Posted: Tue Sep 14, 2010 4:12 am
by Qphoria
the zones field shouldn't get that big

Re: [MOD] GeoZone - Add All Countries Button

Posted: Tue Sep 14, 2010 4:19 am
by PaulineNMU
Lol.. no, I didn't think so.. But I'm not sure why it's doing that. I'm afraid my javascript skills are weak.

Here's what I have, maybe I made a mistake:

Line 62:

Code: Select all

            <td class="left"><a onclick="addGeoZone();" class="button"><span><?php echo $button_add_geo_zone; ?></span></a><a onclick="addAllGeoZones();" class="button"><span class="button_left button_insert"></span><span class="button_middle">Add All Countries</span><span class="button_right"></span></a></td>
Line 104 (the end):

Code: Select all

<script type="text/javascript">
function addAllGeoZones() {
    <?php foreach ($countries as $k => $v) { ?>
    html  = '<table class="green" id="zone_to_geo_zone_row' + zone_to_geo_zone_row + '">';
    html += '<tr>';
    html += '<td><?php echo $entry_country; ?><br /><select name="zone_to_geo_zone[' + zone_to_geo_zone_row + '][country_id]" id="country' + zone_to_geo_zone_row + '" onchange="$(\'#zone' + zone_to_geo_zone_row + '\').load(\'index.php?route=localisation/geo_zone/zone&country_id=\' + this.value + \'&zone_id=0\');">';
    <?php foreach ($countries as $country) { ?>
    html += '<option value="<?php echo $country['country_id']; ?>" <?php echo (($country['country_id']) == $k+1) ? 'selected="selected"' : ''; ?>><?php echo addslashes($country['name']); ?></option>';
    <?php } ?>   
    html += '</select></td>';
    html += '<td rowspan="2"><a onclick="$(\'#zone_to_geo_zone_row' + zone_to_geo_zone_row + '\').remove();" class="button"><span class="button_left button_delete"></span><span class="button_middle"><?php echo $button_remove; ?></span><span class="button_right"></span></a></td>';
    html += '</tr>';
    html += '<tr>';
    html += '<td><?php echo $entry_zone; ?><br /><select name="zone_to_geo_zone[' + zone_to_geo_zone_row + '][zone_id]" id="zone' + zone_to_geo_zone_row + '"></select></td>';
    html += '</tr>';
    html += '</table>';
    $('#zone_to_geo_zone').append(html);
       
    $('#zone' + zone_to_geo_zone_row).load('index.php?route=localisation/geo_zone/zone&country_id=' + $('#country' + zone_to_geo_zone_row).attr('value') + '&zone_id=0');
   
    zone_to_geo_zone_row++;
    <?php } ?>
}
</script>
The add button code didn't match up perfectly, but it didn't seem to me that the SPANs should matter much, they just make the button aligned weird. But maybe that's breaking it?

I appreciate the help! :)

Re: [MOD] GeoZone - Add All Countries Button

Posted: Sat Sep 18, 2010 3:50 am
by IBBoard
Wow, that's a horrendous amount of AJAX requests. I just added this to my wife's shop and even on a Core i5 with 4GB RAM in Chromium (supposedly one of the faster JS browsers) it hammers a processor for minutes or more. I tried stripping out the loop to generate the lists and swapped to a hidden field and text for the country name, but it looks like the crippling delay is in its AJAX requests to populate the zones for each country.

Other than assuming "all zones" and hard-coding that as well then I'm not sure there is much that you can do. Even then, that'll only work the first time that you click the button. After that then if you ever accidentally edit the "Rest of World" zone then you're still going to be hit by the 200+ AJAX requests and the old grinding slowness.

I knew there was a reason that I thought this reliance on Ajax was bad :\ Thank goodness for Chromium keeping tabs in separate processes so that the entire browser doesn't die!




[Edit]
Okay, to be a bit more constructive, here is my hackish version. Your browser is still toast the second you go to edit the group, but that's unavoidable because of the architecture of OpenCart. You have been warned! As it is I appear to be safe, because PHP hits its 64MB memory limit first :choke:

The only limitation of this implementation is that it assumes "All Zones" for all nations, but given the purpose then hopefully it'll work for most people :)

Find:

Code: Select all

<a onclick="addGeoZone();" class="button"><span><?php echo $button_utton_add_geo_zone; ?></span></a>
After, add:

Code: Select all

<a onclick="addAllGeoZones();" class="button"><span>Add All Countries</span></a>
(This fixes the button styling)

Find:

Code: Select all

function addGeoZone() {
Before, add:

Code: Select all

function addAllGeoZones() {
<?php foreach ($countries as $k => $v) { ?>
    html  = '<tbody id="zone_to_geo_zone_row' + zone_to_geo_zone_row + '">';
    html += '<tr>';
    html += '<td><?php echo $entry_country; ?><br /><input type="hidden" name="zone_to_geo_zone[' + zone_to_geo_zone_row + '][country_id]" id="country' + zone_to_geo_zone_row + '" value="<?php echo $v['country_id']; ?>" /><?php echo addslashes($v['name']); ?></td>';
    html += '<td><?php echo $entry_zone; ?><br /><input type="hidden" name="zone_to_geo_zone[' + zone_to_geo_zone_row + '][zone_id]" id="zone' + zone_to_geo_zone_row + '" value="0"/>All Zones</td>';
    html += '<td rowspan="2"><a onclick="$(\'#zone_to_geo_zone_row' + zone_to_geo_zone_row + '\').remove();" class="button"><span><?php echo $button_remove; ?></span></a></td>';
    html += '</tr>';
    
    html += '</tbody>';

    $('#zone_to_geo_zone').append(html);
    $('#zone' + zone_to_geo_zone_row).load('index.php?route=localisation/geo_zone/zone&country_id=' + $('#country' + zone_to_geo_zone_row).attr('value') + '&zone_id=0');
   
    zone_to_geo_zone_row++;
    <?php } ?>
}
Hopefully that helps some people, it's just a shame that the OpenCart architecture can't handle it. Maybe we need negative GeoZones to say "all countries except X, Y and Z" :D

Re: [MOD] GeoZone - Add All Countries Button

Posted: Sat Sep 18, 2010 4:00 am
by Qphoria
yea.. it was just a hacky bandaid that obviously shows the need for a proper fix. I will make this a priority to work on

Re: [MOD] GeoZone - Add All Countries Button

Posted: Sat Sep 18, 2010 9:31 pm
by IBBoard
It requires more work, but what I thought about last night after my post was that you could stick with the AJAX but only use it when necessary. Basically, if you make everything show a string on edit and mass add, with the drop-down for a single add or when you click on an "Edit" link then you'd get the AJAX functionality and still be quick and not memory hungry. Hopefully that makes sense :)

Re: [MOD] GeoZone - Add All Countries Button

Posted: Sun Nov 07, 2010 11:31 pm
by sourcez
Thanks for this, found it extremely useful. I ran the updated code in Chrome and just let it get on with it.

With the zones I used a text box instead so you have some choice on setup and don't have to load the page again.

Another upside of having done this is I can now take zone data directly from the database and copy it to other shops.

Re: [MOD] GeoZone - Add All Countries Button

Posted: Fri Nov 26, 2010 1:19 am
by deeve
Was thinking the other day whilst creating yet more Geo Zones for a client, wouldn't it be great to have pre-bundled Territories such as: EEC, ROW & any other major grouped Zones [dependent upon your Country & the other Zones you already have declared].

I know it's easy to suggest such tweaks & another thing to write them but just thought it worth a mention in the hope it may be possible one day.

Re: [MOD] GeoZone - Add All Countries Button

Posted: Sun Nov 28, 2010 10:07 pm
by addaiamark
I have the opposite problem I think? I need to take orders from multiple countries, but all deliveries are in 1 area. how do I make the shipping address default only to that area, and not a whole drop down of countries I don't need? ???

Re: [MOD] GeoZone - Add All Countries Button

Posted: Mon Jan 17, 2011 4:05 am
by tonnaer
I changed the fix a bit to cut down on the ajax requests (calling for each of the 180+ countries to get their zones was killing my browser ;) , so the new button code of IBBoard is fine , and the code to place at the end of the page becomes:

Code: Select all

<script type="text/javascript"><!--
function addAllGeoZones() {
<?php foreach ($countries as $country) { ?>
    html  = '<tbody id="zone_to_geo_zone_row' + zone_to_geo_zone_row + '">';
    html += '<tr>';
	  html += '<td class="left"><select name="zone_to_geo_zone[' + zone_to_geo_zone_row + '][country_id]" id="country' + zone_to_geo_zone_row + '" onchange="$(\'#zone' + zone_to_geo_zone_row + '\').load(\'index.php?route=localisation/geo_zone/zone&token=<?php echo $token; ?>&country_id=\' + this.value + \'&zone_id=0\');">';
  	html += '<option value="<?php echo $country['country_id']; ?>"><?php echo addslashes($country['name']); ?></option>';
    html += '</select></td>';
	  html += '<td class="left"><select name="zone_to_geo_zone[' + zone_to_geo_zone_row + '][zone_id]" id="zone' + zone_to_geo_zone_row + '"><option value="0">All zones</option></select></td>';
	  html += '<td class="left"><a onclick="$(\'#zone_to_geo_zone_row' + zone_to_geo_zone_row + '\').remove();" class="button"><span><?php echo $button_remove; ?></span></a></td>';
   
    html += '</tr>';
    html += '</tbody>';

    $('#zone_to_geo_zone > tfoot').before(html);
   
    zone_to_geo_zone_row++;
    <?php } ?>
}
//--></script>
This takes away the possibility of choosing countries and zones in favor of speed, so you'll have to save and re-enter to be able to change the zones if you want to.

By the way, when I re-enter the geozone edit page, it seems like the page is constructed again using js, because very slowly all countries and their zone select boxes are added to the page. Really that should be handled all in PHP in one go. Now it takes ages, and quite some countries are getting empty zone select boxes, even when they should have zones to choose from.

Re: [MOD] GeoZone - Add All Countries Button

Posted: Mon Feb 07, 2011 12:50 pm
by glolar
Has this ever been resolved? I desperately need to be able to create a "World GeoZone" that contains all countries, so I can set up my shipping (V 1.4.9.3).

Thanks in advance.

Re: [MOD] GeoZone - Add All Countries Button

Posted: Fri Mar 18, 2011 11:21 am
by ambrosite
I am really surprised that people are trying to do this with JavaScript? That's crazy. You can set up an "All Countries" Geo Zone in a half second by running a SQL query directly against the database.

First of all, setup the Geo Zone in the admin as you normally would, but don't add any countries or zones to it. Next, you need to know the geo_zone_id of the Geo Zone you just created. You can find it at the end of the URL when you edit the Geo Zone:

http: //www.mystore/admin/index.php?route=local ... _zone_id=4

Now, run the following SQL query using a tool like phpMyAdmin. Make sure you edit the query with the table prefix you selected during the OpenCart installation ('oc_' for my database). You need to enter the correct geo_zone_id in the third field on the second line (it was '4' in my case):

Code: Select all

INSERT INTO oc_zone_to_geo_zone (country_id, zone_id, geo_zone_id, date_added, date_modified)
SELECT oc_country.country_id, 0, 4, NOW(), NOW()
FROM oc_country
WHERE oc_country.name NOT IN ('United States');
That's it! The query will complete in less than two seconds, and it will add all countries and all zones except the United States to your Geo Zone. Note that you can easily edit the query to exclude more than one country. For example, if you wanted to create a Geo Zone for all countries except the US and Canada, you would do this:

Code: Select all

INSERT INTO oc_zone_to_geo_zone (country_id, zone_id, geo_zone_id, date_added, date_modified)
SELECT oc_country.country_id, 0, 4, NOW(), NOW()
FROM oc_country
WHERE oc_country.name NOT IN ('United States','Canada');

Re: [MOD] GeoZone - Add All Countries Button

Posted: Fri Mar 18, 2011 12:15 pm
by Qphoria
not everyone knows how to use mysql.. people know how to click a button. Granted this should be done with ajax now .. but this is also over a year old.. it hasn't worked for a while

Re: [MOD] GeoZone - Add All Countries Button

Posted: Fri Mar 18, 2011 12:22 pm
by ambrosite
Qphoria wrote:not everyone knows how to use mysql.. people know how to click a button. Granted this should be done with ajax now .. but this is also over a year old.. it hasn't worked for a while
True, but in order to even add the button they have to know how to edit the templates, so obviously some level of technical knowledge is assumed.

Anyway, I had to solve this problem for one of my clients so I thought I'd share it here, in case it might be useful to someone.

Re: [MOD] GeoZone - Add All Countries Button

Posted: Sat Mar 19, 2011 6:48 pm
by Seuldieu
Just in time for my project. +1 ambrosite!

Re: [MOD] GeoZone - Add All Countries Button

Posted: Mon Mar 21, 2011 9:22 am
by glolar
Thanks, ambrosite,

I also knew that this could be accomplished with a sql call, but my sql skills are rusty, and I did not want to muck it up, so I spent about 15-20 minutes adding the countries manually.

On my next OC installation, I will be sure to use your code.

Thanks again.

Re: [MOD] GeoZone - Add All Countries Button

Posted: Fri May 06, 2011 3:18 pm
by webpie it.
The above suggestions on page 2, worked for me perfect, I am running about a 3 year old imac and added them in less than 10 seconds. Used "tonnaer" script fix.

Running V 1.4.9.4
V 1.4.9.3
Worked on Both on my system, thanks guys, adding the world would have been a nightmare if i hadn't found this!

Many regards

Re: [MOD] GeoZone - Add All Countries Button

Posted: Thu Jun 02, 2011 11:56 pm
by xtr3mx7
Indeed the solution suggested by IBBoard works perfectly fine though it takes a little bit of time to complete.

Re: [MOD] GeoZone - Add All Countries Button

Posted: Sat Jun 18, 2011 10:49 pm
by Brad
A quicker way if you don't mind running SQL direclty on the database is to use the following SQL

1. Find the geo_zone_id for your "Rest of the world" FGeo Zone

Code: Select all

SELECT geo_zone_id FROM `geo_zone` WHERE name = 'Rest of the world'
2. Then take this number (this example uses 6) and replace the number 6 in the following SQL

Code: Select all

INSERT into zone_to_geo_zone (country_id, geo_zone_id)
	SELECT country_id, 6
	FROM country
Done!