Changing Weight to price based in zones.php

Ask the community for help and support.
DarioMartin
Posts: 35
Joined: Thu Dec 03, 2020 2:23 pm
Has thanked: 18 times
Been thanked: 2 times

Changing Weight to price based in zones.php

Post by DarioMartin »

As the title suggests ... is this possible? My business is small items such as crystals, earrings, essential oils etc etc etc that are by and large too difficult to get an accurate weight for. My max parcel size will usually only be around 2 kilos anyway. I'm based in Spain and courier is more interested in parcel size than weight, so I need to use Zones (Spain / Europe / UK & Ireland / rest of World) to accurately get my postage calculated, but I really want it calculated on price of order, not weight. I see that table rate allows for proce based - but I believe only allows one zone.

is it possible to change zones to check price instead of weight, or alternatively add extra zones to table rate?

Or is this something that is offered already when you Go Pro?
heatherbell
VIP Member
VIP Member
Posts: 1996
Joined: Mon Oct 07, 2019 4:39 am
Has thanked: 26 times
Been thanked: 175 times

Re: Changing Weight to price based in zones.php

Post by heatherbell »

DarioMartin wrote: Wed Dec 09, 2020 4:31 pm As the title suggests ... is this possible? My business is small items such as crystals, earrings, essential oils etc etc etc that are by and large too difficult to get an accurate weight for. My max parcel size will usually only be around 2 kilos anyway. I'm based in Spain and courier is more interested in parcel size than weight, so I need to use Zones (Spain / Europe / UK & Ireland / rest of World) to accurately get my postage calculated, but I really want it calculated on price of order, not weight. I see that table rate allows for proce based - but I believe only allows one zone.

is it possible to change zones to check price instead of weight, or alternatively add extra zones to table rate?

Or is this something that is offered already when you Go Pro?
We did this by cloning the table module to create more table modules and use each one for a different zone.
burt
Lead Developer
Lead Developer
Posts: 2423
Joined: Tue Oct 29, 2019 9:37 am
Has thanked: 49 times
Been thanked: 137 times

Re: Changing Weight to price based in zones.php

Post by burt »

Make a new module that is a mashup of the Table (price based) and Zones (multiple zones). You then have what you need.
Give it a go?
Gamechanger Addon: Queued Emails, try before you buy.
DarioMartin
Posts: 35
Joined: Thu Dec 03, 2020 2:23 pm
Has thanked: 18 times
Been thanked: 2 times

Re: Changing Weight to price based in zones.php

Post by DarioMartin »

burt wrote: Thu Dec 10, 2020 11:10 am Make a new module that is a mashup of the Table (price based) and Zones (multiple zones). You then have what you need.
Give it a go?
Would love to ... but as you may guess, I am not a PHP conversant programmer (last time I actually programmed was in COBOL soooooo.....)

I'm happy to give it a go, just need a couple of pointers if you can on how to name / show module to install, how to get CE Phoenix to recognise it. If I can get it working, I'll happily make it available to anyone who wants my very amateur coding!!
DarioMartin
Posts: 35
Joined: Thu Dec 03, 2020 2:23 pm
Has thanked: 18 times
Been thanked: 2 times

Re: Changing Weight to price based in zones.php

Post by DarioMartin »

heatherbell wrote: Wed Dec 09, 2020 8:15 pm We did this by cloning the table module to create more table modules and use each one for a different zone.
Thanks Heather - OK, I'll give it a go and report back
DarioMartin
Posts: 35
Joined: Thu Dec 03, 2020 2:23 pm
Has thanked: 18 times
Been thanked: 2 times

Re: Changing Weight to price based in zones.php

Post by DarioMartin »

DarioMartin wrote: Thu Dec 10, 2020 1:14 pm
heatherbell wrote: Wed Dec 09, 2020 8:15 pm We did this by cloning the table module to create more table modules and use each one for a different zone.
Thanks Heather - OK, I'll give it a go and report back
Help. OK, I copied your method, as it seemed the simplest. Using the CE Phoenix guide, I created new files from table.php, then renamed them, and changed names inside the PHP files and also created and changed corresponding files in both my two language packs. I was able to then see and install the 4 new modules I had created, however on test, it shows the correct rate at checkout, but pressing "continue checkout" doesn't take me to payment information - it refreshes the screen and stays at "Delivery to you", so I have obviously missed a step in coding somewhere .... any ideas?

