Advertisement
osc_pro

Untitled

Dec 10th, 2020
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.83 KB | None | 0 0
  1. /includes/modules/shipping/postino.php
  2. --------------------------------------
  3.  
  4. <?php
  5. /*
  6.   $Id$
  7.  
  8.   osCommerce, Open Source E-Commerce Solutions
  9.   http://www.oscommerce.com
  10.  
  11.   Copyright (c) 2020 osCommerce
  12.  
  13.   Released under the GNU General Public License
  14. */
  15.  
  16.   class postino extends abstract_shipping_module {
  17.  
  18.     const CONFIG_KEY_BASE = 'MODULE_SHIPPING_POSTINO_';
  19.  
  20.     // CUSTOMIZE THIS SETTING FOR THE NUMBER OF ZONES NEEDED
  21.     const ZONE_COUNT = 2;
  22.  
  23. // class methods
  24.     public function quote($method = '') {
  25.       global $order;
  26.      
  27.       switch ($this->base_constant('MODE') ) {
  28.         case 'price':
  29.           $order_total = $this->get_shippable_total();
  30.           break;
  31.         case 'weight':
  32.           $order_total = $GLOBALS['shipping_weight'];
  33.           break;
  34.         case 'quantity':
  35.           $order_total = $this->count_items();
  36.           break;
  37.       }
  38.  
  39.       $dest_zone = false;
  40.       $error = false;
  41.  
  42.       for ($i = 1; $i <= static::ZONE_COUNT; $i++) {
  43.         if (in_array($order->delivery['country']['iso_code_2'], explode(';', $this->base_constant('COUNTRIES_' . $i)))) {
  44.           $dest_zone = $i;
  45.           break;
  46.         }
  47.       }
  48.  
  49.       if (false === $dest_zone) {
  50.         $error = true;
  51.       } else {
  52.         $shipping = false;
  53.         $zones_cost = $this->base_constant('COST_' . $dest_zone);
  54.  
  55.         $zones_table = preg_split('/[:,]/' , $zones_cost);
  56.         for ($i = 0, $size = count($zones_table); $i < $size; $i += 2) {
  57.           if ($order_total <= $zones_table[$i]) {
  58.             $shipping = $zones_table[$i+1];
  59.             $shipping_method = sprintf(MODULE_SHIPPING_POSTINO_TEXT_WAY, $order->delivery['country']['iso_code_2']);
  60.             break;
  61.           }
  62.         }
  63.  
  64.         if (false === $shipping) {
  65.           $shipping_cost = 0;
  66.           $shipping_method = MODULE_SHIPPING_POSTINO_UNDEFINED_RATE;
  67.         } else {
  68.           $shipping_cost = ($shipping * $GLOBALS['shipping_num_boxes']) + $this->base_constant('HANDLING_' . $dest_zone);
  69.         }
  70.       }
  71.  
  72.       $this->quotes = [
  73.         'id' => $this->code,
  74.         'module' => MODULE_SHIPPING_POSTINO_TEXT_TITLE,
  75.         'methods' => [[
  76.           'id' => $this->code,
  77.           'title' => $shipping_method,
  78.           'cost' => $shipping_cost,
  79.         ]],
  80.       ];
  81.  
  82.       $this->quote_common();
  83.  
  84.       if ($error) {
  85.         $this->quotes['error'] = MODULE_SHIPPING_POSTINO_INVALID_ZONE;
  86.       }
  87.  
  88.       return $this->quotes;
  89.     }
  90.  
  91.     protected function get_parameters() {
  92.       $parameters = [
  93.         $this->config_key_base . 'STATUS' => [
  94.           'title' => 'Enable Postino Method',
  95.           'value' => 'True',
  96.           'desc' => 'Do you want to offer this shipping method?',
  97.           'set_func' => "tep_cfg_select_option(['True', 'False'], ",
  98.         ],
  99.         $this->config_key_base . 'TAX_CLASS' => [
  100.           'title' => 'Tax Class',
  101.           'value' => '0',
  102.           'desc' => 'Use the following tax class on the shipping fee.',
  103.           'use_func' => 'tep_get_tax_class_title',
  104.           'set_func' => 'tep_cfg_pull_down_tax_classes(',
  105.         ],
  106.         $this->config_key_base . 'MODE' => [
  107.           'title' => 'Table Method',
  108.           'value' => 'weight',
  109.           'desc' => 'The shipping cost is based on the order total or the total weight of the items ordered.',
  110.           'set_func' => "tep_cfg_select_option(['weight', 'price', 'quantity'], ",
  111.         ],
  112.         $this->config_key_base . 'SORT_ORDER' => [
  113.           'title' => 'Sort Order',
  114.           'value' => '0',
  115.           'desc' => 'Sort order of display.',
  116.         ],
  117.       ];
  118.  
  119.       for ($i = 1; $i <= static::ZONE_COUNT; $i++) {
  120.         $parameters = array_merge($parameters, [
  121.           $this->config_key_base . 'COUNTRIES_' . $i => [
  122.             'title' => 'Zone ' . $i . ' Countries',
  123.             'value' => (($i == 1) ? 'US;CA' : ''),
  124.             'desc' => 'Semi-colon separated list of two character ISO country codes that are part of Zone ' . $i . '.',
  125.           ],
  126.           $this->config_key_base . 'COST_' . $i => [
  127.             'title' => 'Zone ' . $i . ' Shipping Table',
  128.             'value' => '3:8.50,7:10.50,99:20.00',
  129.             'desc' => 'Shipping rates to Zone ' . $i . ' destinations based on a group of maximum order weights. Example: 3:8.50,7:10.50,... Weight/Price less than or equal to 3 would cost 8.50 for Zone ' . $i . ' destinations.',
  130.           ],
  131.           $this->config_key_base . 'HANDLING_' . $i => [
  132.             'title' => 'Zone ' . $i . ' Handling Fee',
  133.             'value' => '0',
  134.             'desc' => 'Handling Fee for this shipping zone',
  135.           ],
  136.         ]);
  137.       }
  138.  
  139.       return $parameters;
  140.     }
  141.    
  142.     protected function get_shippable_total() {
  143.       global $order, $currencies;
  144.  
  145.       $order_total = (('physical' === $order->content_type) ? $_SESSION['cart']->show_total() : 0);
  146.  
  147.       if ('mixed' === $order->content_type) {
  148.         foreach ($order->products as $product) {
  149.           foreach (($product['attributes'] ?? []) as $option => $value) {
  150.             $virtual_check_query = tep_db_query(sprintf(<<<'EOSQL'
  151. SELECT COUNT(*) AS total
  152.   FROM products_attributes pa
  153.     INNER JOIN products_attributes_download pad
  154.       ON pa.products_attributes_id = pad.products_attributes_id
  155.   WHERE pa.products_id = %d AND pa.options_values_id = %d
  156. EOSQL
  157.               , (int)$product['id'], (int)$value['value_id']));
  158.             $virtual_check = tep_db_fetch_array($virtual_check_query);
  159.  
  160.             if ($virtual_check['total'] > 0) {
  161.               // if any attribute is downloadable, the product is virtual
  162.               // and doesn't count; so skip to the next product
  163.               // without adding the product quantity
  164.               continue 2;
  165.             }
  166.           }
  167.  
  168.           $order_total += $currencies->calculate_price($product['final_price'], $product['tax'], $product['qty']);
  169.         }
  170.       }
  171.  
  172.       return $order_total;
  173.     }
  174.  
  175.     function count_items() {
  176.       global $order;
  177.      
  178.       $item_count = ('physical' === $order->content_type)
  179.                   ? ($GLOBALS['total_count'] ?? $_SESSION['cart']->count_contents())
  180.                   : 0;
  181.      
  182.       if ('mixed' === $order->content_type) {
  183.         foreach ($order->products as $product) {
  184.           foreach (($product['attributes'] ?? []) as $option => $value) {
  185.             $virtual_check_query = tep_db_query(sprintf(<<<'EOSQL'
  186. SELECT COUNT(*) AS total
  187.  FROM products_attributes pa INNER JOIN products_attributes_download pad
  188.    ON pa.products_attributes_id = pad.products_attributes_id
  189.  WHERE pa.products_id = %d AND pa.options_values_id = %d
  190. EOSQL
  191.               , (int)$product['id'], (int)$value['value_id']));
  192.             $virtual_check = tep_db_fetch_array($virtual_check_query);
  193.            
  194.             if ($virtual_check['total'] > 0) {
  195.               // if any attribute is downloadable, the product is virtual
  196.               // and doesn't count; so skip to the next product
  197.               // without adding the product quantity
  198.               continue 2;
  199.             }
  200.           }
  201.          
  202.           $item_count += $product['qty'];
  203.         }
  204.       }
  205.      
  206.       return $item_count;
  207.     }
  208.  
  209.   }
  210.  
  211.  
  212.  
  213. /includes/languages/english/modules/shipping/postino.php
  214. --------------------------------------------------------
  215.  
  216. <?php
  217. /*
  218.   $Id$
  219.  
  220.   osCommerce, Open Source E-Commerce Solutions
  221.   http://www.oscommerce.com
  222.  
  223.   Copyright (c) 2020 osCommerce
  224.  
  225.   Released under the GNU General Public License
  226. */
  227.  
  228. const MODULE_SHIPPING_POSTINO_TEXT_TITLE = 'Postino';
  229. const MODULE_SHIPPING_POSTINO_TEXT_DESCRIPTION = 'P&P';
  230.  
  231. const MODULE_SHIPPING_POSTINO_TEXT_WAY = 'Delivery to %s';
  232.  
  233. const MODULE_SHIPPING_POSTINO_INVALID_ZONE = 'No shipping available to the selected country';
  234. const MODULE_SHIPPING_POSTINO_UNDEFINED_RATE = 'The shipping rate cannot be determined at this time';
  235.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement