News:

Looking for documentation? Take a look on our wiki

Main Menu

Additions to Standard Shipping

Started by scotta, June 25, 2005, 10:09:49 AM

Previous topic - Next topic

scotta

I wanted a more flexible standard shipping module that would allow for the following rate calculation:

rate = fixed rate + weight determined rate + packing fee

This form of calulation is useful for us Australians and Australia Post has a basic zone charge and then a weight based charge on top.

Anyhow, I made the mod back for RC2, updated it for PL2 and now for PL3. So maybe is could be integrated as part of the standard module. Soren?

Below is the summary of the changed files and the additional database field - shipping_rate_weight_fee

Hope the is useful.

Scott
Electric Orange
www.electricorange.com.au

The following files have been modified:

/com_phpshop/language/english.php

/com_phpshop/html/shipping.rate_form.php

/com_phpshop/classes/ps_checkout.php
/com_phpshop/classes/ps_shipping.php

/com_phpshop/classes/shipping/standard_shipping.php

Also a database field has been added in the table mos_pshop_shipping_rate. The field shipping_rate_weight_fee has been added.


*/ english.php /*

    var $_PHPSHOP_RATE_FORM_VALUE = "Fee";
    var $_PHPSHOP_RATE_FORM_WEIGHT_FEE = "Fee/Weight Unit";
    var $_PHPSHOP_RATE_FORM_PACKAGE_FEE = "Your package fee";


*/ shipping.rate_form.php /*

An additional entry in the shipping rate form has been inserted to define the weight based component of the shipping rate.

.....................
<tr>
<td width="21%" ><div align="right"><strong><?php echo $PHPSHOP_LANG->_PHPSHOP_RATE_FORM_VALUE ?>:</strong></div></td>
<td width="79%" >
<input type="text" class="inputbox" name="shipping_rate_value" size="32" maxlength="255" value="<?php $db->sp("shipping_rate_value"?>">
</td>
</tr>

<tr>
<td width="21%" ><div align="right"><strong><?php echo $PHPSHOP_LANG->_PHPSHOP_RATE_FORM_WEIGHT_FEE ?>:</strong></div></td>
<td width="79%" >
<input type="text" class="inputbox" name="shipping_rate_weight_fee" size="32" maxlength="255" value="<?php $db->sp("shipping_rate_weight_fee"?>">
</td>
</tr>

<tr>
<td width="21%" ><div align="right"><strong><?php echo $PHPSHOP_LANG->_PHPSHOP_RATE_FORM_PACKAGE_FEE ?>:</strong></div></td>
<td width="79%" >
<input type="text" class="inputbox" name="shipping_rate_package_fee" size="32" maxlength="255" value="<?php $db->sp("shipping_rate_package_fee"?>">
</td>
</tr>.................



*/ ps_checkout.php /*

The lines below have been added to calculate the weight during the checkout phase as weight is not successfully passed through.

   function add( &$d ) {
      global $HTTP_POST_VARS, $afid, $PHPSHOP_LANG, $mosConfig_debug, $mosConfig_offset;
     
      $ps_vendor_id = $_SESSION["ps_vendor_id"];
      $auth = $_SESSION['auth'];
      $cart = $_SESSION['cart'];
      $d["error"] = "";

      require_once(CLASSPATH."ps_shipping_method.php");
      for ($i=0;$i<$cart["idx"];$i++) {
           $weight_subtotal = ps_shipping_method::get_weight($cart[$i]["product_id"]) * $cart[$i]['quantity'];
           $weight_total += $weight_subtotal;
          }
      $d["weight"] = $weight_total;

      require_once(CLASSPATH. 'ps_payment_method.php' );



*/ ps_shipping.php /*

The weight based component has been added to the standard functions.

// Validate Rates

  function validate_rate_add(&$d) {
    global $error_msg, $PHPSHOP_LANG;;
    $db = new ps_DB;....
....

    if ($d["shipping_rate_value"] == "") {
      $d["error"] = $PHPSHOP_LANG->_PHPSHOP_ERR_MSG_RATE_WEIGHT_VALUE_REQ;
      return False;
    }
[b]     if ($d["shipping_rate_weight_fee"] == "") {
    $d["error"] = $PHPSHOP_LANG->_PHPSHOP_ERR_MSG_RATE_WEIGHT_VALUE_REQ;
    return False;
    }[/b]
    if ($d["shipping_rate_package_fee"] == "") {
      $d["shipping_rate_package_fee"] = '0';
    }...............................




function rate_add(&$d) {
    $db = new ps_DB;
    $timestamp = time();

    if (!$this->validate_rate_add($d)) {
      return False;
    }

    $q = "INSERT INTO #__pshop_shipping_rate ";
    $q .= "(shipping_rate_name,shipping_rate_carrier_id,shipping_rate_country,";
    $q .= "shipping_rate_zip_start,shipping_rate_zip_end,shipping_rate_weight_start,";
    $q .= "shipping_rate_weight_end,shipping_rate_value,[b]shipping_rate_weight_fee[/b],shipping_rate_package_fee,";
    $q .= "shipping_rate_currency_id,shipping_rate_vat_id,shipping_rate_list_order) ";
    $q .= "VALUES ('";
    $q .= $d["shipping_rate_name"] . "','";
    $q .= $d["shipping_rate_carrier_id"] . "','";
    $src_str = "";
    if(!empty($d["shipping_rate_country"])) {
      for($i=0;$i<count($d["shipping_rate_country"]);$i++){
        if ($d["shipping_rate_country"][$i] != "") {
          $src_str .= $d["shipping_rate_country"][$i] . ";";
        }
      }
      chop($src_str,";");
    }
    $q .= "$src_str','";
    $q .= $d["shipping_rate_zip_start"] . "','";
    $q .= $d["shipping_rate_zip_end"] . "','";
    $q .= $d["shipping_rate_weight_start"] . "','";
    $q .= $d["shipping_rate_weight_end"] . "','";
    $q .= $d["shipping_rate_value"] . "','";
[b]    $q .= $d["shipping_rate_weight_fee"] . "','";[/b]
    $q .= $d["shipping_rate_package_fee"] . "','";
    $q .= $d["shipping_rate_currency_id"] . "','";
    $q .= $d["shipping_rate_vat_id"] . "','";
    $q .= $d["shipping_rate_list_order"] . "')";
    $db->query($q);
    $db->next_record();
    return True;
  }.........................................



  function rate_update(&$d) {
    $db = new ps_DB;

    $q = "UPDATE #__pshop_shipping_rate SET ";
    $q .= "shipping_rate_name='" . $d["shipping_rate_name"] . "',";
    $q .= "shipping_rate_carrier_id='" . $d["shipping_rate_carrier_id"] . "',";
    $src_str = "";
    if(!empty($d["shipping_rate_country"])) {
      for($i=0;$i<count($d["shipping_rate_country"]);$i++){
        if ($d["shipping_rate_country"][$i] != "") {
          $src_str .= $d["shipping_rate_country"][$i] . ";";
        }
      }
      chop($src_str);
    }
    $q .= "shipping_rate_country='$src_str',";
    $q .= "shipping_rate_zip_start='" . $d["shipping_rate_zip_start"] . "',";
    $q .= "shipping_rate_zip_end='" . $d["shipping_rate_zip_end"] . "',";
    $q .= "shipping_rate_weight_start='" . $d["shipping_rate_weight_start"] . "',";
    $q .= "shipping_rate_weight_end='" . $d["shipping_rate_weight_end"] . "',";
    $q .= "shipping_rate_value='" . $d["shipping_rate_value"] . "',";
[b]   $q .= "shipping_rate_weight_fee='" . $d["shipping_rate_weight_fee"] . "',";[/b]
    $q .= "shipping_rate_package_fee='" . $d["shipping_rate_package_fee"] . "',";
    $q .= "shipping_rate_currency_id='" . $d["shipping_rate_currency_id"] . "',";
    $q .= "shipping_rate_vat_id='" . $d["shipping_rate_vat_id"] . "',";
    $q .= "shipping_rate_list_order='" . $d["shipping_rate_list_order"] . "'";
    $q .= " WHERE shipping_rate_id='" . $d["shipping_rate_id"]."'";
    $db->query($q);
    $db->next_record();
    return True;
 


*/ standard_shipping.php /*

  function list_rates( &$d ) {
    global $PHPSHOP_LANG, $CURRENCY_DISPLAY;
    $auth = $_SESSION["auth"];
   
    $dbc = new ps_DB; // Carriers
    $dbr = new ps_DB; // Rates
       
        [b]$html = "Total weight = ";
        $html .= $d["weight"];
        echo($html);[/b]

.................
while ($dbc->next_record()) {
        $q = "SELECT shipping_rate_id,shipping_rate_name,shipping_rate_value,[b]shipping_rate_weight_fee[/b],shipping_rate_package_fee FROM #__pshop_shipping_rate WHERE ";
        $q .= "shipping_rate_carrier_id='" . $dbc->f("shipping_carrier_id") . "' AND ";
        $q .= "(shipping_rate_country LIKE '%" . $country . "%' OR ";.............................


function get_rate_details( &$d ) {

    $rvalue["pure_rate"] = 0;
    $rvalue["pack_rate"] = 0;
    $rvalue["total_rate"] = 0;
    $rvalue["vat_rate"] = 0;
    $rvalue["vat_value"] = 0;
    $rvalue["rate_curr"] = 0;
       
        $details = explode("|", urldecode($d['shipping_rate_id']) );
        $rate_id = $details[4];
       
    $dbr = new ps_DB; // Rates
    $q = "SELECT * FROM #__pshop_shipping_rate WHERE ";
    $q .= "shipping_rate_id='$rate_id'";
    $dbr->query($q);
    if ($dbr->next_record()) {
      $rvalue["name"] = $dbr->f("shipping_rate_name");
      $rvalue["pure_rate"] = $dbr->f("shipping_rate_value")[b] + $dbr->f("shipping_rate_weight_fee")*$d["weight"][/b];
      $rvalue["pack_rate"] = $dbr->f("shipping_rate_package_fee");
      $rvalue["total_rate"] = $dbr->f("shipping_rate_value")[b] + $dbr->f("shipping_rate_weight_fee")*$d["weight"][/b] + $dbr->f("shipping_rate_package_fee");
      $rvalue["vat_id"] = $dbr->f("shipping_rate_vat_id");



[attachment deleted by admin]

nojargon

hi Scotta,
I wish i could do something like this! how do i install the module? (i can't even do that ....). I was also wondering if there was a way to include international zones...
best,
noj

scotta

Noj,

It's not super hard. Just add the listed code to each of the files. I have include some of the surounding code so show where it goes.

The hardest part is addinf the additional field to the database. I did this manually through myphpadmin but a SQL querie would be the best, but I don't know how. Soren could do this in a blink of an eye.

I use the standard shipping for local and OS calculations now. I have over 200+ rates.

Scott

durian

I would like to express interest in seeing this, or a variation thereof, make it into the standard distribution.
The variation I'd like, is to calculate the weight based shipping fee in quanta, with a fixed minimum.
That probably doesn't make too much sense.  What I mean is:

There is a fixed weight rate component for all shipments under a given weight.  For example,
anything up to and including 1 pound will be $5.  Then additional shipping charges are calculated
for every pound (or half pound, or two pounds, etc) over the minimum weight.  Say, $1 for
every additional pound.

In my example, a 3 pound package would be $7 ($5 for the first pound and an additional $2 for the
next two pounds).


if (shipment_weight <= weight_minimum)
    excess_weight = 0;
else
    excess_weight = shipment_weight - weight_minimum;
num_excess_increments = (int)(excess_weight / weight_increment);
/* round up */
if (num_excess_increments * weight_increment < excess_weight)
    num_excess_increments++;
weight_shipping_charge = fixed_weight_charge + num_excess_increments * excess_cost_increment;


mike

loma

Any chance this will become part of the standard distribution?

Worked perfectly for me.

However, I needed to combine this hack with some hacks to dynamically change the price and weight based on some custom product attributes the user can give. (Use case: The customer can specify the size of the product, and this linearely influences the weight and the price).