EDIT: It's definitely to do with the modules I have created, because if I install flat rate module then select that at checkout, it takes me to Payment Information. My New Table Rate module(s) won't
heatherbell
VIP Member
VIP Member
Posts: 1996
Joined: Mon Oct 07, 2019 4:39 am
Has thanked: 26 times
Been thanked: 175 times

Re: Changing Weight to price based in zones.php

Post by heatherbell »

DarioMartin wrote: Thu Dec 10, 2020 2:53 pm Help.
Just a guess - check you have zones set correctly and triple check your own coding.
heatherbell
VIP Member
VIP Member
Posts: 1996
Joined: Mon Oct 07, 2019 4:39 am
Has thanked: 26 times
Been thanked: 175 times

Re: Changing Weight to price based in zones.php

Post by heatherbell »

DarioMartin wrote: Wed Dec 09, 2020 4:31 pm is it possible to change zones to check price instead of weight, or alternatively add extra zones to table rate?
Thinking a little bit sideways, why not configure the weights to reflect price i.e. weight=price?
DarioMartin
Posts: 35
Joined: Thu Dec 03, 2020 2:23 pm
Has thanked: 18 times
Been thanked: 2 times

Re: Changing Weight to price based in zones.php

Post by DarioMartin »

heatherbell wrote: Thinking a little bit sideways, why not configure the weights to reflect price i.e. weight=price?
That was my initial thought ... but then I would have to specify a price, instead of a weight, for each item, which would result in very weird postage rates...... I can't see any other way of getting zones.php to calculate based on price, instead of weight. I'm afraid I just DON'T have the technical skill to rip apart a PHP file and rebuild it.

Think maybe it's time to ask for developer help in the section specified .....
heatherbell wrote: Thu Dec 10, 2020 4:32 pm Just a guess - check you have zones set correctly and triple check your own coding.
Attached is an example of one of the modules I copied. By default it uses the zones I have set up in "Tax Zones" and recognises which table I am suppoed to be in, so I am guessing Zones are set correctly ... can you see anything wrong with attached code??

Code: Select all

