Jump to content
Juto

Duplicating zones.php I do need help, please

Recommended Posts

Hi, I have duplicated zones.php (incl. language files) in order to offer 5 shipping options from economy mail to ship as gift. Since I didn't like to input the table date each time while testing, also included those tables in the code:

 

At the admin side it shows up as expected, but no options shows on the catalog side plus, the actual payment (money order) doesn't show I only get "This is the only payment method available..."

 

The buying process goes through, despite the above. So, I need any advice to make this work:

 

<?php
/*
 DomesticDelivery.php based upon:
 $Id: zones.php 1739 2007-12-20 00:52:16Z hpdl $
 osCommerce, Open Source E-Commerce Solutions
 http://www.oscommerce.com
 Copyright (c) 2003 osCommerce
 Released under the GNU General Public License
*/
 class DomesticDelivery {
   var $code, $title, $description, $enabled, $num_zones;
// class constructor
   function DomesticDelivery() {
  $this->code = 'DomesticDelivery';
  $this->icon = DIR_WS_IMAGES . 'shipping/posten_logo_small.gif';
   $this->title = (defined('MODULE_DOMESTICDELIVERY_TEXT_TITLE') ? 'Posten Logistik AB: '. MODULE_DOMESTICDELIVERY_TEXT_TITLE .' Valid From 2013-01-01' : null);
   $this->description = (defined('MODULE_DOMESTICDELIVERY_TEXT_DESCRIPTION') ? MODULE_DOMESTICDELIVERY_TEXT_DESCRIPTION : null);
   $this->sort_order = (defined('MODULE_DOMESTICDELIVERY_SORT_ORDER') ? MODULE_DOMESTICDELIVERY_SORT_ORDER : 0);
   $this->tax_class = (defined('MODULE_DOMESTICDELIVERY_TAX_CLASS') ? MODULE_DOMESTICDELIVERY_TAX_CLASS : null);
   $this->enabled = ((defined('MODULE_DOMESTICDELIVERY_STATUS') && (MODULE_DOMESTICDELIVERY_STATUS == 'true')) ? true : false);
// CUSTOMIZE THIS SETTING FOR THE NUMBER OF ZONES NEEDED
  $this->num_zones = 5;
   }
// class methods
   function quote($method = '') {
  global $order, $shipping_weight, $shipping_num_boxes;
  $dest_country = $order->delivery['country']['iso_code_2'];
  $dest_zone = 0;
  $error = false;
  for ($i=1; $i<=$this->num_zones; $i++) {
    $countries_table = constant('MODULE_DOMESTICDELIVERY_COUNTRIES_' . $i);
    $country_zones = preg_split("/[,]/", $countries_table);
    if (in_array($dest_country, $country_zones)) {
	  $dest_zone = $i;
	  break;
    }
  }
  if ($dest_zone == 0) {
    $error = true;
  } else {
    $shipping = -1;
    $zones_cost = constant('MODULE_DOMESTICDELIVERY_COST_' . $dest_zone);
    $zones_table = preg_split("/[:,]/" , $zones_cost);
    $size = sizeof($zones_table);
    for ($i=0; $i<$size; $i+=2) {
	  if ($shipping_weight <= $zones_table[$i]) {
	    $shipping = $zones_table[$i+1];
	    $shipping_method = MODULE_DOMESTICDELIVERY_TEXT_WAY . ' ' . $dest_country . ' : ' . $shipping_weight . ' ' . MODULE_DOMESTICDELIVERY_TEXT_UNITS;
	    break;
	  }
    }
    if ($shipping == -1) {
	  $shipping_cost = 0;
	  $shipping_method = MODULE_DOMESTICDELIVERY_UNDEFINED_RATE;
    } else {
	  $shipping_cost = ($shipping * $shipping_num_boxes) + constant('MODULE_DOMESTICDELIVERY_HANDLING_' . $dest_zone);
    }
  }
  $this->quotes = array('id' => $this->code,
					    'module' => MODULE_DOMESTICDELIVERY_TEXT_TITLE,
					    'methods' => array(array('id' => $this->code,
												 'title' => $shipping_method,
												 'cost' => $shipping_cost)));
  if ($this->tax_class > 0) {
    $this->quotes['tax'] = tep_get_tax_rate($this->tax_class, $order->delivery['country']['id'], $order->delivery['zone_id']);
  }
  if (tep_not_null($this->icon)) $this->quotes['icon'] = tep_image($this->icon, $this->title);
  if ($error == true) $this->quotes['error'] = MODULE_DOMESTICDELIVERY_INVALID_ZONE;
  return $this->quotes;
   }
   function check() {
  if (!isset($this->_check)) {
    $check_query = tep_db_query("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_DOMESTICDELIVERY_STATUS'");
    $this->_check = tep_db_num_rows($check_query);
  }
  return $this->_check;
   }
   function install() {
  tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) VALUES ('Enable Zones Method', 'MODULE_DOMESTICDELIVERY_STATUS', 'True', 'Do you want to offer zone rate shipping?', '6', '0', 'tep_cfg_select_option(array(\'True\', \'False\'), ', now())");
  tep_db_query("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_DOMESTICDELIVERY_TAX_CLASS', '0', 'Use the following tax class on the shipping fee.', '6', '0', 'tep_get_tax_class_title', 'tep_cfg_pull_down_tax_classes(', now())");
  tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Sort Order', 'MODULE_DOMESTICDELIVERY_SORT_ORDER', '0', 'Sort order of display.', '6', '0', now())");
  for ($i = 1; $i <= $this->num_zones; $i++) {
  $current_rate = null;
  switch ($i) {
    case 1:
//Zon 1 = option 1, MailEconomy shipping rate:
	  $current_rate = '0.020:5.50,0.100:11.00,0.250:22.00,0.500:33.00,1.00:44.00,2.00:66.00,10000:0.00';
    break;
    case 2:
//Zon 2 = option 2, MailPrioritaire shipping rate:
	  $current_rate = '0.020:6.00,0.100:12.00,0.250:24.00,0.500:36.00,1.00:48.00,2.00:72.00,10000:0.00';
    break;
    case 3:
//Zon 3 = option 3, MailPrioritaireRek shipping rate:
	  $current_rate = '0.020:71.00,0.100:77.00,0.250:89.00,0.500:101.00,1.00:113.00,2.00:137.00,10000:0.00';
    break;
    case 4:
//Zon 4 = option 4, Parcel shipping rate:
	  $current_rate = '3.00:155.00,5.00:185.00,10.00:235.00,15.00:285.00,20.00:330.00,10000:0.00';
    break;
//Zon 5 = option 5, ParcelGift shipping rate:
    case 5:
	  $current_rate = '2.00:55.00,10000:0.00';
    break;
  }
    $default_countries = 'SE';
    tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Zone " . $i ." Countries', 'MODULE_DOMESTICDELIVERY_COUNTRIES_" . $i ."', '" . $default_countries . "', 'Comma separated list of two character ISO country codes that are part of Zone " . $i . ".', '6', '0', now())");
    tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Zone " . $i ." Shipping Table', 'MODULE_DOMESTICDELIVERY_COST_" . $i ."', '" . $current_rate . "', '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.', '6', '0', now())");
    tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Zone " . $i ." Handling Fee', 'MODULE_DOMESTICDELIVERY_HANDLING_" . $i."', '0', 'Handling Fee for this shipping zone', '6', '0', now())");
  }
   }
   function remove() {
  tep_db_query("delete from " . TABLE_CONFIGURATION . " where configuration_key in ('" . implode("', '", $this->keys()) . "')");
   }
   function keys() {
  $keys = array('MODULE_DOMESTICDELIVERY_STATUS', 'MODULE_DOMESTICDELIVERY_TAX_CLASS', 'MODULE_DOMESTICDELIVERY_SORT_ORDER');
  for ($i=1; $i<=$this->num_zones; $i++) {
    $keys[] = 'MODULE_DOMESTICDELIVERY_COUNTRIES_' . $i;
    $keys[] = 'MODULE_DOMESTICDELIVERY_COST_' . $i;
    $keys[] = 'MODULE_DOMESTICDELIVERY_HANDLING_' . $i;
  }
  return $keys;
   }
 }
