Jump to content


Photo

Help modifying a third party File - for osCommerce


  • Please log in to reply
No replies to this topic

#1 Bigbird_3156

Bigbird_3156

    Newbie

  • New Members
  • Pip
  • 1 posts

Posted 19 March 2014 - 07:43 PM

Hi,

 

I am setting up a site in oscommerce, and as I modify it I am trying to learn PHP on the go.

 

I have this third party module that I am trying to modify to suit my purposes but I cannot get it to work and I am not sure why (I have gone onto the osCommerce help forum but no one has helped me other than telling my to download other modules... but I want to crack this one myself).

 

THe module I am working with is a shipping module,

 

The basic idea of the original module is this....

 

1. Chooses the delivery State (US) $dest_state

2. allocates an appropriate postage zone based on this state $dest_zone(?)

3. Allocates a postage cost $interstate_cost based on the weight of the order $shipping_weight and the zone

 

the Flow as it works in my mind is:

 

Get STATE -> Allocate ZONE based on State -> allocate COST based on ZONE and WEIGHT

 

What I have tried to do:

 

1. Changed delivery state $dest_state to delivery postcode  $dest_postcode

2. Through a series of very cumbersome If statements allocate a zone which I have put into $dest_state based on the postcode selected in part 1

 

The flow as it works in my mind has only added a couple of steps

get POSTCODE -> Allocate STATE based on POSTCODE -> Allocate ZONE based on State -> allocate COST based on ZONE and WEIGHT

 

I have put an echo statement in my code to check the value of $dest_postcode and $dest_state and these seem to work, but that is about as far as it goes, it does not spit out any more info...

 

I have a few questions about this:

 

1. have I put my if statements (which seem to work) in the right place? or is there another problem that makes it not work

2. is there a more streamlined and possibly faster way to do the if statements

 

I have added a BOF and EOF comment to show the modifications I have made plus a few other comments to explain stuff.

 

I hope this is not too much to ask.

 

Thanks.

