Zen Cart Logo
Forums / Addon Shipping Modules / Enable / Disable shipping module by DAY and TIME

Enable / Disable shipping module by DAY and TIME

Views: 1,374

Results 1 to 10 of 10
30 Jun 2015, 11:36 AM
#1
schoolboy avatar

schoolboy

Totally Zenned

Join Date:
Jun 2005
Location:
Cumbria, UK
Posts:
10,327
Plugin Contributions:
0

Enable / Disable shipping module by DAY and TIME

I know this may not be easy, but we want certain shipping options to be sensitive (enabled / disabled) depending on the DAY and TIME a visitor accesses the site.

For example, we want to allow Royal Mail Priority Mail as a postal option, but only if the DAY is SUNDAY 01H00 thru to THURSDAY 13H00 (British Standard Time).

If a visitor goes through checkout, then PRIORITY POSTAL OPTION would only be visible/selectable to them, IF the time (and day) is later than 1am Sunday, and earlier than 3pm Thursday.

Outside of these days/times, the module is disabled.

As this shipping option is only available to UK-address deliveries, we would base the programming on our server time, which is BST. This shipping option is already disabled using standard limitation features in ZC for non-UK shipping addresses.

Any ideas?


20 years a Zencart User

30 Jun 2015, 12:36 PM
#2
design75 avatar

design75

Totally Zenned

Join Date:
Dec 2009
Location:
Amersfoort, The Netherlands
Posts:
2,862
Plugin Contributions:
5

Re: Enable / Disable shipping module by DAY and TIME

the first thing that came to my mind was to maybe us a cron job to execute a script, and enable/disable the module directly in the database

30 Jun 2015, 12:48 PM
#3
mc12345678 avatar

mc12345678

Totally Zenned

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

Re: Enable / Disable shipping module by DAY and TIME

In each shipping module there is generally a statment towards the beginning of execution that identifies if the module is enabled or disabled. It is in that area that enablement and disablement would be determined by use of PHP date and time functions.

I don't have ready access to the module's code to provide specific guidance, but in my opinion it isn't a significant task to accomplish. The thing of concern is more likely an issue of program/customer flow if the final button press occurs just after the cutoff time and how that affects the shopping experience. Ie. If all seems well and good for the purchase, they perhaps navigate around a little then go to complete the purchase just after cutoff where the selected option is no longer available, will the purchase go through with/without shipping?

Ps. Perhaps post the "upper" portion of the shipping code and can offer specific guidance?

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

30 Jun 2015, 6:27 PM
#4
schoolboy avatar

schoolboy

Totally Zenned

Join Date:
Jun 2005
Location:
Cumbria, UK
Posts:
10,327
Plugin Contributions:
0

Re: Enable / Disable shipping module by DAY and TIME

mc12345678:

In each shipping module there is generally a statment towards the beginning of execution that identifies if the module is enabled or disabled. It is in that area that enablement and disablement would be determined by use of PHP date and time functions.

I don't have ready access to the module's code to provide specific guidance, but in my opinion it isn't a significant task to accomplish. The thing of concern is more likely an issue of program/customer flow if the final button press occurs just after the cutoff time and how that affects the shopping experience. Ie. If all seems well and good for the purchase, they perhaps navigate around a little then go to complete the purchase just after cutoff where the selected option is no longer available, will the purchase go through with/without shipping?

Ps. Perhaps post the "upper" portion of the shipping code and can offer specific guidance?

Thanks for the reply - and the various cautions.

To keep it relatively simple, and not specify a time of day (in addition to the day concerned), the following configuration is acceptable - being the day of the week only:

MODULE ACTIVE: Monday, Tuesday, Wednesday, Thursday.
MODULE INACTIVE: Friday, Saturday, Sunday

The module is out of "Big Royal Mail". The relevant code seems to be:

<?php
/*

GPL released as part of the big_royalmail_v2.3 package

see CREDITS.txt for the contributors and support forum.

*/


  class specialdelivery {
    var $code, $title, $description, $enabled, $num_zones ;


// class constructor
    function specialdelivery() {

      global $order, $total_weight, $messageStack ;

      $this->version = '2.3.8';
      $this->code = 'specialdelivery';
      $this->title = ( (defined('IS_ADMIN_FLAG') && IS_ADMIN_FLAG == true) ? @constant('MODULE_SHIPPING_'.(strtoupper($this->code)).'_TEXT_TITLE'). ' <b style="color:#ff4000">ver. '.$this->version.'</b>' : constant('MODULE_SHIPPING_'.(strtoupper($this->code)).'_TEXT_TITLE') );
      $this->description = @constant('MODULE_SHIPPING_'.(strtoupper($this->code)).'_TEXT_DESCRIPTION');
      $this->sort_order = @constant('MODULE_SHIPPING_'.(strtoupper($this->code)).'_SORT_ORDER');
      $this->icon = (( defined('DIR_WS_ICONS') ? DIR_WS_ICONS : 'images/icons/' ) . 'shipping_sdnd.gif');
      $this->tax_class = @constant('MODULE_SHIPPING_'.(strtoupper($this->code)).'_TAX_CLASS');
      $this->enabled = @constant('MODULE_SHIPPING_'.(strtoupper($this->code)).'_STATUS') == 'True' ? true : false;

      // CUSTOMIZE THIS SETTING FOR THE NUMBER OF ZONES NEEDED
      $this->num_zones = 1;

// inspired by Jim Barrington (JollyJim)

      if( isset($order) && @constant('MODULE_SHIPPING_'.(strtoupper($this->code)).'_HIDE_SHIPPING_ERRORS') == 'True' ){

        switch(isset($order->info['subtotal'])){

        case true:
          if( $order->info['subtotal'] < @constant('MODULE_SHIPPING_'.(strtoupper($this->code)).'_MIN_ORDERVALUE')){
              $this->enabled = false;
            return ;
          }else
          if(MODULE_SHIPPING_SPECIALDELIVERY_MAX_ORDERVALUE != -1 && $order->info['subtotal'] > @constant('MODULE_SHIPPING_'.(strtoupper($this->code)).'_MAX_ORDERVALUE')){
              $this->enabled = false;
            return ;
          }
        break;

        case false:
          if($order->info['total'] < @constant('MODULE_SHIPPING_'.(strtoupper($this->code)).'_MIN_ORDERVALUE')){
              $this->enabled = false;
              return ;
          }else
          if(MODULE_SHIPPING_SPECIALDELIVERY_MAX_ORDERVALUE != -1 && $order->info['total'] > @constant('MODULE_SHIPPING_'.(strtoupper($this->code)).'_MAX_ORDERVALUE')){
              $this->enabled = false;
              return ;
          }
        break;

        } // end of switch on subtotal


        // check that it is a valid country being shipped to.
        $dest_country = $order->delivery['country']['iso_code_2'];
        $dest_zone = 0;
  
        for ($i=1; $i<=$this->num_zones; $i++) {
          $countries_table = constant('MODULE_SHIPPING_SPECIALDELIVERY_ZONES_COUNTRIES_' . $i);
          $country_zones = preg_split("/,/", preg_replace('/\s*/','',$countries_table) );
          if (in_array($dest_country, $country_zones)) {
            $dest_zone = $i;
            break;
          }
        }

        // ship to country is invalid for this service
        if ($dest_zone == 0) {
              $this->enabled = false;
            return ;
        }else{

        $this->enabled = false;  // enabled set to true if valid shipping weight found

        $zones_cost = constant('MODULE_SHIPPING_SPECIALDELIVERY_ZONES_COST0_' . $dest_zone)
          . ',' . constant('MODULE_SHIPPING_SPECIALDELIVERY_ZONES_COST1_' . $dest_zone)
          . ',' . constant('MODULE_SHIPPING_SPECIALDELIVERY_ZONES_COST2_' . $dest_zone)
          . ',' . constant('MODULE_SHIPPING_SPECIALDELIVERY_ZONES_COST3_' . $dest_zone)
          . ',' . constant('MODULE_SHIPPING_SPECIALDELIVERY_ZONES_COST4_' . $dest_zone);


        $zones_table = preg_split("/[:,]/" , preg_replace('/\s*/', '', $zones_cost) );

        $size = sizeof($zones_table);
        for ($i=0; $i<$size; $i+=2) {
          if ($total_weight <= $zones_table[$i]) {
            $this->enabled = true;
            break;
          }
        } // end of looping through

        return ;

      } // end of valid country

     } // end of if hide invalid shipping methods is set

    }

20 years a Zencart User

30 Jun 2015, 6:28 PM
#5
schoolboy avatar

schoolboy

Totally Zenned

Join Date:
Jun 2005
Location:
Cumbria, UK
Posts:
10,327
Plugin Contributions:
0

Re: Enable / Disable shipping module by DAY and TIME

Design75:

the first thing that came to my mind was to maybe us a cron job to execute a script, and enable/disable the module directly in the database

Yes... this was my plan-B, but thanks for the suggestion.

20 years a Zencart User

30 Jun 2015, 9:01 PM
#7
mc12345678 avatar

mc12345678

Totally Zenned

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

Re: Enable / Disable shipping module by DAY and TIME

So looks like the code initially determines if the module is active or inactive based on the admin setting (Which in this case will be date related), then if other criteria are "met" the module is deactivated, such as too large of a weight, etc... See below with the colored text based on the criteria provided for day of week. Including time would be possible as well.. There still is the "issue" of how it is desired to handle say someone initiating the purchase at 2359 on Thursday night, but not getting to the final purchase until 0000+ the next day. If it is considered acceptable to have the purchase started but not completed until after that midnight cutoff, then a $_SESSION[] variable could be used as a standin that would expire with logout/timeout...

schoolboy:

Thanks for the reply - and the various cautions.

To keep it relatively simple, and not specify a time of day (in addition to the day concerned), the following configuration is acceptable - being the day of the week only:

MODULE ACTIVE: Monday, Tuesday, Wednesday, Thursday.
MODULE INACTIVE: Friday, Saturday, Sunday

The module is out of "Big Royal Mail". The relevant code seems to be:

<?php /* GPL released as part of the big_royalmail_v2.3 package see CREDITS.txt for the contributors and support forum. */ class specialdelivery { var $code, $title, $description, $enabled, $num_zones, $dayofweek ; // class constructor function specialdelivery() { global $order, $total_weight, $messageStack ; $this->version = '2.3.8'; $this->code = 'specialdelivery'; $this->title = ( (defined('IS_ADMIN_FLAG') && IS_ADMIN_FLAG == true) ? @constant('MODULE_SHIPPING_'.(strtoupper($this->code)).'_TEXT_TITLE'). ' <b style="color:#ff4000">ver. '.$this->version.'</b>' : constant('MODULE_SHIPPING_'.(strtoupper($this->code)).'_TEXT_TITLE') ); $this->description = @constant('MODULE_SHIPPING_'.(strtoupper($this->code)).'_TEXT_DESCRIPTION'); $this->sort_order = @constant('MODULE_SHIPPING_'.(strtoupper($this->code)).'_SORT_ORDER'); $this->icon = (( defined('DIR_WS_ICONS') ? DIR_WS_ICONS : 'images/icons/' ) . 'shipping_sdnd.gif'); $this->tax_class = @constant('MODULE_SHIPPING_'.(strtoupper($this->code)).'_TAX_CLASS'); $this->dayofweek = date('N'); //mc12345678 added to use a single day-of-week value. 1= Monday, 7 = Sunday. if desired to instead use a session situation, then in addition to this line uncomment below: // if (isset($_SESSION['big_royal_day_of_week']) && $_SESSION['big_royal_day_of_week'] != '') { // $this->dayofweek = (int)$_SESSION['big_royal_day_of_week']; // } else { // $_SESSION['big_royal_day_of_week'] = $this->dayofweek; // } $this->enabled = ($this->dayofweek >= 1 && $this->dayofweek <= 4) ? true : false; //mc12345678 15-06-30 following code is commented out to support date specific activation: @constant('MODULE_SHIPPING_'.(strtoupper($this->code)).'_STATUS') == 'True' ? true : false; // CUSTOMIZE THIS SETTING FOR THE NUMBER OF ZONES NEEDED $this->num_zones = 1; // inspired by Jim Barrington (JollyJim) if( isset($order) && @constant('MODULE_SHIPPING_'.(strtoupper($this->code)).'_HIDE_SHIPPING_ERRORS') == 'True' ){ switch(isset($order->info['subtotal'])){ case true: if( $order->info['subtotal'] < @constant('MODULE_SHIPPING_'.(strtoupper($this->code)).'_MIN_ORDERVALUE')){ $this->enabled = false; return ; }else if(MODULE_SHIPPING_SPECIALDELIVERY_MAX_ORDERVALUE != -1 && $order->info['subtotal'] > @constant('MODULE_SHIPPING_'.(strtoupper($this->code)).'_MAX_ORDERVALUE')){ $this->enabled = false; return ; } break; case false: if($order->info['total'] < @constant('MODULE_SHIPPING_'.(strtoupper($this->code)).'_MIN_ORDERVALUE')){ $this->enabled = false; return ; }else if(MODULE_SHIPPING_SPECIALDELIVERY_MAX_ORDERVALUE != -1 && $order->info['total'] > @constant('MODULE_SHIPPING_'.(strtoupper($this->code)).'_MAX_ORDERVALUE')){ $this->enabled = false; return ; } break; } // end of switch on subtotal // check that it is a valid country being shipped to. $dest_country = $order->delivery['country']['iso_code_2']; $dest_zone = 0; for ($i=1; $i<=$this->num_zones; $i++) { $countries_table = constant('MODULE_SHIPPING_SPECIALDELIVERY_ZONES_COUNTRIES_' . $i); $country_zones = preg_split("/,/", preg_replace('/\s*/','',$countries_table) ); if (in_array($dest_country, $country_zones)) { $dest_zone = $i; break; } } // ship to country is invalid for this service if ($dest_zone == 0) { $this->enabled = false; return ; }else{ $this->enabled = false; // enabled set to true if valid shipping weight found $zones_cost = constant('MODULE_SHIPPING_SPECIALDELIVERY_ZONES_COST0_' . $dest_zone) . ',' . constant('MODULE_SHIPPING_SPECIALDELIVERY_ZONES_COST1_' . $dest_zone) . ',' . constant('MODULE_SHIPPING_SPECIALDELIVERY_ZONES_COST2_' . $dest_zone) . ',' . constant('MODULE_SHIPPING_SPECIALDELIVERY_ZONES_COST3_' . $dest_zone) . ',' . constant('MODULE_SHIPPING_SPECIALDELIVERY_ZONES_COST4_' . $dest_zone); $zones_table = preg_split("/[:,]/" , preg_replace('/\s*/', '', $zones_cost) ); $size = sizeof($zones_table); for ($i=0; $i<$size; $i+=2) { if ($total_weight <= $zones_table[$i]) { $this->enabled = true; break; } } // end of looping through return ; } // end of valid country } // end of if hide invalid shipping methods is set } ```

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

30 Jun 2015, 9:14 PM
#8
mc12345678 avatar

mc12345678

Totally Zenned

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

Re: Enable / Disable shipping module by DAY and TIME

schoolboy:

Thanks for the reply - and the various cautions.

To keep it relatively simple, and not specify a time of day (in addition to the day concerned), the following configuration is acceptable - being the day of the week only:

MODULE ACTIVE: Monday, Tuesday, Wednesday, Thursday.
MODULE INACTIVE: Friday, Saturday, Sunday

The module is out of "Big Royal Mail". The relevant code seems to be:
mc12345678:

So looks like the code initially determines if the module is active or inactive based on the admin setting (Which in this case will be date related), then if other criteria are "met" the module is deactivated, such as too large of a weight, etc... See below with the colored text based on the criteria provided for day of week. Including time would be possible as well.. There still is the "issue" of how it is desired to handle say someone initiating the purchase at 2359 on Thursday night, but not getting to the final purchase until 0000+ the next day. If it is considered acceptable to have the purchase started but not completed until after that midnight cutoff, then a $_SESSION[] variable could be used as a standin that would expire with logout/timeout...

Would like to take my nickel back a little... Basically the previous code enabled the module regardless of the setting in the admin panel, which is less than ideal... Below is a more correct version. Sorry about that.

<?php
/*

GPL released as part of the big_royalmail_v2.3 package

see CREDITS.txt for the contributors and support forum.

*/


  class specialdelivery {
    var $code, $title, $description, $enabled, $num_zones ;


// class constructor
    function specialdelivery() {

      global $order, $total_weight, $messageStack ;

      $this->version = '2.3.8';
      $this->code = 'specialdelivery';
      $this->title = ( (defined('IS_ADMIN_FLAG') && IS_ADMIN_FLAG == true) ? @constant('MODULE_SHIPPING_'.(strtoupper($this->code)).'_TEXT_TITLE'). ' <b style="color:#ff4000">ver. '.$this->version.'</b>' : constant('MODULE_SHIPPING_'.(strtoupper($this->code)).'_TEXT_TITLE') );
      $this->description = @constant('MODULE_SHIPPING_'.(strtoupper($this->code)).'_TEXT_DESCRIPTION');
      $this->sort_order = @constant('MODULE_SHIPPING_'.(strtoupper($this->code)).'_SORT_ORDER');
      $this->icon = (( defined('DIR_WS_ICONS') ? DIR_WS_ICONS : 'images/icons/' ) . 'shipping_sdnd.gif');
      $this->tax_class = @constant('MODULE_SHIPPING_'.(strtoupper($this->code)).'_TAX_CLASS');
      $this->dayofweek = date('N'); //mc12345678 added to use a single day-of-week value. 1= Monday, 7 = Sunday. if desired to instead use a session situation, then in addition to this line uncomment below:
//      if (isset($_SESSION['big_royal_day_of_week']) && $_SESSION['big_royal_day_of_week'] != '') {
//        $this->dayofweek = (int)$_SESSION['big_royal_day_of_week'];
//      } else {
//        $_SESSION['big_royal_day_of_week'] = $this->dayofweek;
//      }
      $this->enabled = (($this->dayofweek >= 1 && $this->dayofweek <= 4) && @constant('MODULE_SHIPPING_'.(strtoupper($this->code)).'_STATUS') == 'True') ? true : false; //mc12345678 15-06-30 evaluates true for customers if within the day of the week desired and activated in admin, otherwise false.  This "setting" could also be incorporated into the module as criteria; however, would require a bit of rewriting and other logic.


      // CUSTOMIZE THIS SETTING FOR THE NUMBER OF ZONES NEEDED
      $this->num_zones = 1;

// inspired by Jim Barrington (JollyJim)

      if( isset($order) && @constant('MODULE_SHIPPING_'.(strtoupper($this->code)).'_HIDE_SHIPPING_ERRORS') == 'True' ){

        switch(isset($order->info['subtotal'])){

        case true:
          if( $order->info['subtotal'] < @constant('MODULE_SHIPPING_'.(strtoupper($this->code)).'_MIN_ORDERVALUE')){
              $this->enabled = false;
            return ;
          }else
          if(MODULE_SHIPPING_SPECIALDELIVERY_MAX_ORDERVALUE != -1 && $order->info['subtotal'] > @constant('MODULE_SHIPPING_'.(strtoupper($this->code)).'_MAX_ORDERVALUE')){
              $this->enabled = false;
            return ;
          }
        break;

        case false:
          if($order->info['total'] < @constant('MODULE_SHIPPING_'.(strtoupper($this->code)).'_MIN_ORDERVALUE')){
              $this->enabled = false;
              return ;
          }else
          if(MODULE_SHIPPING_SPECIALDELIVERY_MAX_ORDERVALUE != -1 && $order->info['total'] > @constant('MODULE_SHIPPING_'.(strtoupper($this->code)).'_MAX_ORDERVALUE')){
              $this->enabled = false;
              return ;
          }
        break;

        } // end of switch on subtotal


        // check that it is a valid country being shipped to.
        $dest_country = $order->delivery['country']['iso_code_2'];
        $dest_zone = 0;
  
        for ($i=1; $i<=$this->num_zones; $i++) {
          $countries_table = constant('MODULE_SHIPPING_SPECIALDELIVERY_ZONES_COUNTRIES_' . $i);
          $country_zones = preg_split("/,/", preg_replace('/\s*/','',$countries_table) );
          if (in_array($dest_country, $country_zones)) {
            $dest_zone = $i;
            break;
          }
        }

        // ship to country is invalid for this service
        if ($dest_zone == 0) {
              $this->enabled = false;
            return ;
        }else{

        $this->enabled = false;  // enabled set to true if valid shipping weight found

        $zones_cost = constant('MODULE_SHIPPING_SPECIALDELIVERY_ZONES_COST0_' . $dest_zone)
          . ',' . constant('MODULE_SHIPPING_SPECIALDELIVERY_ZONES_COST1_' . $dest_zone)
          . ',' . constant('MODULE_SHIPPING_SPECIALDELIVERY_ZONES_COST2_' . $dest_zone)
          . ',' . constant('MODULE_SHIPPING_SPECIALDELIVERY_ZONES_COST3_' . $dest_zone)
          . ',' . constant('MODULE_SHIPPING_SPECIALDELIVERY_ZONES_COST4_' . $dest_zone);


        $zones_table = preg_split("/[:,]/" , preg_replace('/\s*/', '', $zones_cost) );

        $size = sizeof($zones_table);
        for ($i=0; $i<$size; $i+=2) {
          if ($total_weight <= $zones_table[$i]) {
            $this->enabled = true;
            break;
          }
        } // end of looping through

        return ;

      } // end of valid country

     } // end of if hide invalid shipping methods is set

    }

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

30 Jun 2015, 9:45 PM
#9
mc12345678 avatar

mc12345678

Totally Zenned

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

Re: Enable / Disable shipping module by DAY and TIME

And not sure off hand which would be "easier" to understand on the next upgrade or not, but the following would pull the new code out a little more providing a more logical presentation of what is going on, though equally functional. There are other potential variations, though I suggest keeping the day of week check within a bounds rather than simply greater than/greater than equal to a number or less than/less than equal to a number. By limiting the potential comparisons to only the values that could exist the code has less of an opportunity to have other data impress upon it. Ie., if the value were >6 and somehow a date value of A were provided, then the function would evaluate as true because A is greater than 6 (See ascii tables for one type of example.)

Alright, enough now.. :) Sorry, just expressing some other potential concepts.

<?php
/*

GPL released as part of the big_royalmail_v2.3 package

see CREDITS.txt for the contributors and support forum.

*/


  class specialdelivery {
    var $code, $title, $description, $enabled, $num_zones ;


// class constructor
    function specialdelivery() {

      global $order, $total_weight, $messageStack ;

      $this->version = '2.3.8';
      $this->code = 'specialdelivery';
      $this->title = ( (defined('IS_ADMIN_FLAG') && IS_ADMIN_FLAG == true) ? @constant('MODULE_SHIPPING_'.(strtoupper($this->code)).'_TEXT_TITLE'). ' <b style="color:#ff4000">ver. '.$this->version.'</b>' : constant('MODULE_SHIPPING_'.(strtoupper($this->code)).'_TEXT_TITLE') );
      $this->description = @constant('MODULE_SHIPPING_'.(strtoupper($this->code)).'_TEXT_DESCRIPTION');
      $this->sort_order = @constant('MODULE_SHIPPING_'.(strtoupper($this->code)).'_SORT_ORDER');
      $this->icon = (( defined('DIR_WS_ICONS') ? DIR_WS_ICONS : 'images/icons/' ) . 'shipping_sdnd.gif');
      $this->tax_class = @constant('MODULE_SHIPPING_'.(strtoupper($this->code)).'_TAX_CLASS');
      $this->dayofweek = date('N'); //mc12345678 added to use a single day-of-week value. 1= Monday, 7 = Sunday. if desired to instead use a session situation, then in addition to this line uncomment below:
//      if (isset($_SESSION['big_royal_day_of_week']) && $_SESSION['big_royal_day_of_week'] != '') {
//        $this->dayofweek = (int)$_SESSION['big_royal_day_of_week'];
//      } else {
//        $_SESSION['big_royal_day_of_week'] = $this->dayofweek;
//      }
      $this->enabled = @constant('MODULE_SHIPPING_'.(strtoupper($this->code)).'_STATUS') == 'True' ? true : false; 

      $this->enabled = $this->enabled && (($this->dayofweek >= 1 && $this->dayofweek <= 4) ? true : false);
//mc12345678 15-06-30 evaluates true for customers if activated in admin and if within the day of the week desired, otherwise false.  This "setting" could also be incorporated into the module as criteria; however, would require a bit of rewriting and other logic.


      // CUSTOMIZE THIS SETTING FOR THE NUMBER OF ZONES NEEDED
      $this->num_zones = 1;

// inspired by Jim Barrington (JollyJim)

      if( isset($order) && @constant('MODULE_SHIPPING_'.(strtoupper($this->code)).'_HIDE_SHIPPING_ERRORS') == 'True' ){

        switch(isset($order->info['subtotal'])){

        case true:
          if( $order->info['subtotal'] < @constant('MODULE_SHIPPING_'.(strtoupper($this->code)).'_MIN_ORDERVALUE')){
              $this->enabled = false;
            return ;
          }else
          if(MODULE_SHIPPING_SPECIALDELIVERY_MAX_ORDERVALUE != -1 && $order->info['subtotal'] > @constant('MODULE_SHIPPING_'.(strtoupper($this->code)).'_MAX_ORDERVALUE')){
              $this->enabled = false;
            return ;
          }
        break;

        case false:
          if($order->info['total'] < @constant('MODULE_SHIPPING_'.(strtoupper($this->code)).'_MIN_ORDERVALUE')){
              $this->enabled = false;
              return ;
          }else
          if(MODULE_SHIPPING_SPECIALDELIVERY_MAX_ORDERVALUE != -1 && $order->info['total'] > @constant('MODULE_SHIPPING_'.(strtoupper($this->code)).'_MAX_ORDERVALUE')){
              $this->enabled = false;
              return ;
          }
        break;

        } // end of switch on subtotal


        // check that it is a valid country being shipped to.
        $dest_country = $order->delivery['country']['iso_code_2'];
        $dest_zone = 0;
  
        for ($i=1; $i<=$this->num_zones; $i++) {
          $countries_table = constant('MODULE_SHIPPING_SPECIALDELIVERY_ZONES_COUNTRIES_' . $i);
          $country_zones = preg_split("/,/", preg_replace('/\s*/','',$countries_table) );
          if (in_array($dest_country, $country_zones)) {
            $dest_zone = $i;
            break;
          }
        }

        // ship to country is invalid for this service
        if ($dest_zone == 0) {
              $this->enabled = false;
            return ;
        }else{

        $this->enabled = false;  // enabled set to true if valid shipping weight found

        $zones_cost = constant('MODULE_SHIPPING_SPECIALDELIVERY_ZONES_COST0_' . $dest_zone)
          . ',' . constant('MODULE_SHIPPING_SPECIALDELIVERY_ZONES_COST1_' . $dest_zone)
          . ',' . constant('MODULE_SHIPPING_SPECIALDELIVERY_ZONES_COST2_' . $dest_zone)
          . ',' . constant('MODULE_SHIPPING_SPECIALDELIVERY_ZONES_COST3_' . $dest_zone)
          . ',' . constant('MODULE_SHIPPING_SPECIALDELIVERY_ZONES_COST4_' . $dest_zone);


        $zones_table = preg_split("/[:,]/" , preg_replace('/\s*/', '', $zones_cost) );

        $size = sizeof($zones_table);
        for ($i=0; $i<$size; $i+=2) {
          if ($total_weight <= $zones_table[$i]) {
            $this->enabled = true;
            break;
          }
        } // end of looping through

        return ;

      } // end of valid country

     } // end of if hide invalid shipping methods is set

    }

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

1 Jul 2015, 5:44 AM
#10
schoolboy avatar

schoolboy

Totally Zenned

Join Date:
Jun 2005
Location:
Cumbria, UK
Posts:
10,327
Plugin Contributions:
0

Re: Enable / Disable shipping module by DAY and TIME

mc12345678:

And not sure off hand which would be "easier" to understand on the next upgrade or not, but the following would pull the new code out a little more providing a more logical presentation of what is going on, though equally functional. There are other potential variations, though I suggest keeping the day of week check within a bounds rather than simply greater than/greater than equal to a number or less than/less than equal to a number. By limiting the potential comparisons to only the values that could exist the code has less of an opportunity to have other data impress upon it. Ie., if the value were >6 and somehow a date value of A were provided, then the function would evaluate as true because A is greater than 6 (See ascii tables for one type of example.)

Alright, enough now.. :) Sorry, just expressing some other potential concepts.

<?php /* GPL released as part of the big_royalmail_v2.3 package see CREDITS.txt for the contributors and support forum. */ class specialdelivery { var $code, $title, $description, $enabled, $num_zones ; // class constructor function specialdelivery() { global $order, $total_weight, $messageStack ; $this->version = '2.3.8'; $this->code = 'specialdelivery'; $this->title = ( (defined('IS_ADMIN_FLAG') && IS_ADMIN_FLAG == true) ? @constant('MODULE_SHIPPING_'.(strtoupper($this->code)).'_TEXT_TITLE'). ' <b style="color:#ff4000">ver. '.$this->version.'</b>' : constant('MODULE_SHIPPING_'.(strtoupper($this->code)).'_TEXT_TITLE') ); $this->description = @constant('MODULE_SHIPPING_'.(strtoupper($this->code)).'_TEXT_DESCRIPTION'); $this->sort_order = @constant('MODULE_SHIPPING_'.(strtoupper($this->code)).'_SORT_ORDER'); $this->icon = (( defined('DIR_WS_ICONS') ? DIR_WS_ICONS : 'images/icons/' ) . 'shipping_sdnd.gif'); $this->tax_class = @constant('MODULE_SHIPPING_'.(strtoupper($this->code)).'_TAX_CLASS'); $this->dayofweek = date('N'); //mc12345678 added to use a single day-of-week value. 1= Monday, 7 = Sunday. if desired to instead use a session situation, then in addition to this line uncomment below: // if (isset($_SESSION['big_royal_day_of_week']) && $_SESSION['big_royal_day_of_week'] != '') { // $this->dayofweek = (int)$_SESSION['big_royal_day_of_week']; // } else { // $_SESSION['big_royal_day_of_week'] = $this->dayofweek; // } $this->enabled = @constant('MODULE_SHIPPING_'.(strtoupper($this->code)).'_STATUS') == 'True' ? true : false; $this->enabled = $this->enabled && (($this->dayofweek >= 1 && $this->dayofweek <= 4) ? true : false); //mc12345678 15-06-30 evaluates true for customers if activated in admin and if within the day of the week desired, otherwise false. This "setting" could also be incorporated into the module as criteria; however, would require a bit of rewriting and other logic. // CUSTOMIZE THIS SETTING FOR THE NUMBER OF ZONES NEEDED $this->num_zones = 1; // inspired by Jim Barrington (JollyJim) if( isset($order) && @constant('MODULE_SHIPPING_'.(strtoupper($this->code)).'_HIDE_SHIPPING_ERRORS') == 'True' ){ switch(isset($order->info['subtotal'])){ case true: if( $order->info['subtotal'] < @constant('MODULE_SHIPPING_'.(strtoupper($this->code)).'_MIN_ORDERVALUE')){ $this->enabled = false; return ; }else if(MODULE_SHIPPING_SPECIALDELIVERY_MAX_ORDERVALUE != -1 && $order->info['subtotal'] > @constant('MODULE_SHIPPING_'.(strtoupper($this->code)).'_MAX_ORDERVALUE')){ $this->enabled = false; return ; } break; case false: if($order->info['total'] < @constant('MODULE_SHIPPING_'.(strtoupper($this->code)).'_MIN_ORDERVALUE')){ $this->enabled = false; return ; }else if(MODULE_SHIPPING_SPECIALDELIVERY_MAX_ORDERVALUE != -1 && $order->info['total'] > @constant('MODULE_SHIPPING_'.(strtoupper($this->code)).'_MAX_ORDERVALUE')){ $this->enabled = false; return ; } break; } // end of switch on subtotal // check that it is a valid country being shipped to. $dest_country = $order->delivery['country']['iso_code_2']; $dest_zone = 0; for ($i=1; $i<=$this->num_zones; $i++) { $countries_table = constant('MODULE_SHIPPING_SPECIALDELIVERY_ZONES_COUNTRIES_' . $i); $country_zones = preg_split("/,/", preg_replace('/\s*/','',$countries_table) ); if (in_array($dest_country, $country_zones)) { $dest_zone = $i; break; } } // ship to country is invalid for this service if ($dest_zone == 0) { $this->enabled = false; return ; }else{ $this->enabled = false; // enabled set to true if valid shipping weight found $zones_cost = constant('MODULE_SHIPPING_SPECIALDELIVERY_ZONES_COST0_' . $dest_zone) . ',' . constant('MODULE_SHIPPING_SPECIALDELIVERY_ZONES_COST1_' . $dest_zone) . ',' . constant('MODULE_SHIPPING_SPECIALDELIVERY_ZONES_COST2_' . $dest_zone) . ',' . constant('MODULE_SHIPPING_SPECIALDELIVERY_ZONES_COST3_' . $dest_zone) . ',' . constant('MODULE_SHIPPING_SPECIALDELIVERY_ZONES_COST4_' . $dest_zone); $zones_table = preg_split("/[:,]/" , preg_replace('/\s*/', '', $zones_cost) ); $size = sizeof($zones_table); for ($i=0; $i<$size; $i+=2) { if ($total_weight <= $zones_table[$i]) { $this->enabled = true; break; } } // end of looping through return ; } // end of valid country } // end of if hide invalid shipping methods is set } ``` Thanks a ton for the help... You're like I used to be on this forum (before age and health issues slowed me down) - not letting go of a challenge! I appreciate the help MC...

20 years a Zencart User