?>

 

I have tried for ages, but ... what ever I've tried didn't make it. I've looked and searched in the forums and with google... and I am at a stand still.

 

Please advice me. I need this going.

 

Many thanks

 

Sara

Share this post


Link to post
Share on other sites

Hi - in function quote() you have some code:

 

if ($dest_zone == 0) {

$error = true;

 

I'm wondering if the couple of lines above that are making $dest_zone = 0 so no 'quote' gets returned for the shipping page??

Share this post


Link to post
Share on other sites

@@Bob Terveuren

Hi! Thank you for answer, those lines are unchanged from the zones.php module. And at the end of that function there's this line:

 

if ($error == true) $this->quotes['error'] = MODULE_DOMESTICDELIVERY_INVALID_ZONE;

 

just before the quotes are returned.

 

Since "MODULE_DOMESTICDELIVERY_INVALID_ZONE" isn't displayed I don't think that the problem is with the quotes.

 

O' my... I am really lost..

 

Sara

Share this post


Link to post
Share on other sites

Hi

 

I tried to get your code to install but I was getting an admin error when trying to edit (although it did install and was visible in the configuration table in the admin) - it did not show in the front end though.

 

I'd try again from scratch if I were you and create a straight clone of zones.php - see if that installs & displays. If it does then uninstall, check the db for any left over config values then add in some of your code and try again.

 

I'd go for either the code in in install() or the code in DomesticDelivery() as the prime suspects

Share this post


Link to post
Share on other sites

@@Bob Terveuren

Hi Bob, thanks for trying to install it. I am sorry for my late answer, I had to be at the doctors for a tiny op.

 

First this line:

 

 

 $this->enabled = ((defined('MODULE_DOMESTICDELIVERY_STATUS') && (MODULE_DOMESTICDELIVERY_STATUS == 'true')) ? true : false);

 

Should be:

 $this->enabled = ((defined('MODULE_DOMESTICDELIVERY_STATUS') && (MODULE_DOMESTICDELIVERY_STATUS == 'True')) ? true : false);

 

Then, I think I've found a major bug, which concerns every module:

 

This is the function tep_cfg_select_option($select_array, $key_value, $key = '')

 

Used in the install code.

 

As an example in the ot_loworder_fee we have:

 

 tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Display Low Order Fee', 'MODULE_ORDER_TOTAL_LOWORDERFEE_STATUS', 'true', 'Do you want to display the low order fee?', '6', '1','tep_cfg_select_option(array(\'true\', \'false\'), ', now())");

 

A closer look at the line reveals that there are 6 left parenthesis, (, but only 5 right parenthesis, ), So they don't match up!

 

The error seems to be here:

 

tep_cfg_select_option(array(\'true\', \'false\'), ', now())

 

which should correspond to:

 

set_function, date_added

 

I seems to me that the above should read:

 

tep_cfg_select_option(array(\'true\', \'false\'), '', ''), now()

 

That is, the array is array(\'true\', \'false\') while the $key_value and $key are not set, maby they should be?

 

Thoughts?

 

Sara

Edited by Juto

Share this post


Link to post
Share on other sites

If that was truly a bug then none of these modules would ever install -- they would just throw a PHP fatal error and stop. If you look at the code carefully, one of the open parentheses is inside the single quotes tor the set_function field. It's actually inserting this code (the part in the single quotes) into the database:

 

'tep_cfg_select_option(array(\'true\', \'false\'), '

 

That value is extracted from the database and completed by the code that interprets the data. That's admin/modules.php in this case.

 

Regards

Jim


See my profile for a list of my addons and ways to get support.

Share this post


Link to post
Share on other sites

@@kymation

Hi Jim and thanks for looking into this.

 

However I do believe that the number of parenthesis must match, also in the insert list, the last parameters are:

set_function, date_added

That is, date_added is not part of the set_function.

 

I cant beleive that this is correct:

 

1)

tep_cfg_select_option(array(\'true\', \'false\'), ', now())

 

 

When looking at the function itself:

2)

function tep_cfg_select_option($select_array, $key_value, $key = '')

 

I can't see how 1) can match...

 

kindly advice

 

Thanks for looking, Jim. Hopefully this can be resolved.

 

Sara

Share this post


Link to post
Share on other sites

Parenthesess do not always have to match. Consider this code:

 

my_function( '))))))))))' );

 

That is correct and valid PHP. It supplied a text string as a parameter to the function. The text string contains parentheses, but they are not PHP code, they are text. Text does not count, no matter what it is in.

 

In the case that you posted, 'tep_cfg_select_option(array(\'true\', \'false\'),' is text. It doesn't matter what is in it, it's still just text. Yes, single quotes need to be escaped to prevent confusing the PHP parser. It's still just text.

 

That text is stored in the database and then retrieved as text. The code in modules.php retrieves that text and adds some variables to it so that it becomes 'tep_cfg_select_option(array(\'true\', \'false\'), \'foo\', \'bar\')'. At this point it is still just text. The code then runs that text string through eval() which causes it to become executable code.

 

Regards

Jim


See my profile for a list of my addons and ways to get support.

Share this post


Link to post
Share on other sites

@@kymation

Goodmorning Jim, yes you are quite right, i.e the closing right parenthesis is within:

eval('$keys .= ' . $value['set_function'] . "'" . $value['value'] . "', '" . $key . "');");

 

I see now that the db insert needs to be that way, otherwise the $value and the $key would be left out from the function.

So it's not a bug, it was a learning lesson for me! Thanks Jim!

 

Sara

Share this post


Link to post
Share on other sites

You're welcome. I don't mind teaching somebody who is willing to learn. Not that I'm all that good a teacher, but I do try.

 

Regards

Jim


See my profile for a list of my addons and ways to get support.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×