Jump to content
Sign in to follow this  
ztwistbooks

Subtract Media Mail price from Priority Mail?

Recommended Posts

Ok, I could use some hand-holding here....

 

We ship books, either USPS Media Mail or USPS Priority Mail.

 

If the customer wants Media Mail, it's free.

 

If the customer wants Priority Mail, subtract the Media Mail price from the Priority Mail price, and charge the difference.

 

Example: The actual cost of shipping Media Mail is $1.50. The actual price of shipping Priority Mail is $4.60. If the customer chooses Media Mail, it's free. If the customer chooses Priority Mail, it's $3.10.

 

Any hints on how to do this?

 

Note: I'm a competent programmer, but I don't know my way around OS Commerce. If I have to program a module for this, I'll need pointers on where to start.

Share this post


Link to post
Share on other sites

I ended up doing this myself by modifying the USPS Methods module.

 

First, I changed most of the occurrences of "usps" to "dusps". I don't know if there would be conflicts with the regular USPS module, but I didn't want to find out.

 

Then, I inserted code to find the lowest-price shipping option, then subtract that price from all the prices. So, the lowest-cost price drops to zero. Here is the relevant change from the modfied usps.php module:

 

      $duspsQuote = $this->_getQuote();

     if (is_array($duspsQuote)) {
       if (isset($duspsQuote['error'])) {
         $this->quotes = array('module' => $this->title,
                               'error' => $duspsQuote['error']);
       } else {
         $this->quotes = array('id' => $this->code,
                               'module' => $this->title . $shiptitle);

         $methods = array();
         $size = sizeof($duspsQuote);

         // vvvvvvvvvvvvvvvvvvv DISCOUNTED USPS vvvvvvvvvvvvvvvvvvvvvvv          
         // Find the cheapest method
         $cheapest = 1000000000;
         for ($i=0; $i<$size; $i++) {
           list($type, $cost) = each($duspsQuote[$i]);
           reset($duspsQuote[$i]);
           $cheapest = (($cost < $cheapest) ? $cost : $cheapest);
         }

         // error_log("Cheapest is ".$cheapest);
         // ^^^^^^^^^^^^^^^^^^^ DISCOUNTED USPS ^^^^^^^^^^^^^^^^^^^^^^^



         for ($i=0; $i<$size; $i++) {
           list($type, $cost) = each($duspsQuote[$i]);
           // error_log("Type $i is $type.  Cost $i is $cost.");

           $title = ((isset($this->types[$type])) ? $this->types[$type] : $type);
           if(in_array('Display transit time', explode(', ', MODULE_SHIPPING_DUSPS_OPTIONS)))    $title .= $transittime[$type];


                       if (MODULE_SHIPPING_DMSTC_INSURANCE_OPTION == 'Force Insurance') {
                               $methods[] = array('id' => $type,
                               'title' => $title,
                               // vvvvvvvvvvvvvvvvvvv DISCOUNTED USPS vvvvvvvvvvvvvvvvvvvvvvv
                               // 'cost' => ($cost + $insurance + $handling_cost[0]) * $shipping_num_boxes);
                               'cost' => ($cost - $cheapest + $insurance + $handling_cost[0]) * $shipping_num_boxes);
                               // ^^^^^^^^^^^^^^^^^^^ DISCOUNTED USPS ^^^^^^^^^^^^^^^^^^^^^^^
                       }
                        else {
                               $methods[] = array('id' => $type,
                                      'title' => $title,
                                      // vvvvvvvvvvvvvvvvvvv DISCOUNTED USPS vvvvvvvvvvvvvvvvvvvvvvv
                                      // 'cost' => ($cost + $handling_cost[0]) * $shipping_num_boxes);
                                       'cost' => ($cost - $cheapest + $handling_cost[0]) * $shipping_num_boxes);
                                      // ^^^^^^^^^^^^^^^^^^^ DISCOUNTED USPS ^^^^^^^^^^^^^^^^^^^^^^^
                       }
         }

         $this->quotes['methods'] = $methods;

Edited by ztwistbooks

Share this post


Link to post
Share on other sites

Sadly, the above method isn't good enough. When the user hits the "continue" (submit) button, it calls the quote function again. This time, though, it only uses one shipping method. That one is obviously the lowest price, so the it goes to zero every time. Ouch.

 

Any ideas on how to fix that? I need some way to carry the "cheapest" value through the submit button. I don't know much about Web programming. Maybe there's some sort of session variable I can save it in?

Edited by ztwistbooks

Share this post


Link to post
Share on other sites

I was thinking of something similar, but have not tried my hand at coding it yet.

 

My thought was to subtract the lowest priced quote from each element of the array. This would require the process to sort the array lowest-to-highest price first (which looks like is being done with USPS Methods 4.3.2), and (likely) have a requirement that the array of quotes contain more than a single item (thus eliminating the possibility of waiving express shipping costs when the post office changes things on their side again).

 

I didn't realize that quotes are called again upon submit, so still might be problematic. Perhaps the adjusted prices could be stored in a secondary array (or, an additional dimension of the original quotes array), and these could then be referenced on subsequent pages.

 

Just vaporware thoughts at this point.

 

Rob

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
Sign in to follow this  

×