<?php
/*
  $Id$

  osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com

  Copyright (c) 2020 osCommerce

  Released under the GNU General Public License
*/

  class table_zone_1 extends abstract_shipping_module {

    const CONFIG_KEY_BASE = 'MODULE_SHIPPING_TABLE_ZONE_1_';

// class methods
    public function quote($method = '') {
      switch ($this->base_constant('MODE') ) {
        case 'price':
          $order_total = $this->get_shippable_total();
          break;
        case 'weight':
          $order_total = $GLOBALS['shipping_weight'];
          break;
        case 'quantity':
          $order_total = $this->count_items();
          break;
      }

      $table_zone_1_cost = preg_split("/[:,]/" , $this->base_constant('COST'));
      for ($i = 0, $n = count($table_zone_1_cost); $i < $n; $i += 2) {
        if ($order_total <= $table_zone_1_cost[$i]) {
          $shipping = $table_zone_1_cost[$i+1];
          break;
        }
      }

      if ('weight' === $this->base_constant('MODE')) {
        $shipping *= $GLOBALS['shipping_num_boxes'];
      }

      $this->quotes = [
        'id' => $this->code,
        'module' => MODULE_SHIPPING_TABLE_ZONE_1_TEXT_TITLE,
        'methods' => [[
          'id' => $this->code,
          'title' => MODULE_SHIPPING_TABLE_ZONE_1_TEXT_WAY,
          'cost' => $shipping + $this->calculate_handling(),
        ]],
      ];

      $this->quote_common();

      return $this->quotes;
    }

    protected function get_parameters() {
      return [
        $this->config_key_base . 'STATUS' => [
          'title' => 'Enable Table Zone 1 Method',
          'value' => 'True',
          'desc' => 'Do you want to offer table rate shipping?',
          'set_func' => "tep_cfg_select_option(['True', 'False'], ",
        ],
        $this->config_key_base . 'COST' => [
          'title' => 'Shipping Table Zone 1',
          'value' => '25:8.50,50:5.50,10000:0.00',
          'desc' => 'The shipping cost is based on the total cost or weight of items. Example: 25:8.50,50:5.50,etc.. Up to 25 charge 8.50, from there to 50 charge 5.50, etc',
        ],
        $this->config_key_base . 'MODE' => [
          'title' => 'Table Zone 1 Method',
          'value' => 'weight',
          'desc' => 'The shipping cost is based on the order total or the total weight of the items ordered.',
          'set_func' => "tep_cfg_select_option(['weight', 'price', 'quantity'], ",
        ],
        $this->config_key_base . 'HANDLING' => [
          'title' => 'Handling Fee',
          'value' => '0',
          'desc' => 'Handling fee for this shipping method.',
        ],
        $this->config_key_base . 'TAX_CLASS' => [
          'title' => 'Tax Class',
          'value' => '0',
          'desc' => 'Use the following tax class on the shipping fee.',
          'use_func' => 'tep_get_tax_class_title',
          'set_func' => 'tep_cfg_pull_down_tax_classes(',
        ],
        $this->config_key_base . 'ZONE' => [
          'title' => 'Shipping Zone',
          'value' => '0',
          'desc' => 'If a zone is selected, only enable this shipping method for that zone.',
          'use_func' => 'tep_get_zone_class_title',
          'set_func' => 'tep_cfg_pull_down_zone_classes(',
        ],
        $this->config_key_base . 'SORT_ORDER' => [
          'title' => 'Sort Order',
          'value' => '0',
          'desc' => 'Sort order of display.',
        ],
      ];
    }

    protected function get_shippable_total() {
      global $order, $currencies;

      $order_total = (('physical' === $order->content_type) ? $_SESSION['cart']->show_total() : 0);

      if ('mixed' === $order->content_type) {
        foreach ($order->products as $product) {
          foreach (($product['attributes'] ?? []) as $option => $value) {
            $virtual_check_query = tep_db_query(sprintf(<<<'EOSQL'
SELECT COUNT(*) AS total
  FROM products_attributes pa
    INNER JOIN products_attributes_download pad
      ON pa.products_attributes_id = pad.products_attributes_id
  WHERE pa.products_id = %d AND pa.options_values_id = %d
EOSQL
              , (int)$product['id'], (int)$value['value_id']));
            $virtual_check = tep_db_fetch_array($virtual_check_query);

            if ($virtual_check['total'] > 0) {
              // if any attribute is downloadable, the product is virtual
              // and doesn't count; so skip to the next product
              // without adding the product quantity
              continue 2;
            }
          }

          $order_total += $currencies->calculate_price($product['final_price'], $product['tax'], $product['qty']);
        }
      }

      return $order_total;
    }

    function count_items() {
      global $order;
      
      $item_count = ('physical' === $order->content_type)
                  ? ($GLOBALS['total_count'] ?? $_SESSION['cart']->count_contents())
                  : 0;
      
      if ('mixed' === $order->content_type) {
        foreach ($order->products as $product) {
          foreach (($product['attributes'] ?? []) as $option => $value) {
            $virtual_check_query = tep_db_query(sprintf(<<<'EOSQL'
SELECT COUNT(*) AS total
 FROM products_attributes pa INNER JOIN products_attributes_download pad
   ON pa.products_attributes_id = pad.products_attributes_id
 WHERE pa.products_id = %d AND pa.options_values_id = %d
EOSQL
              , (int)$product['id'], (int)$value['value_id']));
            $virtual_check = tep_db_fetch_array($virtual_check_query);
            
            if ($virtual_check['total'] > 0) {
              // if any attribute is downloadable, the product is virtual
              // and doesn't count; so skip to the next product
              // without adding the product quantity
              continue 2;
            }
          }
          
          $item_count += $product['qty'];
        }
      }
      
      return $item_count;
    }

  }
 
burt
Lead Developer
Lead Developer
Posts: 2423
Joined: Tue Oct 29, 2019 9:37 am
Has thanked: 49 times
Been thanked: 137 times

Re: Changing Weight to price based in zones.php

Post by burt »

Gamechanger Addon: Queued Emails, try before you buy.
Post Reply