Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

Changing default shipping option from cheapest to flat


Terra

Recommended Posts

I usually feel quite confident with PHP but today I'm stuck. I'm trying to change the default shipping option from the cheapest to the flat rate.

 

The code which generates the default option is in checkout_shippping.php around line 140:

 

// if no shipping method has been selected, automatically select the cheapest method.
// if the modules status was changed when none were available, to save on implementing
// a javascript force-selection method, also automatically select the cheapest shipping
// method if more than one module is now enabled
 if ( !tep_session_is_registered('shipping') || ( tep_session_is_registered('shipping') && ($shipping == false) && (tep_count_shipping_modules() > 1) ) ) $shipping = $shipping_modules->cheapest();

 

Now all I need to do is change the reference from "cheapest()" to the flat rate (which I think is defined as 'flat_flat') ... but I've got a problem, somehow I just can't specify the correct array.

 

Any PHP coders who can give me a hint (or kick) in the right direction? :'(

 

many thanks, Terra

My code for combining PayPal IPN with ** QTPro 4.25 ** osC Affiliate ** CCGV(trad)

and how to solve the invoice already paid error

General info: Allow customer to delete order comment ** FTP Programs & Text Editors ** Amending order email **

Link to comment
Share on other sites

in your catalog\includes\classes\shipping.php locate this code:

 

				$rates[] = array('id' => $quotes['id'] . '_' . $quotes['methods'][$i]['id'],
							 'title' => $quotes['module'] . ' (' . $quotes['methods'][$i]['title'] . ')',
							 'cost' => $quotes['methods'][$i]['cost']);

 

right below it add:

 

  if( $quotes['id'] == 'flat' )
$cheapest = $rates[count($rates)-1];

 

further down just before this line:

 

$cheapest = false;

 

add this:

if( isset($cheapest) )
 return $cheapest;

Link to comment
Share on other sites

Hi Enigma - many thanks! I was playing around with shipping.php but just didn't get the right code. I have amended your suggestion above slightly and it's working - so many thanks for replying to my post and the code snippet :thumbsup:

 

My amended code:

 

I replaced this:

		$cheapest = false;
	for ($i=0, $n=sizeof($rates); $i<$n; $i++) {
	  if (is_array($cheapest)) {
		if ($rates[$i]['cost'] < $cheapest['cost']) {
		  $cheapest = $rates[$i];
		}
	  } else {
		$cheapest = $rates[$i];
	  }
	}

	return $cheapest;
  }
}
 }

 

with this:

		 if($quotes['id'] == 'flat') {
	 $cheapest = $rates[count($rates)-1];
}else {
	$cheapest = false;
	for ($i=0, $n=sizeof($rates); $i<$n; $i++) {
	  if (is_array($cheapest)) {
		if ($rates[$i]['cost'] < $cheapest['cost']) {
		  $cheapest = $rates[$i];
		}
	  } else {
		$cheapest = $rates[$i];
	  }
	}

	return $cheapest;
  }
}
 }

 

I've also created a new function rather than amending the existing one, just in case I want to switch back.

 

One happy coding bunny!

Edith

My code for combining PayPal IPN with ** QTPro 4.25 ** osC Affiliate ** CCGV(trad)

and how to solve the invoice already paid error

General info: Allow customer to delete order comment ** FTP Programs & Text Editors ** Amending order email **

Link to comment
Share on other sites

No - was wrong - my code stinks - went back to your way!

My code for combining PayPal IPN with ** QTPro 4.25 ** osC Affiliate ** CCGV(trad)

and how to solve the invoice already paid error

General info: Allow customer to delete order comment ** FTP Programs & Text Editors ** Amending order email **

Link to comment
Share on other sites

Hi Enigma,

 

I have one query - I know your code works (it's installed and running) but I can't figure out WHY it works. This line:

 $cheapest = $rates[count($rates)-1];

counts all $rates and then deducts 1 ... which on my setting (3 shipping modules installed) would give you "2" .. but how does that set the array to flat?

 

I'm having another look on php.net to understand this - my array knowledge is still very shaky.

 

many thanks, Edith

My code for combining PayPal IPN with ** QTPro 4.25 ** osC Affiliate ** CCGV(trad)

and how to solve the invoice already paid error

General info: Allow customer to delete order comment ** FTP Programs & Text Editors ** Amending order email **

Link to comment
Share on other sites

Hi Enigma,

 

I have one query - I know your code works (it's installed and running) but I can't figure out WHY it works. This line:

 $cheapest = $rates[count($rates)-1];

counts all $rates and then deducts 1 ... which on my setting (3 shipping modules installed) would give you "2" .. but how does that set the array to flat?

 

I'm having another look on php.net to understand this - my array knowledge is still very shaky.

 

many thanks, Edith

no, it does not count all rates of all shipping modules. It only gets the last rate returned from the flat shippng module. And then it assigns the object to the $cheapest variable. The rates (array) has several elements for each shipping module. And you want to extract the one for the flat. It is where the "if" statement above comes into play.

Link to comment
Share on other sites

Okay, yes, you're right of course. Would the code then also work like this: $cheapest = $rates[$i];

and like this: $cheapest = $rates[0]; ?

 

I'm just not sure why I need the count - 1 in there.

 

many thanks - it's really appreciated. Edith

Edited by Terra

My code for combining PayPal IPN with ** QTPro 4.25 ** osC Affiliate ** CCGV(trad)

and how to solve the invoice already paid error

General info: Allow customer to delete order comment ** FTP Programs & Text Editors ** Amending order email **

Link to comment
Share on other sites

you need the count/size because you do not know the current index in the array when you hit the 'flat'. There is no counter for it because of the if statement so you do not know the number of elements in the array when it finds the 'flat' module.

Link to comment
Share on other sites

*noise of penny dropping* - many thanks for explaining! Like Cher used to sing "I can see clearly now!". Edith

My code for combining PayPal IPN with ** QTPro 4.25 ** osC Affiliate ** CCGV(trad)

and how to solve the invoice already paid error

General info: Allow customer to delete order comment ** FTP Programs & Text Editors ** Amending order email **

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...