Zen Cart Logo
Forums / Built-in Shipping and Payment Modules / Flat Rate Shipping Clone with added code won't progress to the payment page

Flat Rate Shipping Clone with added code won't progress to the payment page

Views: 900

Results 1 to 4 of 4
15 Jan 2015, 4:15 PM
#1
mrudden avatar

mrudden

New Zenner

Join Date:
Apr 2013
Location:
Boston, Massachusetts, United States
Posts:
10
Plugin Contributions:
0

Flat Rate Shipping Clone with added code won't progress to the payment page

I only use domestic USPS Flat Rate boxes. Using the USPS module I just get a bunch of shipping options that don't really relate to how many flat rate boxes will actually be used. What I really need is dimensional weight shipping.

So I did some research and found the formula that UPS and FedEx are using ((LxWxH)/166).

First I did a real life test pack of a products into flat rate boxes. I measured each product and got it's LxWxH. Because I use packing peanuts, I added 1 inch to the dimensions. I then figured the dimensional weight of each product using the above formula and then entered that as the weight for the product in Zencart. Then I did the actual test pack so that I would know how many boxes I would need and what the price would be. Doing this validated my algorithm.

I then took copies of the 2 flat.php files used for Flat Rate Shipping in Zencart (1 is in include/languages and 1 is in include/modules). I inserted some code using the algorithm I'd developed based on the above tests and research. I also renamed the variables and the files in the manner described on an older thread on subject.

I uploaded the files and installed them to my test cart as uspost_flat.php. The shipping page came up and had the right price based on the real life test packs of products. I choose that shipping method via the radio button (even though it was the only shipping method). Then I tried to go to the Payment Step and it didn't go. It just came back to the Shipping Step.

So I then downloaded the 2 flat.php files that come with the standard installation. I made copies (so I could go back) and inserted the logic into the flat.php module file. I uploaded and overwrote the original ones.

I made sure both shipping methods - my new one (USPOST_Flat) and Flat - were installed and active. No issues there.

I tried placing an order again. Both shipping methods came up with the same price for shipping. If I chose the Flat method, the order progressed to the Payment Step. If I chose the USPOST Flat method, it just came back to the Shipping Step.

And before anyone asks, I called it USPOST_Flat because USPS checks for the string USPS when it checks to see if it is installed correctly.

Any ideas?

Module:

<?php
/**
 * @package shippingMethod
 * @copyright Copyright 2003-2009 Zen Cart Development Team
 * @copyright Portions Copyright 2003 osCommerce
 * @license [url]http://www.zen-cart.com/license/2_0.txt[/url] GNU Public License V2.0
 */
