Page 1 of 1

Shipping - Zones

Posted: Wed Apr 10, 2024 4:57 pm
by Xpajun
I've installed zones on shipping, loaded the countries and shipping table for 6 country zones But when I test it nothing works

The zones are in the data base - enable zones method is set to true the zones do not appear on the zones.php file (is this correct?)

Or have I missed something?

Re: Shipping - Zones

Posted: Wed Apr 10, 2024 6:36 pm
by Xpajun
In addition:
If I change the name of the file from zones.php to airmail.php and upload to the shipping directories it does not show under Install Modules in Shipping

Re: Shipping - Zones

Posted: Wed Apr 10, 2024 11:18 pm
by ecartz
Xpajun wrote: Wed Apr 10, 2024 6:36 pm If I change the name of the file from zones.php to airmail.php and upload to the shipping directories it does not show under Install Modules in Shipping
Did you also change class zones to airmail inside the file?

Did you change the number of zones from the default to 6?

Re: Shipping - Zones

Posted: Thu Apr 11, 2024 6:26 am
by Xpajun
ecartz wrote: Wed Apr 10, 2024 11:18 pm
Xpajun wrote: Wed Apr 10, 2024 6:36 pm If I change the name of the file from zones.php to airmail.php and upload to the shipping directories it does not show under Install Modules in Shipping
Did you also change class zones to airmail inside the file?

Did you change the number of zones from the default to 6?
Perhaps better if I posted my altered code:

Code: Select all

  class zones extends abstract_shipping_module {

    const CONFIG_KEY_BASE = 'MODULE_SHIPPING_AIRMAIL_';

// CUSTOMIZE THIS SETTING FOR THE NUMBER OF ZONES NEEDED
    const ZONE_COUNT = 6;

    protected $destination_zone = false;

    public function update_status_by($address) {
      if (!$this->enabled || (false !== $this->destination_zone) || !isset($address['country']['iso_code_2'])) {
        return;
      }

      for ($i = 1; $i <= static::ZONE_COUNT; $i++) {
        if (in_array($address['country']['iso_code_2'], explode(';', $this->base_constant("COUNTRIES_$i")))) {
          $this->destination_zone = $i;
          return;
        }
      }

      $this->enabled = false;
    }

    public function quote($method = '') {
      global $order, $shipping_weight, $shipping_num_boxes;
      $this->quotes = [
        'id' => $this->code,
        'module' => MODULE_SHIPPING_AIRMAIL_TEXT_TITLE,
        'methods' => [],
      ];

      if (false !== $this->destination_zone) {
        $zones_table = preg_split('{[:,]}' , $this->base_constant("COST_{$this->destination_zone}"));
        for ($i = 0, $size = count($zones_table); $i < $size; $i += 2) {
          if ($shipping_weight <= $zones_table[$i]) {
            $this->quotes['methods'][] = [
              'id' => $this->code,
              'title' => sprintf(MODULE_SHIPPING_AIRMAIL_TEXT_WAY,
                $order->delivery['country']['iso_code_2'],
                $shipping_weight),
              'cost' => ((float)$zones_table[$i+1] * $GLOBALS['shipping_num_boxes'])
                      + (float)$this->base_constant("HANDLING_{$this->destination_zone}"),
            ];
            break;
          }
        }

        if (!isset($this->quotes['methods'][0])) {
          error_log(sprintf('Weight [%d] larger than maximum in table [%s] for [%s].',
            $shipping_weight,
            $this->base_constant("COST_$dest_zone"),
            $order->delivery['country']['iso_code_2']));
        }
      }

      $this->quote_common();

      return $this->quotes;
    }

    protected function get_parameters() {
      $parameters = [
        $this->config_key_base . 'STATUS' => [
          'title' => 'Enable Zones Method',
          'value' => 'True',
          'desc' => 'Do you want to offer airmail rate shipping?',
          'set_func' => "Config::select_one(['True', 'False'], ",
        ],
        $this->config_key_base . 'TAX_CLASS' => [
          'title' => 'Tax Class',
          'value' => '0',
          'desc' => 'Use the following tax class on the shipping fee.',
          'use_func' => 'Tax::get_class_title',
          'set_func' => 'Config::select_tax_class(',
        ],
        $this->config_key_base . 'SORT_ORDER' => [
          'title' => 'Sort Order',
          'value' => '0',
          'desc' => 'Sort order of display.',
        ],
      ];

      for ($i = 1; $i <= static::ZONE_COUNT; $i++) {
        $parameters = array_merge($parameters, [
          "{$this->config_key_base}COUNTRIES_$i" => [
            'title' => "Zone $i Countries",
            'value' => (($i == 1) ? 'US;CA' : ''),
            'desc' => "Semi-colon separated list of two character ISO country codes that are part of Zone $i.",
          ],
          "{$this->config_key_base}COST_$i" => [
            'title' => "Zone $i Shipping Table",
            'value' => '3:8.50,7:10.50,99:20.00',
            'desc' => <<<"EOT"
Shipping rates to Zone $i destinations based on a group of maximum order weights.
Example: 3:8.50,7:10.50,...
Weights less than or equal to 3 would cost 8.50 for Zone $i destinations.
EOT
          ],
          "{$this->config_key_base}HANDLING_$i" => [
            'title' => "Zone $i Handling Fee",
            'value' => '0',
            'desc' => 'Handling Fee for this shipping zone',
          ],
        ]);
      }

      return $parameters;
    }

  }