<?php
/*
  $Id: Interstate.php
  Contributed by Anita L. Cross (aka skittles)
  (http://www.callofthewildphoto.com)

  Zone Shipping: By State Within A Country
  Developed For USA. Can be adapted to other countries.
  Based on Zones.php, distributed with osCommerce MS2.2 (051113)

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

  Released under the GNU General Public License
*/

  class interstate {
    var $code, $title, $description, $enabled, $num_zones;

// class constructor
    function interstate() {
      $this->code = 'interstate';
      $this->title = MODULE_SHIPPING_INTERSTATE_TEXT_TITLE;
      $this->description = MODULE_SHIPPING_INTERSTATE_TEXT_DESCRIPTION;
      $this->sort_order = MODULE_SHIPPING_INTERSTATE_SORT_ORDER;
      $this->icon = '';
      $this->tax_class = MODULE_SHIPPING_INTERSTATE_TAX_CLASS;
      $this->enabled = ((MODULE_SHIPPING_INTERSTATE_STATUS == 'True') ? true : false);

      // CUSTOMIZE THIS SETTING FOR THE NUMBER OF ZONES NEEDED
      $this->num_zones = 16;
    }
//MODULE_SHIPPING_
// class methods
    function quote($method = '') {
      global $order, $shipping_weight, $shipping_num_boxes;

      // if delivery is to other country, skip module in cart
      if ($order->delivery['country']['iso_code_2'] != 'AU') return; // obviously this was origninally the US

 // BOF - Select Postcode from DB and Assign it a postage zone ($dest_state)
	 //$dest_state = $order->delivery['state'];  ***** MOD ******
	  $dest_postcode = $order->delivery['postcode']; 
	  
	  	// Set VIC Zone 1 Postcodes to V1
	  	if (($dest_postcode >= 3000) && ($dest_postcode <= 3220)
			|| ($dest_postcode >= 3335) && ($dest_postcode <= 3341)
			|| ($dest_postcode >= 3425) && ($dest_postcode <= 3443)
			|| ($dest_postcode >= 3750) && ($dest_postcode <= 3811)
			|| ($dest_postcode >= 3910) && ($dest_postcode <= 3920)
			|| ($dest_postcode >= 3926) && ($dest_postcode <= 3944)
			|| ($dest_postcode >= 3972) && ($dest_postcode <= 3978)
			|| ($dest_postcode >= 3980) && ($dest_postcode <= 3983)
			|| ($dest_postcode >= 8000) && ($dest_postcode <= 8999)) {
												
				$dest_state = 'V1';
				}
			
		// Set VIC Zone 2 Postcodes to V2		
		elseif (($dest_postcode == 2648)
				|| ($dest_postcode == 2715)
				|| ($dest_postcode >= 2717) && ($dest_postcode <= 2719)
				|| ($dest_postcode >= 2731) && ($dest_postcode <= 2739)
				|| ($dest_postcode >= 3221) && ($dest_postcode <= 3334)
				|| ($dest_postcode >= 3342) && ($dest_postcode <= 3424)
				|| ($dest_postcode >= 3444) && ($dest_postcode <= 3749)
				|| ($dest_postcode >= 3812) && ($dest_postcode <= 3909)
				|| ($dest_postcode >= 3921) && ($dest_postcode <= 3925)
				|| ($dest_postcode >= 3945) && ($dest_postcode <= 3971)
				|| ($dest_postcode == 3979)
				|| ($dest_postcode >= 2984) && ($dest_postcode <= 3999)) {
														
					$dest_state = 'V2';
					}

		// Set NSW Zone 1 Postcodes to N
		elseif (($dest_postcode >= 1000) && ($dest_postcode <= 2263)
				|| ($dest_postcode >= 2500) && ($dest_postcode <= 2530)
				|| ($dest_postcode >= 2555) && ($dest_postcode <= 2574)
				|| ($dest_postcode >= 2740) && ($dest_postcode <= 2786)
				|| ($dest_postcode == 2890)) {
								
					$dest_state = 'N1';
					}												
		
		// Set NSW Zone 2 Postcodes to N2		
		elseif (($dest_postcode >= 0200) && ($dest_postcode <= 0229) 				
				|| ($dest_postcode >= 2264) && ($dest_postcode <= 2499) 
				||($dest_postcode >= 2531) && ($dest_postcode <= 2554) 
				|| ($dest_postcode >= 2575) && ($dest_postcode <= 2647) 
				|| ($dest_postcode >= 2649) && ($dest_postcode <= 2714)
				|| ($dest_postcode == 2716)
				|| ($dest_postcode >= 2720) && ($dest_postcode <= 2730)
				|| ($dest_postcode >= 2787) && ($dest_postcode <= 2879)
				|| ($dest_postcode >= 2881) && ($dest_postcode <= 2889)
				|| ($dest_postcode >= 2891) && ($dest_postcode <= 2898)
				|| ($dest_postcode >= 2900) && ($dest_postcode <= 2999)) {
													 
					 $dest_state = 'N2'; 
					 }
		
		// Set QLD Zone 1 Postcodes to Q1
		elseif (($dest_postcode >= 4000) && ($dest_postcode <= 4299)
				|| ($dest_postcode >= 4500) && ($dest_postcode <= 4549)
				|| ($dest_postcode >= 9000) && ($dest_postcode <= 9299)
				|| ($dest_postcode >= 9400) && ($dest_postcode <= 9596)
				|| ($dest_postcode >= 9700) && ($dest_postcode <= 9799)) {
								
					$dest_state = 'Q1';
					}
					
		// Set QLD Zone 2 Postcodes to Q2					
		elseif (($dest_postcode >= 4300) && ($dest_postcode <= 4449)
				|| ($dest_postcode >= 4550) && ($dest_postcode <= 4699)
				|| ($dest_postcode >= 9597) && ($dest_postcode <= 9599)
				|| ($dest_postcode >= 9880) && ($dest_postcode <= 9919)) {
						
					$dest_state = 'Q2' ;
					}
		
		// Set QLD Zone 3 Postcodes to Q3
		elseif (($dest_postcode >= 4450) && ($dest_postcode <= 4499)
				|| ($dest_postcode >= 4700) && ($dest_postcode <= 4805)
				|| ($dest_postcode >= 9920) && ($dest_postcode <= 9959)){
						
					$dest_state = 'Q3' ;
					}
		
		// Set QLD Zone 4 Postcodes to Q4
		elseif (($dest_postcode >= 9960) && ($dest_postcode <= 9999)) {
			
					$dest_state = 'Q4';	
				}
		
		// Set SA Zone 1 Postcodes to S1
		elseif (($dest_postcode >= 5000) && ($dest_postcode <= 5199)
				|| ($dest_postcode >= 5800) && ($dest_postcode <= 5999)) {
					
					$dest_state = 'S1';
				}
		
		// Set SA Zone 2 Postcodes to S2
		elseif  (($dest_postcode == 2880)
				|| ($dest_postcode >= 5200) && ($dest_postcode <= 5749)) {
					
					$dest_state = 'S2';
				}		
		
		// Set NT Zone 1 Postcodes to NT1
		elseif (($dest_postcode >= 0800) && ($dest_postcode <= 0999)) {
			
				$dest_state = 'NT1';
		}
		
		// Set WA Zone 1 Postcodes to W1
		elseif (($dest_postcode >= 6000) && ($dest_postcode <= 6214)
				|| ($dest_postcode >= 6800) && ($dest_postcode <= 6999)) {
					
					$dest_state = 'W1';
				}
		
		// Set WA Zone 2 Postcodes to W2
		elseif  (($dest_postcode >= 6215) && ($dest_postcode <= 6699)) {
			
				$dest_state = 'W2';
				}
		
		// Set WA Zone 3 Postcodes to W3
		elseif (($dest_postcode >= 6700) && ($dest_postcode <= 6799)) {
			
				$dest_state = 'W3';
				}
		
		// Set TAS Zone 1 Postcodes to T1
		elseif (($dest_postcode >= 7000) && ($dest_postcode <= 7999)) {
			
				$dest_state = 'T1';
				}
		
		// Set NF Island  Postcode to NF
		elseif  ($dest_postcode == 2899)		 {
			
				$dest_state = 'NF';
		}
		
		// If no valid postcode
		else {
			$dest_state = 'ERROR'; // I still need to put in some sort of routine for this error 
		}
				
	echo ($dest_postcode . ' ' . $dest_state ); // This is just to test that the variable contain info & they do
// EOF - Select Postcode from DB and Assign it a postage zone ($dest_state)


      $dest_zone = 0;
      $error = false;

      for ($i=1; $i<=$this->num_zones; $i++) {
        $state_table = constant('MODULE_SHIPPING_INTERSTATE_STATES_' . $i);
        $state_zones = explode(',', $state_table);
        if (in_array($dest_state, $state_zones)) {
          $dest_zone = $i;
          break;
        }
      }
      if ($dest_zone == 0) {
        $error = true;
      } else {
        $shipping = -1;
        $interstate_cost = constant('MODULE_SHIPPING_INTERSTATE_COST_' . $dest_zone);
        $interstate_table = split("[:,]" , $interstate_cost);
        $size = sizeof($interstate_table);
        for ($i=0; $i<$size; $i+=2) {
          if ($shipping_weight <= $interstate_table[$i]) {
            $shipping = $interstate_table[$i+1];
            $shipping_method = MODULE_SHIPPING_INTERSTATE_TEXT_WAY . ' ' 
                             . $dest_state . ' : ' 
                             . $shipping_weight . ' ' 
                             . MODULE_SHIPPING_INTERSTATE_TEXT_UNITS;
            break;
          }
        }

        if ($shipping == -1) {
          $shipping_cost = 0;
          $shipping_method = MODULE_SHIPPING_INTERSTATE_UNDEFINED_RATE;
        } else {
          $shipping_cost = ($shipping * $shipping_num_boxes) + constant('MODULE_SHIPPING_INTERSTATE_HANDLING_' . $dest_zone);
        }
      }

      $this->quotes = array('id' >= $this->code,
                            'module' >= MODULE_SHIPPING_INTERSTATE_TEXT_SECTION_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_SHIPPING_INTERSTATE_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_SHIPPING_INTERSTATE_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 Aust States Method', 'MODULE_SHIPPING_INTERSTATE_STATUS', 'True', 'Offer Australian specific zone rates by state?', '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_SHIPPING_INTERSTATE_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_SHIPPING_INTERSTATE_SORT_ORDER', '0', 'Sort order of display.', '6', '0', now())");

      for ($i = 1; $i <= $this->num_zones; $i++) {
        $default_states = '';
        $default_rates = '';
        if ($i == 1) {
          $default_states = 'N1'; // These are default states (shipping zones) and  Weights:Charges for each zone  
          $default_rates = '0.125:1.01,0.250:2.01,0.500:4.01,3.000:10.01,6.000:20.01,9.000:30.01';
        }
        if ($i == 2) {
          $default_states = 'N2';
          $default_rates = '0.125:1.00,0.250:2.00,0.500:4.00,3.000:10.00,6.000:20.00,9.000:30.00';
        }
        if ($i == 3) {
          $default_states = 'V1';
          $default_rates = '3:10.00,7:16.00,99:50.00';
        }
        if ($i == 4) {
          $default_states = 'V2';
          $default_rates = '3:11.00,7:18.00,99:60.00';
        }
        if ($i == 5) {
          $default_states = 'Q1';
          $default_rates = '3:20.00,7:30.00,99:90.00';
        }
		if ($i == 6) {
          $default_states = 'Q2';
          $default_rates = '3:20.00,7:30.00,99:90.00';
        }
		if ($i == 7) {
          $default_states = 'Q3';
          $default_rates = '3:20.00,7:30.00,99:90.00';
        }
		if ($i == 8) {
          $default_states = 'Q4';
          $default_rates = '3:20.00,7:30.00,99:90.00';
        }
		if ($i == 9) {
          $default_states = 'S1';
          $default_rates = '3:20.00,7:30.00,99:90.00';
        }
		if ($i == 10) {
          $default_states = 'S2';
          $default_rates = '3:20.00,7:30.00,99:90.00';
        }
		if ($i == 11) {
          $default_states = 'NT1';
          $default_rates = '3:20.00,7:30.00,99:90.00';
        }
		if ($i == 12) {
          $default_states = 'W1';
          $default_rates = '3:20.00,7:30.00,99:90.00';
        }
		if ($i == 13) {
          $default_states = 'W2';
          $default_rates = '3:20.00,7:30.00,99:90.00';
        }
		if ($i == 14) {
          $default_states = 'W3';
          $default_rates = '3:20.00,7:30.00,99:90.00';
        }
		if ($i == 15) {
          $default_states = 'T1';
          $default_rates = '3:20.00,7:30.00,99:90.00';
        }
		if ($i == 16) {
          $default_states = 'NF';
          $default_rates = '3:20.00,7:30.00,99:90.00';
        }
        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 ." States', 'MODULE_SHIPPING_INTERSTATE_STATES_" . $i ."', '" . $default_states . "', 'Comma separated list of states - use full name not abreviation for 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_SHIPPING_INTERSTATE_COST_" . $i ."', '" . $default_rates . "', '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_SHIPPING_INTERSTATE_HANDLING_" . $i."', '0.00', '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_SHIPPING_INTERSTATE_STATUS', 'MODULE_SHIPPING_INTERSTATE_TAX_CLASS', 'MODULE_SHIPPING_INTERSTATE_SORT_ORDER');
      for ($i=1; $i<=$this->num_zones; $i++) {
        $keys[] = 'MODULE_SHIPPING_INTERSTATE_STATES_' . $i;
        $keys[] = 'MODULE_SHIPPING_INTERSTATE_COST_' . $i;
        $keys[] = 'MODULE_SHIPPING_INTERSTATE_HANDLING_' . $i;
      }
      return $keys;
    }
  }
?>






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com