// $Id: uspost_flat.php 14498 2009-10-01 20:16:16Z ajeh $
//

  class uspost_flat {
    var $code, $title, $description, $icon, $enabled;

// class constructor
    function uspost_flat() {
      global $order, $db;

      $this->code = 'uspost_flat';
      $this->title = MODULE_SHIPPING_USPOST_FLAT_TEXT_TITLE;
      $this->description = MODULE_SHIPPING_USPOST_FLAT_TEXT_DESCRIPTION;
      $this->sort_order = MODULE_SHIPPING_USPOST_FLAT_SORT_ORDER;
      $this->icon = '';
      $this->tax_class = MODULE_SHIPPING_USPOST_FLAT_TAX_CLASS;
      $this->tax_basis = MODULE_SHIPPING_USPOST_FLAT_TAX_BASIS;

      // disable only when entire cart is free shipping
      if (zen_get_shipping_enabled($this->code)) {
        $this->enabled = ((MODULE_SHIPPING_USPOST_FLAT_STATUS == 'True') ? true : false);
      }

      if ( ($this->enabled == true) && ((int)MODULE_SHIPPING_USPOST_FLAT_ZONE > 0) ) {
        $check_flag = false;
        $check = $db->Execute("select zone_id from " . TABLE_ZONES_TO_GEO_ZONES . " where geo_zone_id = '" . MODULE_SHIPPING_USPOST_FLAT_ZONE . "' and zone_country_id = '" . $order->delivery['country']['id'] . "' order by zone_id");
        while (!$check->EOF) {
          if ($check->fields['zone_id'] < 1) {
            $check_flag = true;
            break;
          } elseif ($check->fields['zone_id'] == $order->delivery['zone_id']) {
            $check_flag = true;
            break;
          }
          $check->MoveNext();
        }

        if ($check_flag == false) {
          $this->enabled = false;
        }
      }
    }

// class methods
    function quote($method = '') {
      global $order;

      $sfrbdw = .453819;
      $mfrbdw = 3.097891;
      $lfrbdw = 4.771084;
      $scwleft = $_SESSION['cart']->weight;
      $mcwleft = $_SESSION['cart']->weight;
      $lcwleft = $_SESSION['cart']->weight;
      $snumbox = 0;
      $mnumbox = 0;
      $lnumbox = 0;
      $lcost = 17.9;
      $mcost = 12.65;
      $scost = 5.95;
      $final_cost = 0;
      
      if ($lcwleft >= $lfrbdw) {
            $lnumbox = floor($lcwleft/$lfrbdw);
            $mcwleft = $lcwleft - ($lfrbdw*$lnumbox);
            $scwleft = $mcwleft;
      }
      if ($mcwleft >= $mfrbdw) {
            $mnumbox = floor($mcwleft/$mfrbdw);
            $scwleft = $mcwleft - ($mfrbdw*$mnumbox);
      }

      if ($scwleft > $sfrbdw) {
            $mnumbox = $mnumbox + 1;
      }
      if ($scwleft <= $sfrbdw) {     
            $snumbox = 1;
      }

      $final_cost = ($snumbox * $scost) + ($mnumbox * $mcost) + ($lnumbox * $lcost);
      
      $this->quotes = array('id' => $this->code,
                            'module' => MODULE_SHIPPING_USPOST_FLAT_TEXT_TITLE,
                            'methods' => array(array('id' => $this->code,
                                                     'title' => MODULE_SHIPPING_USPOST_FLAT_TEXT_WAY,
                                                     'cost' => $final_cost)));
      if ($this->tax_class > 0) {
        $this->quotes['tax'] = zen_get_tax_rate($this->tax_class, $order->delivery['country']['id'], $order->delivery['zone_id']);
      }

      if (zen_not_null($this->icon)) $this->quotes['icon'] = zen_image($this->icon, $this->title);

      return $this->quotes;
    }

    function check() {
      global $db;
      if (!isset($this->_check)) {
        $check_query = $db->Execute("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_SHIPPING_USPOST_FLAT_STATUS'");
        $this->_check = $check_query->RecordCount();
      }
      return $this->_check;
    }

    function install() {
      global $db;
      $db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Enable USPOST Flat Shipping', 'MODULE_SHIPPING_USPOST_FLAT_STATUS', 'True', 'Do you want to offer uspost_flat rate shipping?', '6', '0', 'zen_cfg_select_option(array(\'True\', \'False\'), ', now())");
      $db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Shipping Cost', 'MODULE_SHIPPING_USPOST_FLAT_COST', '5.00', 'The shipping cost for all orders using this shipping method.', '6', '0', now())");
      $db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, use_function, set_function, date_added) values ('Tax Class', 'MODULE_SHIPPING_USPOST_FLAT_TAX_CLASS', '0', 'Use the following tax class on the shipping fee.', '6', '0', 'zen_get_tax_class_title', 'zen_cfg_pull_down_tax_classes(', now())");
      $db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Tax Basis', 'MODULE_SHIPPING_USPOST_FLAT_TAX_BASIS', 'Shipping', 'On what basis is Shipping Tax calculated. Options are<br />Shipping - Based on customers Shipping Address<br />Billing Based on customers Billing address<br />Store - Based on Store address if Billing/Shipping Zone equals Store zone', '6', '0', 'zen_cfg_select_option(array(\'Shipping\', \'Billing\', \'Store\'), ', now())");
      $db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, use_function, set_function, date_added) values ('Shipping Zone', 'MODULE_SHIPPING_USPOST_FLAT_ZONE', '0', 'If a zone is selected, only enable this shipping method for that zone.', '6', '0', 'zen_get_zone_class_title', 'zen_cfg_pull_down_zone_classes(', now())");
      $db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Sort Order', 'MODULE_SHIPPING_USPOST_FLAT_SORT_ORDER', '0', 'Sort order of display.', '6', '0', now())");
    }

    function remove() {
      global $db;
      $db->Execute("delete from " . TABLE_CONFIGURATION . " where configuration_key like 'MODULE\_SHIPPING\_USPOST_FLAT\_%'");
    }

    function keys() {
      return array('MODULE_SHIPPING_USPOST_FLAT_STATUS', 'MODULE_SHIPPING_USPOST_FLAT_COST', 'MODULE_SHIPPING_USPOST_FLAT_TAX_CLASS', 'MODULE_SHIPPING_USPOST_FLAT_TAX_BASIS', 'MODULE_SHIPPING_USPOST_FLAT_ZONE', 'MODULE_SHIPPING_USPOST_FLAT_SORT_ORDER');
    }
  }
