Results 1 to 7 of 7
  1. #1
    Join Date
    Feb 2017
    Location
    Tokyo, Japan
    Posts
    263
    Plugin Contributions
    0

    Default How to force a shipping module to be selected by default as cheapest?

    Hi all,
    ZC 1.5.6c, using built-in responsive template, and built-in shipping modules plus specific Japanese shipping modules (yupack, yupackchiiled).
    Test site:https://www.proteawines.jp/testshop/

    I want to set "yupackchilled" (or in general any particular module based on conditions) to be the default selected module. This is a refrigerated shipping option and more expensive than the non-refrigerated option. Finally I want to set this the default during the period 1st May to 30th September each year.

    I understand that there is no way to set a default shipping option, Zen Cart chooses the cheapest shipping option for an order as the default one.

    Based on
    https://www.zen-cart.com/showthread....g-option/page2
    as a test, I tried to override the decision for cheapest rate in
    includes/classes/shipping.php
    in function cheapest(), as follows:
    Code:
          for ($i=0; $i<$size; $i++) {
            if (is_array($cheapest)) {
                // never quote storepickup as lowest - needs to be configured in shipping module
                //if ($rates[$i]['cost'] < $cheapest['cost'] and $rates[$i]['module'] != 'storepickup') {
                //    $cheapest = $rates[$i];
                //}
                // override with yupackchilled
                if ( $rates[$i]['module'] == 'yupackchilled') {
                    $cheapest = $rates[$i];
                }
            } else {
                if ($rates[$i]['module'] != 'storepickup') {
                    //$cheapest = $rates[$i];
                    // override with yupackchilled
                    if (  $rates[$i]['module'] == 'yupackchilled') {
                        $cheapest = $rates[$i];
                    }
                }
            }
          }
    However, the radio button in the shipping selection does not change in the
    main_page=checkout_shipping
    page, it still stays selected at the actual cheaper "yupack" module.

    Looking at the /includes/templates/<MYOVERRIDES>/templates/tpl_checkout_shipping_default.php
    it looks like nothing in particular needs to be set there (the module inserts information).

    So I am wondering how and where to change what Zen Cart perceives as being the cheapest (and therefore default selected) shipping option.
    Any advice most appreciated (and I do realize my code might well be entirely wrong for testing this).
    Zen Cart 1.5.6c modified to support Japanese language (postage module support work in progress). Upgraded incrementally each version from initial 1.5.5d.

  2. #2
    Join Date
    Feb 2017
    Location
    Tokyo, Japan
    Posts
    263
    Plugin Contributions
    0

    Default Re: How to force a shipping module to be selected by default as cheapest?

    Update: it seemed to actually have worked. Despite F5 to reload browser several times, nothing had changed. But today, after logging in again, the default seems to have changed to the refrigerated "yupackchilled" option.
    I wonder if that behaviour has something to to with the session or not.
    Regardless, I will try to fix up the code (only need the override in one place I expect), and test by logging out and logging in again as customer.
    Zen Cart 1.5.6c modified to support Japanese language (postage module support work in progress). Upgraded incrementally each version from initial 1.5.5d.

  3. #3
    Join Date
    Feb 2017
    Location
    Tokyo, Japan
    Posts
    263
    Plugin Contributions
    0

    Default Re: How to force a shipping module to be selected by default as cheapest?

    Here is my hack (since I do not trust my programming skills) for function cheapest() in includes/classes/shipping.php
    to use the preferred postage method even when it is not the cheapest. In this case, refrigerated service "yupackchilled".
    It only becomes the preferred method between 1st May and 30th September each year.
    Code:
          /../
          $cheapest = false;
          $preferred_found = false; // to indicate if already found preferred method
          $size = sizeof($rates);
          // override normal YuPack with refrigerated Yupack during the below period
          // 1st May - 30th September
          $curr_date = date("m-d");
          $date_start = "04-30"; // use previous day
          $date_end = "10-01"; // use following day
          for ($i=0; $i<$size; $i++) {
              if (is_array($cheapest)) { // if it is an array or not
                  // override with yupackchilled
                  // if this module is active we will use its quote
                  if ( $rates[$i]['module'] == 'yupackchilled' and $curr_date > $date_start and $curr_date < $date_end ) {
                      $cheapest = $rates[$i];
                      $preferred_found = true;
                  }
                  // never quote storepickup as lowest - needs to be configured in shipping module
                  //if ($rates[$i]['cost'] < $cheapest['cost'] and $rates[$i]['module'] != 'storepickup') {
                  if ($rates[$i]['cost'] < $cheapest['cost'] and $rates[$i]['module'] != 'storepickup' and $preferred_found == false) {
                      // update if the current rate is cheaper than the current cheapest rate
                      // but make sure not to override preferred method
                      $cheapest = $rates[$i];
                  }
              } else { // if it is not an array
                  if ($rates[$i]['module'] != 'storepickup') {
                      // override with yupackchilled
                      // if this module is active we will use its quote
                      if (  $rates[$i]['module'] == 'yupackchilled' and $curr_date > $date_start and $curr_date < $date_end ) {
                          $cheapest = $rates[$i];
                          $preferred_found = true;
                      }
                      if ( $preferred_found == false ) {
                          // update but
                          // make sure not to override preferred method
                          $cheapest = $rates[$i];
                      }
                  }
              }
          }
          $this->notify('NOTIFY_SHIPPING_MODULE_CALCULATE_CHEAPEST', $cheapest, $cheapest, $rates);
          return $cheapest;
          /../
    Note to self: Probably adding such functionality would be nice to have, in future Zen Cart versions. As I get more comfortable I might consider adding this myself. Still need to learn how to use observers though.
    Zen Cart 1.5.6c modified to support Japanese language (postage module support work in progress). Upgraded incrementally each version from initial 1.5.5d.

  4. #4
    Join Date
    Feb 2006
    Location
    Tampa Bay, Florida
    Posts
    9,683
    Plugin Contributions
    123

    Default Re: How to force a shipping module to be selected by default as cheapest?

    you might want to break out of the loop when preferred_found goes to true.
    That Software Guy. My Store: Zen Cart Modifications
    Available for hire - See my ad in Services
    Plugin Moderator, Documentation Curator, Chief Cook and Bottle-Washer.
    Do you benefit from Zen Cart? Then please support the project.

  5. #5
    Join Date
    Feb 2017
    Location
    Tokyo, Japan
    Posts
    263
    Plugin Contributions
    0

    Default Re: How to force a shipping module to be selected by default as cheapest?

    Thanks swguy, done.
    Added the following after each the found statements, so they look like this:
    Code:
    $preferred_found = true;
    break 1; // this breaks out of the whole loop
    Zen Cart 1.5.6c modified to support Japanese language (postage module support work in progress). Upgraded incrementally each version from initial 1.5.5d.

  6. #6
    Join Date
    Feb 2006
    Location
    Tampa Bay, Florida
    Posts
    9,683
    Plugin Contributions
    123

    Default Re: How to force a shipping module to be selected by default as cheapest?

    oh also remember that preferred shipping is stored in your session data; that might have been what tripped you up in your earlier testing.
    That Software Guy. My Store: Zen Cart Modifications
    Available for hire - See my ad in Services
    Plugin Moderator, Documentation Curator, Chief Cook and Bottle-Washer.
    Do you benefit from Zen Cart? Then please support the project.

  7. #7
    Join Date
    Feb 2017
    Location
    Tokyo, Japan
    Posts
    263
    Plugin Contributions
    0

    Default Re: How to force a shipping module to be selected by default as cheapest?

    Thanks swguy. Looks like I need to learn more about the session data. Fun stuff!
    Zen Cart 1.5.6c modified to support Japanese language (postage module support work in progress). Upgraded incrementally each version from initial 1.5.5d.

 

 

Similar Threads

  1. exclude shipping module from being auto selected as cheapest
    By marcopolo in forum General Questions
    Replies: 6
    Last Post: 27 Apr 2016, 07:58 PM
  2. Replies: 3
    Last Post: 8 Oct 2015, 03:34 PM
  3. How to force my custom $0 shipping module to not be default?
    By amyleew in forum General Questions
    Replies: 11
    Last Post: 10 Feb 2012, 12:55 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
disjunctive-egg
Zen-Cart, Internet Selling Services, Klamath Falls, OR