Re: Shipping - Zones

Posted: Thu Apr 11, 2024 6:56 am
by ecartz

Code: Select all

  class airmail extends abstract_shipping_module {
Other than that, it looks fine. Note that if you are making a new file, you need a new language file too. The language file constants should be, e.g., MODULE_SHIPPING_AIRMAIL_TEXT_TITLE.

Re: Shipping - Zones

Posted: Thu Apr 11, 2024 8:48 am
by Xpajun
ecartz wrote: Thu Apr 11, 2024 6:56 am

Code: Select all

  class airmail extends abstract_shipping_module {
Other than that, it looks fine. Note that if you are making a new file, you need a new language file too. The language file constants should be, e.g., MODULE_SHIPPING_AIRMAIL_TEXT_TITLE.
Doh... Thank you Matt - long day yesterday :oops:

Yes I have the language file as well, it is loading now

But is still not loading on checkout
Xpajun wrote: Wed Apr 10, 2024 4:57 pm I've installed zones on shipping, loaded the countries and shipping table for 6 country zones But when I test it nothing works

The zones are in the data base - enable zones method is set to true the zones do not appear on the zones.php file (is this correct?)

Or have I missed something?
Both the Flat and Table Rates show but not the Zones or my new Airmail

Re: Shipping - Zones

Posted: Thu Apr 11, 2024 10:31 am
by Xpajun
One day I will learn to read instead of assuming - I was working on the old principal of putting a comma between the country code rather than a semi-colon

Thanks Matt for your help (I will have another query on the shipping files later)

Re: Shipping - Zones

Posted: Thu Apr 11, 2024 10:41 am
by 14Steve14
Xpajun wrote: Thu Apr 11, 2024 10:31 am One day I will learn to read instead of assuming - I was working on the old principal of putting a comma between the country code rather than a semi-colon

Thanks Matt for your help (I will have another query on the shipping files later)
I fell for that exact same thing.

Re: Shipping - Zones

Posted: Thu Apr 11, 2024 1:11 pm
by 14Steve14
14Steve14 wrote: Thu Apr 11, 2024 10:41 am
Xpajun wrote: Thu Apr 11, 2024 10:31 am One day I will learn to read instead of assuming - I was working on the old principal of putting a comma between the country code rather than a semi-colon

Thanks Matt for your help (I will have another query on the shipping files later)
I fell for that exact same thing when Gary was coding me shipping modules and I could not get them to work.