?>

Language:

<?php
//
// +----------------------------------------------------------------------+
// |zen-cart Open Source E-commerce                                       |
// +----------------------------------------------------------------------+
// | Copyright (c) 2003 The zen-cart developers                           |
// |                                                                      |   
// | [url]http://www.zen-cart.com/index.php[/url]                                    |   
// |                                                                      |   
// | Portions Copyright (c) 2003 osCommerce                               |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the GPL license,       |
// | that is bundled with this package in the file LICENSE, and is        |
// | available through the world-wide-web at the following url:           |
// | [url]http://www.zen-cart.com/license/2_0.txt[/url].                             |
// | If you did not receive a copy of the zen-cart license and are unable |
// | to obtain it through the world-wide-web, please send a note to       |
// | [email protected] so we can mail you a copy immediately.          |
// +----------------------------------------------------------------------+
// $Id: uspost_flat.php 1969 2005-09-13 06:57:21Z drbyte $
//

define('MODULE_SHIPPING_USPOST_FLAT_TEXT_TITLE', 'USPOST Flat Rate');
define('MODULE_SHIPPING_USPOST_FLAT_TEXT_DESCRIPTION', 'USPOST Flat Rate');
define('MODULE_SHIPPING_USPOST_FLAT_TEXT_WAY', 'Best Way');
?>
15 Jan 2015, 4:37 PM
#2
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
177

Re: Flat Rate Shipping Clone with added code won't progress to the payment page

It's the underscore in uspost_flat that's causing the problem.

Internally when the shipping quotes are processed, they are parsed based on internally-inserted underscores, and having an underscore in the module name breaks it, resulting in the symptoms you've posted about.

Simply refactor by removing the underscore from uspost_flat and all should work fine.

15 Jan 2015, 4:41 PM
#3
mrudden avatar

mrudden

New Zenner

Join Date:
Apr 2013
Location:
Boston, Massachusetts, United States
Posts:
10
Plugin Contributions:
0

Re: Flat Rate Shipping Clone with added code won't progress to the payment page

Thank you!

15 Jan 2015, 5:25 PM
#4
mrudden avatar

mrudden

New Zenner

Join Date:
Apr 2013
Location:
Boston, Massachusetts, United States
Posts:
10
Plugin Contributions:
0

Re: Flat Rate Shipping Clone with added code won't progress to the payment page

I've made those corrections. At this point the code only works with Small, Medium Box 1 and Large flat rate boxes and the cost for each of them are hard-coded. I'll be looking at getting live price quotes from USPS as inputs next. But for now, this will suffice for my store.

Just in case anyone wants to use this:

You need to measure each of your products and get a LxWxH and then add 1 inch to the dimensions if you use packing peanuts. Then you multiply that corrected LxWxH and divide the answer by 166 to get the dimensional weight that you would then enter on the products page.

Because dimensional weight is not the actual weight, if you use this module, you can't use any shipping methods that rely on weight to calculate the shipping cost.

The updated code is below. I'd upload it as a plugin, but I have no idea how to do this.:

Place in /includes/modules/shipping as uspostflat.php

<?php
/**
 * @package shippingMethod
 * @copyright Copyright 2003-2009 Zen Cart Development Team
 * @copyright Portions Copyright 2003 osCommerce
 * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
 */
