Jump to content


Corporate Sponsors


Latest News: (loading..)

- - - - -

Subtract Media Mail price from Priority Mail?


3 replies to this topic

#1 ztwistbooks

  • Community Member
  • 27 posts
  • Real Name:Dan Zerkle

Posted 07 January 2010, 08:07

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.

#2 ztwistbooks

  • Community Member
  • 27 posts
  • Real Name:Dan Zerkle

Posted 18 January 2010, 04:06

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, 18 January 2010, 04:07.


#3 ztwistbooks

  • Community Member
  • 27 posts
  • Real Name:Dan Zerkle

Posted 20 January 2010, 09:39

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, 20 January 2010, 09:43.


#4 aiyou

  • Community Member
  • 64 posts
  • Real Name:Rob
  • Gender:Male

Posted 24 January 2010, 05:55

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