Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

Question on implementation technique


charleyshipman

Recommended Posts

In my normal, brute force, approach I have defined and set a variable in checkout_shipping that I refer to as a global in usps.php.

 

It seems to work for what I need (allowing or disallowing Media Mail as a shipping method depending upon cart contents), but I am wondering how this will work for many persons using the system at the same time. Have I set myself up to use a shipping method for one customer based upon another customer's cart contents?

 

I am new to php and osCommerce and am trying to get my store in its final configuration as quickly as possible.

 

http://AffordableBooksAndStuff.com

 

Thanks.

 

CharleyShipman

Link to comment
Share on other sites

without an example of what you changed code wise a bit tough for anyone to help. you need to give full details of what you are doing, and what you are trying to do. as this is a forum where the help is 'free', without examples, most would not even bother to logon and create an account to see what you are doing

Link to comment
Share on other sites

In checkout_shipping.php I added line 61:

 

  $no_media = $cart->test_Media();  // SET TO TRUE TO PROHIBIT MEDIA MAIL

 

In usps (which is the USPS Methods contribution 2.6 modified to include Media Mail) I have modified the _getQuote function:

 

    function _getQuote() {

      global $order, $transittime;

      global $no_media;      // ADDED REFERENCE TO GLOBAL

//$this->fancy_varmail($no_media, '$no_media in usps->_getQuote');

 

      if(in_array('Display transit time', explode(', ', MODULE_SHIPPING_USPS_OPTIONS))) $transit = TRUE;

 

      if ($order->delivery['country']['id'] == SHIPPING_ORIGIN_COUNTRY) {

        $request  = '<RateRequest USERID="' . MODULE_SHIPPING_USPS_USERID . '" PASSWORD="' . MODULE_SHIPPING_USPS_PASSWORD . '">';

        $services_count = 0;

 

        if (isset($this->service)) {

          $this->types = array($this->service => $this->types[$this->service]);

        }

 

        $dest_zip = str_replace(' ', '', $order->delivery['postcode']);

        if ($order->delivery['country']['iso_code_2'] == 'US') $dest_zip = substr($dest_zip, 0, 5);

 

        reset($this->types);

        $allowed_types = explode(", ", MODULE_SHIPPING_USPS_TYPES);

//$this->fancy_varmail($allowed_types, 'allowed_types');

        while (list($key, $value) = each($this->types)) {

 

  if ( !in_array($key, $allowed_types) ) continue;

 

 

  if ($no_media && $key == 'Media') continue;  // TESTED GLOBAL HERE

 

          $request .= '<Package ID="' . $services_count . '">' .

                      '<Service>' . $key . '</Service>' .

                      '<ZipOrigination>' . SHIPPING_ORIGIN_ZIP . '</ZipOrigination>' .

                      '<ZipDestination>' . $dest_zip . '</ZipDestination>' .

                      '<Pounds>' . $this->pounds . '</Pounds>' .

                      '<Ounces>' . $this->ounces . '</Ounces>' .

                      '<Container>' . $this->container . '</Container>' .

                      '<Size>' . $this->size . '</Size>' .

                      '<Machinable>' . $this->machinable . '</Machinable>' .

                      '</Package>';

 

          if($transit){

            $transitreq  = 'USERID="' . MODULE_SHIPPING_USPS_USERID .

                        '" PASSWORD="' . MODULE_SHIPPING_USPS_PASSWORD . '">' .

                        '<OriginZip>' . STORE_ORIGIN_ZIP . '</OriginZip>' .

                        '<DestinationZip>' . $dest_zip . '</DestinationZip>';

 

            switch ($key) {

              case 'Express':  $transreq[$key] = 'API=ExpressMail&XML=' .

                              urlencode( '<ExpressMailRequest ' . $transitreq . '</ExpressMailRequest>');

                              break;

              case 'Priority': $transreq[$key] = 'API=PriorityMail&XML=' .

                              urlencode( '<PriorityMailRequest ' . $transitreq . '</PriorityMailRequest>');

                              break;

              case 'Media':

              case 'Parcel':  $transreq[$key] = 'API=StandardB&XML=' .

                              urlencode( '<StandardBRequest ' . $transitreq . '</StandardBRequest>');

                              break;

              default:        $transreq[$key] = '';

                              break;

            }

          }

 

          $services_count++;

        }

        $request .= '</RateRequest>';

 

        $request = 'API=Rate&XML=' . urlencode($request);

      } else {

        $request  = '<IntlRateRequest USERID="' . MODULE_SHIPPING_USPS_USERID . '" PASSWORD="' . MODULE_SHIPPING_USPS_PASSWORD . '">' .

                    '<Package ID="0">' .

                    '<Pounds>' . $this->pounds . '</Pounds>' .

                    '<Ounces>' . $this->ounces . '</Ounces>' .

                    '<MailType>Package</MailType>' .

                    '<Country>' . $this->countries[$order->delivery['country']['iso_code_2']] . '</Country>' .

                    '</Package>' .

                    '</IntlRateRequest>';

 

        $request = 'API=IntlRate&XML=' . urlencode($request);

      }

 

      switch (MODULE_SHIPPING_USPS_SERVER) {

        case 'production': $usps_server = 'production.shippingapis.com';

                          $api_dll = 'shippingapi.dll';

                          break;

        case 'test':

        default:          $usps_server = 'testing.shippingapis.com';

                          $api_dll = 'ShippingAPITest.dll';

                          break;

      }

 

      $body = '';

 

      $http = new httpClient();

      if ($http->Connect($usps_server, 80)) {

        $http->addHeader('Host', $usps_server);

        $http->addHeader('User-Agent', 'osCommerce');

        $http->addHeader('Connection', 'Close');

 

        if ($http->Get('/' . $api_dll . '?' . $request)) $body = $http->getBody();

//  mail('[email protected]','USPS rate quote response',$body,'From: <[email protected]>');

//  mail('[email protected]','Charley test',$body,'From: <[email protected]>');

        if ($transit && is_array($transreq) && ($order->delivery['country']['id'] == STORE_COUNTRY)) {

          while (list($key, $value) = each($transreq)) {

            if ($http->Get('/' . $api_dll . '?' . $value)) $transresp[$key] = $http->getBody();

          }

        }

 

        $http->Disconnect();

 

      } else {

        return false;

      }

 

      $response = array();

      while (true) {

        if ($start = strpos($body, '<Package ID=')) {

          $body = substr($body, $start);

          $end = strpos($body, '</Package>');

          $response[] = substr($body, 0, $end+10);

          $body = substr($body, $end+9);

        } else {

          break;

        }

      }

 

      $rates = array();

      if ($order->delivery['country']['id'] == SHIPPING_ORIGIN_COUNTRY) {

        if (sizeof($response) == '1') {

          if (ereg('<Error>', $response[0])) {

            $number = ereg('<Number>(.*)</Number>', $response[0], $regs);

            $number = $regs[1];

            $description = ereg('<Description>(.*)</Description>', $response[0], $regs);

            $description = $regs[1];

 

            return array('error' => $number . ' - ' . $description);

          }

        }

 

        $n = sizeof($response);

        for ($i=0; $i<$n; $i++) {

          if (strpos($response[$i], '<Postage>')) {

            $service = ereg('<Service>(.*)</Service>', $response[$i], $regs);

            $service = $regs[1];

            $postage = ereg('<Postage>(.*)</Postage>', $response[$i], $regs);

            $postage = $regs[1];

 

            $rates[] = array($service => $postage);

 

            if ($transit) {

              switch ($service) {

                case 'Express':    $time = ereg('<MonFriCommitment>(.*)</MonFriCommitment>', $transresp[$service], $tregs);

                                    $time = $tregs[1];

                                    if ($time == '' || $time == 'No Data') {

                                      $time = '1 - 2 ' . MODULE_SHIPPING_USPS_TEXT_DAYS;

                                    } else {

                                      $time = 'Tomorrow by ' . $time;

                                    }

                                    break;

                case 'Priority':    $time = ereg('<Days>(.*)</Days>', $transresp[$service], $tregs);

                                    $time = $tregs[1];

                                    if ($time == '' || $time == 'No Data') {

                                      $time = '2 - 3 ' . MODULE_SHIPPING_USPS_TEXT_DAYS;

                                    } elseif ($time == '1') {

                                      $time .= ' ' . MODULE_SHIPPING_USPS_TEXT_DAY;

                                    } else {

                                      $time .= ' ' . MODULE_SHIPPING_USPS_TEXT_DAYS;

                                    }

                                    break;

                case 'Media':

                case 'Parcel':      $time = ereg('<Days>(.*)</Days>', $transresp[$service], $tregs);

                                    $time = $tregs[1];

                                    if ($time == '' || $time == 'No Data') {

                                      $time = '4 - 7 ' . MODULE_SHIPPING_USPS_TEXT_DAYS;

                                    } elseif ($time == '1') {

                                      $time .= ' ' . MODULE_SHIPPING_USPS_TEXT_DAY;

                                    } else {

                                      $time .= ' ' . MODULE_SHIPPING_USPS_TEXT_DAYS;

                                    }

                                    break;

                case 'First Class': $time = '2 - 5 ' . MODULE_SHIPPING_USPS_TEXT_DAYS;

                                    break;

                default:            $time = '';

                                    break;

              }

              if ($time != '') $transittime[$service] = ' (' . $time . ')';

            }

          }

        }

      } else {

        if (ereg('<Error>', $response[0])) {

          $number = ereg('<Number>(.*)</Number>', $response[0], $regs);

          $number = $regs[1];

          $description = ereg('<Description>(.*)</Description>', $response[0], $regs);

          $description = $regs[1];

 

          return array('error' => $number . ' - ' . $description);

        } else {

          $body = $response[0];

          $services = array();

          while (true) {

            if ($start = strpos($body, '<Service ID=')) {

              $body = substr($body, $start);

              $end = strpos($body, '</Service>');

              $services[] = substr($body, 0, $end+10);

              $body = substr($body, $end+9);

            } else {

              break;

            }

          }

          $allowed_types = array();

          foreach( explode(", ", MODULE_SHIPPING_USPS_TYPES_INTL) as $value ) $allowed_types[$value] = $this->intl_types[$value];

 

          $size = sizeof($services);

          for ($i=0, $n=$size; $i<$n; $i++) {

            if (strpos($services[$i], '<Postage>')) {

              $service = ereg('<SvcDescription>(.*)</SvcDescription>', $services[$i], $regs);

              $service = $regs[1];

              $postage = ereg('<Postage>(.*)</Postage>', $services[$i], $regs);

              $postage = $regs[1];

              $time = ereg('<SvcCommitments>(.*)</SvcCommitments>', $services[$i], $tregs);

              $time = $tregs[1];

              $time = preg_replace('/Weeks$/', MODULE_SHIPPING_USPS_TEXT_WEEKS, $time);

              $time = preg_replace('/Days$/', MODULE_SHIPPING_USPS_TEXT_DAYS, $time);

              $time = preg_replace('/Day$/', MODULE_SHIPPING_USPS_TEXT_DAY, $time);

 

              if( !in_array($service, $allowed_types) ) continue;

              if (isset($this->service) && ($service != $this->service) ) {

                continue;

              }

 

              $rates[] = array($service => $postage);

      if ($time != '') $transittime[$service] = ' (' . $time . ')';

            }

          }

        }

      }

 

      return ((sizeof($rates) > 0) ? $rates : false);

    }

 

As I said, this seems to work for me. It correctly excludes Media Mail as a shipping method when $no_media is true, and it allows Media Mail otherwise.

 

What I want to know is am I setting up some sort of condition where the variable can be set for one customer and tested for another? I don't believe that this can happen since by the time the checkout_shipping and usps modules are executed an SID has been established (at least I think it has) and I assume that the global that will be tested in usps is the one that was set in checkout_shipping for the same SID. But I don't know enough to know whether my assumptions are correct or not.

 

And, if I am setting myself up for some sort of problem, how can I get around it?

 

Thanks.

 

CharleyShipman

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...