// Based on flat.php 14498 2009-10-01 20:16:16Z ajeh $
// $Id: uspostflat.php 1969 2015-01-15 11:55:00:21Z mrudden $
//

  class uspostflat {
    var $code, $title, $description, $icon, $enabled;

// class constructor
    function uspostflat() {
      global $order, $db;

      $this->code = 'uspostflat';
      $this->title = MODULE_SHIPPING_USPOST_FLAT_TEXT_TITLE;
      $this->description = MODULE_SHIPPING_USPOST_FLAT_TEXT_DESCRIPTION;
      $this->sort_order = MODULE_SHIPPING_USPOST_FLAT_SORT_ORDER;
      $this->icon = '';
      $this->tax_class = MODULE_SHIPPING_USPOST_FLAT_TAX_CLASS;
      $this->tax_basis = MODULE_SHIPPING_USPOST_FLAT_TAX_BASIS;

      // disable only when entire cart is free shipping
      if (zen_get_shipping_enabled($this->code)) {
        $this->enabled = ((MODULE_SHIPPING_USPOST_FLAT_STATUS == 'True') ? true : false);
      }

      if ( ($this->enabled == true) && ((int)MODULE_SHIPPING_USPOST_FLAT_ZONE > 0) ) {
        $check_flag = false;
        $check = $db->Execute("select zone_id from " . TABLE_ZONES_TO_GEO_ZONES . " where geo_zone_id = '" . MODULE_SHIPPING_USPOST_FLAT_ZONE . "' and zone_country_id = '" . $order->delivery['country']['id'] . "' order by zone_id");
        while (!$check->EOF) {
          if ($check->fields['zone_id'] < 1) {
            $check_flag = true;
            break;
          } elseif ($check->fields['zone_id'] == $order->delivery['zone_id']) {
            $check_flag = true;
            break;
          }
          $check->MoveNext();
        }

        if ($check_flag == false) {
          $this->enabled = false;
        }
      }
    }

// class methods
    function quote($method = '') {
      global $order;

	  $sfrbdw = .453819;
	  $mfrbdw = 3.097891;
	  $lfrbdw = 4.771084;
	  $scwleft = $_SESSION['cart']->weight;
	  $mcwleft = $_SESSION['cart']->weight;
	  $lcwleft = $_SESSION['cart']->weight;
	  $snumbox = 0;
	  $mnumbox = 0;
	  $lnumbox = 0;
	  $lcost = 17.9;
	  $mcost = 12.65;
	  $scost = 5.95;
	  $final_cost = 0;
	  
	  if ($lcwleft >= $lfrbdw) {
			$lnumbox = floor($lcwleft/$lfrbdw);
			$mcwleft = $lcwleft - ($lfrbdw*$lnumbox);
			$scwleft = $mcwleft;
	  }
	  if ($mcwleft >= $mfrbdw) {
			$mnumbox = floor($mcwleft/$mfrbdw);
			$scwleft = $mcwleft - ($mfrbdw*$mnumbox);
	  }

	  if ($scwleft > $sfrbdw) {
			$mnumbox = $mnumbox + 1;
	  }
	  if ($scwleft <= $sfrbdw) { 	
			$snumbox = 1;
	  }

	  $final_cost = ($snumbox * $scost) + ($mnumbox * $mcost) + ($lnumbox * $lcost);
	  
      $this->quotes = array('id' => $this->code,
                            'module' => MODULE_SHIPPING_USPOST_FLAT_TEXT_TITLE,
                            'methods' => array(array('id' => $this->code,
                                                     'title' => MODULE_SHIPPING_USPOST_FLAT_TEXT_WAY.$snumbox.' small boxes ,'.$mnumbox.' medium boxes and '.$lnumbox.' large boxes',
                                                     'cost' => $final_cost)));
      if ($this->tax_class > 0) {
        $this->quotes['tax'] = zen_get_tax_rate($this->tax_class, $order->delivery['country']['id'], $order->delivery['zone_id']);
      }

      if (zen_not_null($this->icon)) $this->quotes['icon'] = zen_image($this->icon, $this->title);

      return $this->quotes;
    }

    function check() {
      global $db;
      if (!isset($this->_check)) {
        $check_query = $db->Execute("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_SHIPPING_USPOST_FLAT_STATUS'");
        $this->_check = $check_query->RecordCount();
      }
      return $this->_check;
    }

    function install() {
      global $db;
      $db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Enable USPOST Flat Shipping', 'MODULE_SHIPPING_USPOST_FLAT_STATUS', 'True', 'Do you want to offer uspostflat rate shipping?', '6', '0', 'zen_cfg_select_option(array(\'True\', \'False\'), ', now())");
      $db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Shipping Cost', 'MODULE_SHIPPING_USPOST_FLAT_COST', '5.00', 'The shipping cost for all orders using this shipping method.', '6', '0', now())");
      $db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, use_function, set_function, date_added) values ('Tax Class', 'MODULE_SHIPPING_USPOST_FLAT_TAX_CLASS', '0', 'Use the following tax class on the shipping fee.', '6', '0', 'zen_get_tax_class_title', 'zen_cfg_pull_down_tax_classes(', now())");
      $db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Tax Basis', 'MODULE_SHIPPING_USPOST_FLAT_TAX_BASIS', 'Shipping', 'On what basis is Shipping Tax calculated. Options are<br />Shipping - Based on customers Shipping Address<br />Billing Based on customers Billing address<br />Store - Based on Store address if Billing/Shipping Zone equals Store zone', '6', '0', 'zen_cfg_select_option(array(\'Shipping\', \'Billing\', \'Store\'), ', now())");
      $db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, use_function, set_function, date_added) values ('Shipping Zone', 'MODULE_SHIPPING_USPOST_FLAT_ZONE', '0', 'If a zone is selected, only enable this shipping method for that zone.', '6', '0', 'zen_get_zone_class_title', 'zen_cfg_pull_down_zone_classes(', now())");
      $db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Sort Order', 'MODULE_SHIPPING_USPOST_FLAT_SORT_ORDER', '0', 'Sort order of display.', '6', '0', now())");
    }

    function remove() {
      global $db;
      $db->Execute("delete from " . TABLE_CONFIGURATION . " where configuration_key like 'MODULE\_SHIPPING\_USPOST_FLAT\_%'");
    }

    function keys() {
      return array('MODULE_SHIPPING_USPOST_FLAT_STATUS', 'MODULE_SHIPPING_USPOST_FLAT_COST', 'MODULE_SHIPPING_USPOST_FLAT_TAX_CLASS', 'MODULE_SHIPPING_USPOST_FLAT_TAX_BASIS', 'MODULE_SHIPPING_USPOST_FLAT_ZONE', 'MODULE_SHIPPING_USPOST_FLAT_SORT_ORDER');
    }
  }
