Forums / General Questions / Write returned shipping quote vaules to order table

Write returned shipping quote vaules to order table

Views: 1,565

Results 1 to 20 of 23
12 Aug 2014, 1:39 AM
#1
marcopolo avatar

marcopolo

Totally Zenned

Join Date:
May 2008
Location:
United States
Posts:
524
Plugin Contributions:
2

Write returned shipping quote vaules to order table

I'm trying to write the quoted returned values from the following carriers UPS, FedEx, and USPS.

I just need to write:

UPS ground
FedEx Ground
USPS Priority Mail

to the below tables I created in the orders database called:

shipping_rate_ups
shipping_rate_fedex
shipping_rate_usps

I updated the includes/classes/order.php and can write to the new tables using the following code:

'shipping_rate_ups' => $_SESSION['shipping']['cost'],
'shipping_rate_fedex' => $_SESSION['shipping']['cost'],
'shipping_rate_usps' => $_SESSION['shipping']['cost']);

I used the $_SESSION['shipping']['cost'] just to test the writing of the new tables which worked fine. Now I just need to grab the actual quoted returned values but I'm stuck and have no idea how to do it? Any help would be much appreciated!

marcopolo
Zen Cart 2.2.2 | PHP 8.5.12 | MariaDB 10.11.14

13 Aug 2014, 2:43 AM
#2
bislewl avatar

bislewl

Totally Zenned

Join Date:
Dec 2011
Location:
Wisconsin, USA
Posts:
674
Plugin Contributions:
12

Re: Write returned shipping quote vaules to order table

Ok...Can you clarify why you need to record the rates, they changes sometimes on a daily basis and there is virtually an infinite amount of combinations between zip-codes, weights, dimensions and services... Why not just use the live rates?

13 Aug 2014, 10:29 AM
#3
marcopolo avatar

marcopolo

Totally Zenned

Join Date:
May 2008
Location:
United States
Posts:
524
Plugin Contributions:
2

Re: Write returned shipping quote vaules to order table

I'm going to start offering a flat rate (best way) shipping option which will loose me some money per shipment since it will be cheaper then the carriers actual rates. It will be offered alongside those options (UPS ground, Fedex Ground, USPS) by recording the real rates at the time of order I can then reference those rates and see which carrier was the cheapest and use them for that shipment. Doing it this way will guarantee I'm using the cheapest carrier to ship the package.

marcopolo
Zen Cart 2.2.2 | PHP 8.5.12 | MariaDB 10.11.14

14 Aug 2014, 3:15 PM
#4
marcopolo avatar

marcopolo

Totally Zenned

Join Date:
May 2008
Location:
United States
Posts:
524
Plugin Contributions:
2

Re: Write returned shipping quote vaules to order table

Just in case I was not clear in the below picture, I'm trying to log the following rates highlighted in yellow in the new tables I created in orders database.Attachment 14403

marcopolo
Zen Cart 2.2.2 | PHP 8.5.12 | MariaDB 10.11.14

15 Aug 2014, 1:58 PM
#5
ajeh avatar

ajeh

Oba-san

Join Date:
Sep 2003
Location:
Ohio
Posts:
62,757
Plugin Contributions:
1

Re: Write returned shipping quote vaules to order table

You appear to want to save the lowest cost of each shipping module to your order ...

For USPS you could customize the code:
/includes/modules/shipping/usps.php

around line 380 to include the code in RED:

[B]      // bof: save lowest cost to $_SESSION['shipping_rate_usps']
[/B]      $_SESSION['shipping_rate_usps'] == 0;
[B]      // eof: save lowest cost to $_SESSION['shipping_rate_usps']
[/B]
      for ($i=0; $i<$PackageSize; $i++) {

Then around line 566 include the code in RED:

        $cost += (MODULE_SHIPPING_USPS_HANDLING_METHOD == 'Box') ? $usps_handling_fee * $shipping_num_boxes : $usps_handling_fee;
[B]        // bof: save lowest cost to $_SESSION['shipping_rate_usps']
        if ($_SESSION['shipping_rate_usps'] == 0 || $_SESSION['shipping_rate_usps'] > $cost) {
          $_SESSION['shipping_rate_usps'] = $cost;
        }
        // eof: save lowest cost to $_SESSION['shipping_rate_usps']
[/B]        // set the output title display name back to correct format
        $title = str_replace(array('RM', 'TM', '**'), array('®', '™', ''), $type_rebuilt);

Maybe this will help you on the other shipping modules ...

NOTE: I have not tested this code

Linda McGrath
If you have to think ... you haven't been zenned ...

**Did YOU buy the Zen Cart Team a cup of coffee and a donut today? Just click here to support the Zen Cart Team!!**

Are you using the latest? Perhaps you've a problem that's fixed in the latest version: [Upgrade today!]
Officially PayPal-Certified! Just click here

Try our Zen Cart Recommended Services - Hosting, Payment and more ...
Signup for our Announcements Forums to stay up to date on important changes and updates!

15 Aug 2014, 2:06 PM
#6
marcopolo avatar

marcopolo

Totally Zenned

Join Date:
May 2008
Location:
United States
Posts:
524
Plugin Contributions:
2

Re: Write returned shipping quote vaules to order table

Thank you so much I will test later today and report back!!

marcopolo
Zen Cart 2.2.2 | PHP 8.5.12 | MariaDB 10.11.14

15 Aug 2014, 8:57 PM
#7
marcopolo avatar

marcopolo

Totally Zenned

Join Date:
May 2008
Location:
United States
Posts:
524
Plugin Contributions:
2

Re: Write returned shipping quote vaules to order table

Ajeh It worked perfect, and I was able to use your code to modify the UPS and FedEx modules as well without issue! Thank you again!

marcopolo
Zen Cart 2.2.2 | PHP 8.5.12 | MariaDB 10.11.14

16 Aug 2014, 3:22 AM
#8
ajeh avatar

ajeh

Oba-san

Join Date:
Sep 2003
Location:
Ohio
Posts:
62,757
Plugin Contributions:
1

Re: Write returned shipping quote vaules to order table

You are most welcome ... thanks for the update that this worked for you ... :smile:

Linda McGrath
If you have to think ... you haven't been zenned ...

**Did YOU buy the Zen Cart Team a cup of coffee and a donut today? Just click here to support the Zen Cart Team!!**

Are you using the latest? Perhaps you've a problem that's fixed in the latest version: [Upgrade today!]
Officially PayPal-Certified! Just click here

Try our Zen Cart Recommended Services - Hosting, Payment and more ...
Signup for our Announcements Forums to stay up to date on important changes and updates!

9 Mar 2016, 3:39 AM
#9
marcopolo avatar

marcopolo

Totally Zenned

Join Date:
May 2008
Location:
United States
Posts:
524
Plugin Contributions:
2

Re: Write returned shipping quote vaules to order table

Ajeh,

This has been working great however I'm adding UPS Surepost and need to save that as well as the Ground rate, can the above code be modified to save two/three rates? UPS uses service codes which are:

Gound: 03
SurePost: 92 less than 1 lb
SurePost: 93 greater than 1 lb

There will on be either a 92 or 93 not both plus code 03

marcopolo
Zen Cart 2.2.2 | PHP 8.5.12 | MariaDB 10.11.14

9 Mar 2016, 10:23 PM
#10
marcopolo avatar

marcopolo

Totally Zenned

Join Date:
May 2008
Location:
United States
Posts:
524
Plugin Contributions:
2

Re: Write returned shipping quote vaules to order table

Sorry there is a typo in my post above I do not need to save the surepost as ground I need to save both surepost and ground rates separately.

marcopolo
Zen Cart 2.2.2 | PHP 8.5.12 | MariaDB 10.11.14

10 Mar 2016, 2:10 PM
#11
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: Write returned shipping quote vaules to order table

For every field to be captured, if you use a unique session variable name then you can capture as many such rates as coded. In the case of surepost, looks like only 92 or 93 will provide a non-zero rate based on weight. At least it would appear that way... Would test, but basically could use something like:

if($service_code == 92 || $service_code ==  93) {
$_SESSION['shipping_rate_surepost'] = $rate;
}

At the point where the rate is returned, using the variables specific to UPS, after setting that session variable to 0 and also put within an if statement like Ajeh suggested.

The only thing overall that I see that might be an issue is if the shipping contents/rate change after the initial data collection. It might not be/probably isn't an issue, but I would recommend testing that scenario to see if something more has to be done...

ZC Installation/Maintenance Support <- Site
Contribution for contributions welcome...

10 Mar 2016, 9:47 PM
#12
marcopolo avatar

marcopolo

Totally Zenned

Join Date:
May 2008
Location:
United States
Posts:
524
Plugin Contributions:
2

Re: Write returned shipping quote vaules to order table

Ok that got me on the right track, I had to modify it to the below for it to work:

			if ($rateReply->Service->Code == 03) {
			$_SESSION['shipping_rate_ups'] = $cost;
			}
			if ($rateReply->Service->Code == 92 || $rateReply->Service->Code ==  93) {
			$_SESSION['shipping_rate_ups_surepost'] = $cost;
			}

Thank you for your help!

marcopolo
Zen Cart 2.2.2 | PHP 8.5.12 | MariaDB 10.11.14

10 Mar 2016, 10:42 PM
#13
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: Write returned shipping quote vaules to order table

marcopolo:

Ok that got me on the right track, I had to modify it to the below for it to work:

		if ($rateReply->Service->Code == 03) {
		$_SESSION['shipping_rate_ups'] = $cost;
		}
		if ($rateReply->Service->Code == 92 || $rateReply->Service->Code ==  93) {
		$_SESSION['shipping_rate_ups_surepost'] = $cost;
		}
> 
> Thank you for your help!
Glad that it helped, but I notice that the tests that Ajeh included in her below code were not incorporated: ie if current session value is 0 or if current estimate is greater than the current session value.  I don't know the impact of the absence of that test, and it is that aspect that I encouraged doing your own testing to validate that the proper data is captured as expected, needed, or desired.

If incorporated I would suggest placing it within each of the above if statements, around the session assignments.

> **Ajeh:**
>
> You appear to want to save the lowest cost of each shipping module to your order ...
> 
> For USPS you could customize the code:
> /includes/modules/shipping/usps.php
> 
> around line 380 to include the code in **RED**:
> ```
[B]      // bof: save lowest cost to $_SESSION['shipping_rate_usps']
[/B]      $_SESSION['shipping_rate_usps'] == 0;
[B]      // eof: save lowest cost to $_SESSION['shipping_rate_usps']
[/B]
      for ($i=0; $i<$PackageSize; $i++) {

Then around line 566 include the code in RED:

    $cost += (MODULE_SHIPPING_USPS_HANDLING_METHOD == 'Box') ? $usps_handling_fee * $shipping_num_boxes : $usps_handling_fee;

[B] // bof: save lowest cost to $_SESSION['shipping_rate_usps']
if ($_SESSION['shipping_rate_usps'] == 0 || $_SESSION['shipping_rate_usps'] > $cost) {
$_SESSION['shipping_rate_usps'] = $cost;
}
// eof: save lowest cost to $_SESSION['shipping_rate_usps']
[/B]
// set the output title display name back to correct format
$title = str_replace(array('RM', 'TM', '**'), array('®', '™', ''), $type_rebuilt);

> 
> Maybe this will help you on the other shipping modules ...
> 
> NOTE: I have not tested this code

ZC Installation/Maintenance Support <- Site
Contribution for contributions welcome...

10 Mar 2016, 10:49 PM
#14
marcopolo avatar

marcopolo

Totally Zenned

Join Date:
May 2008
Location:
United States
Posts:
524
Plugin Contributions:
2

Re: Write returned shipping quote vaules to order table

Yes the below is in my code as Ajeh outlined:

			$_SESSION['shipping_rate_ups'] == 0;
			$_SESSION['shipping_rate_ups_surepost'] == 0;

marcopolo
Zen Cart 2.2.2 | PHP 8.5.12 | MariaDB 10.11.14

10 Mar 2016, 10:53 PM
#15
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: Write returned shipping quote vaules to order table

			if ($rateReply->Service->Code == 03) {
        if ($_SESSION['shipping_rate_ups'] == 0 || $_SESSION['shipping_rate_ups'] > $cost) {
			$_SESSION['shipping_rate_ups'] = $cost;
       }
			}
			if ($rateReply->Service->Code == 92 || $rateReply->Service->Code ==  93) {
        if ($_SESSION['shipping_rate_ups_surepost'] == 0 || $_SESSION['shipping_rate_ups_surepost'] > $cost) {
			$_SESSION['shipping_rate_ups_surepost'] = $cost;
       }
			}

Right, but it doesn't include what I have copied and pasted above...

ZC Installation/Maintenance Support <- Site
Contribution for contributions welcome...

10 Mar 2016, 10:56 PM
#16
marcopolo avatar

marcopolo

Totally Zenned

Join Date:
May 2008
Location:
United States
Posts:
524
Plugin Contributions:
2

Re: Write returned shipping quote vaules to order table

Got it I will update my code, thanks!

marcopolo
Zen Cart 2.2.2 | PHP 8.5.12 | MariaDB 10.11.14

11 Mar 2016, 1:47 AM
#17
marcopolo avatar

marcopolo

Totally Zenned

Join Date:
May 2008
Location:
United States
Posts:
524
Plugin Contributions:
2

Re: Write returned shipping quote vaules to order table

Just found an issue with this whole logic, on the first order the values are recorded however if the customer does not logout and completes another 2nd, 3rd etc order(s) the same values from the first order are recorded again even if the shipping rates on the 2nd, 3rd etc order(s) are higher or lower. Is there a way to reset the recorded values per order if a customer is still active on the original session?

marcopolo
Zen Cart 2.2.2 | PHP 8.5.12 | MariaDB 10.11.14

18 Mar 2016, 11:36 PM
#18
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,515
Plugin Contributions:
176

Re: Write returned shipping quote vaules to order table

You should probably reset any of your new $_SESSION variables at the end of checkout.

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

19 Mar 2016, 1:58 PM
#19
marcopolo avatar

marcopolo

Totally Zenned

Join Date:
May 2008
Location:
United States
Posts:
524
Plugin Contributions:
2

Re: Write returned shipping quote vaules to order table

DrByte:

You should probably reset any of your new $_SESSION variables at the end of checkout.

OK should this be done within the order define_checkout_success.php page?

Also is there a special way to reset a define or just put the following:

$_SESSION['shipping_rate_ups'] == 0;

marcopolo
Zen Cart 2.2.2 | PHP 8.5.12 | MariaDB 10.11.14

19 Mar 2016, 3:33 PM
#20
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: Write returned shipping quote vaules to order table

marcopolo:

OK should this be done within the order define_checkout_success.php page?

Also is there a special way to reset a define or just put the following:

$_SESSION['shipping_rate_ups'] == 0;


Point of clarification/terminology. The assignment above is not related to a "define" in the sense of PHP. A define is a constant... Constants are intended NOT to be modifiable. (That's a variable. :) )

But, to unset a variable well, unset($_SESSION['shipping_rate_ups']);

As to where in the code, there is a section I thought in the checkout_process header area but it might be as far in programming as the checkout_success section where other cart related session variables are reset/unset after the data has been captured for storage and technically I thought after that data has been captured in the database.

ZC Installation/Maintenance Support <- Site
Contribution for contributions welcome...