?>

Place in /includes/languages/english/modules/shipping as uspostflat.php

<?php
//
// +----------------------------------------------------------------------+
// |zen-cart Open Source E-commerce                                       |
// +----------------------------------------------------------------------+
// | Copyright (c) 2003 The zen-cart developers                           |
// |                                                                      |   
// | http://www.zen-cart.com/index.php                                    |   
// |                                                                      |   
// | Portions Copyright (c) 2003 osCommerce                               |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the GPL license,       |
// | that is bundled with this package in the file LICENSE, and is        |
// | available through the world-wide-web at the following url:           |
// | http://www.zen-cart.com/license/2_0.txt.                             |
// | If you did not receive a copy of the zen-cart license and are unable |
// | to obtain it through the world-wide-web, please send a note to       |
// | [email protected] so we can mail you a copy immediately.          |
// +----------------------------------------------------------------------+
// Based on flat.php 1969 2005-09-13 06:57:21Z drbyte $
// $Id: uspostflat.php 1969 2015-01-15 11:55:00:21Z mrudden $
//

define('MODULE_SHIPPING_USPOST_FLAT_TEXT_TITLE', 'USPOST Flat Rate');
define('MODULE_SHIPPING_USPOST_FLAT_TEXT_DESCRIPTION', 'USPOST Flat Rate');
define('MODULE_SHIPPING_USPOST_FLAT_TEXT_WAY', 'We only ship using USPS Flat Rate Boxes. You will receive your order in ');
?>