Skip to content

Instantly share code, notes, and snippets.

@pixeltrix
Created February 13, 2012 12:40
Show Gist options
  • Save pixeltrix/1816615 to your computer and use it in GitHub Desktop.
Save pixeltrix/1816615 to your computer and use it in GitHub Desktop.
Diff to add a per item calculation for the WooCommerce Table Rates Shipping plugin
diff --git a/public/wp-content/plugins/woocommerce-shipping-table-rate/shipping-table-rate.php b/public/wp-content/plugins/woocommerce-shipping-table-rate/shipping-table-rate.php
index 8a7e082..01f5e47 100644
--- a/public/wp-content/plugins/woocommerce-shipping-table-rate/shipping-table-rate.php
+++ b/public/wp-content/plugins/woocommerce-shipping-table-rate/shipping-table-rate.php
@@ -242,6 +242,7 @@ if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', g
<th class="country"><?php _e('Destination countries/states', 'wc_table_rate'); ?></th>
<th><?php _e('Postcode', 'wc_table_rate'); ?>&nbsp;<a class="tips" tip="<?php _e('(optional) Comma separated list of ZIPs/Postcodes. Accepts wildcards, e.g. P* will match a postcode of PE30.', 'wc_table_rate'); ?>">[?]</a></th>
<th><?php _e('Exclude Postcode', 'wc_table_rate'); ?>&nbsp;<a class="tips" tip="<?php _e('(optional) Comma separated list of ZIPs/Postcodes to EXCLUDE. Accepts wildcards, e.g. P* will match a postcode of PE30.', 'wc_table_rate'); ?>">[?]</a></th>
+ <th><?php _e('Per Item', 'wc_table_rate'); ?>&nbsp;<a class="tips" tip="<?php _e('Calculate shipping per item', 'wc_table_rate'); ?>">[?]</a></th>
<th><?php _e('Condition', 'wc_table_rate'); ?>&nbsp;<a class="tips" tip="<?php _e('Condition vs. destination', 'wc_table_rate'); ?>">[?]</a></th>
<th><?php _e('Range', 'wc_table_rate'); ?>&nbsp;<a class="tips" tip="<?php _e('Bottom/top range for the selected condition.', 'wc_table_rate'); ?>">[?]</a></th>
<th><?php _e('Cost', 'wc_table_rate'); ?>&nbsp;<a class="tips" tip="<?php _e('Cost, excluding tax.', 'wc_table_rate'); ?>">[?]</a></th>
@@ -252,7 +253,7 @@ if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', g
<tfoot>
<tr>
<th colspan="2"><a href="#" class="add button"><?php _e('+ Add Shipping Rate', 'wc_table_rate'); ?></a></th>
- <th colspan="7"><small><?php _e('Note, if the user has multiple matching rates they will get the choice of which to use.', 'wc_table_rate'); ?></small> <a href="#" class="dupe button"><?php _e('Duplicate selected rows', 'wc_table_rate'); ?></a> <a href="#" class="remove button"><?php _e('Delete selected rows', 'wc_table_rate'); ?></a></th>
+ <th colspan="8"><small><?php _e('Note, if the user has multiple matching rates they will get the choice of which to use.', 'wc_table_rate'); ?></small> <a href="#" class="dupe button"><?php _e('Duplicate selected rows', 'wc_table_rate'); ?></a> <a href="#" class="remove button"><?php _e('Delete selected rows', 'wc_table_rate'); ?></a></th>
</tr>
</tfoot>
<tbody class="table_rates">
@@ -274,6 +275,7 @@ if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', g
</td>
<td><input type="text" class="text" value="'.$rate['postcode'].'" name="shipping_postcode['.$i.']" placeholder="*" size="8" /></td>
<td><input type="text" class="text" value="'.$rate['exclude_postcode'].'" name="shipping_exclude_postcode['.$i.']" placeholder="" size="8" /></td>
+ <td><input type="checkbox" class="checkbox" '.checked( $rate['per_item'], 1, true ).' name="shipping_per_item['.$i.']" /></td>
<td><select class="select" name="shipping_condition['.$i.']" id="woocommerce_table_rate_condition" style="min-width:100px;">
<option value="price" '.selected( $rate['condition'], 'price', false ).'>'.__('Price', 'wc_table_rate').'</option>
<option value="weight" '.selected( $rate['condition'], 'weight', false ).'>'.__('Weight', 'wc_table_rate').'</option>
@@ -367,6 +369,7 @@ if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', g
</td>\
<td><input type="text" class="text" name="shipping_postcode[' + size + ']" placeholder="*" size="10" /></td>\
<td><input type="text" class="text" name="shipping_exclude_postcode[' + size + ']" placeholder="" size="10" /></td>\
+ <td><input type="checkbox" class="checkbox" name="shipping_per_item[' + size + ']" /></td>\
<td><select class="select" name="shipping_condition[' + size + ']" id="woocommerce_table_rate_condition" style="min-width:100px;">\
<option value="price"><?php _e('Price', 'wc_table_rate'); ?></option>\
<option value="weight"><?php _e('Weight', 'wc_table_rate'); ?></option>\
@@ -681,11 +684,19 @@ if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', g
// Default label if not set
if (!$rate['label']) $rate['label'] = __('Shipping', 'wc_table_rate');
- $this->add_rate(array(
- 'id' => $rate['id'],
- 'label' => $rate['label'],
- 'cost' => $this->get_fee( $this->fee, $woocommerce->cart->cart_contents_total ) + $rate['cost']
- ));
+ if ($rate['per_item']) {
+ $this->add_rate(array(
+ 'id' => $rate['id'],
+ 'label' => $rate['label'],
+ 'cost' => ($this->get_fee( $this->fee, $woocommerce->cart->cart_contents_total ) + $rate['cost']) * $woocommerce->cart->cart_contents_count
+ ));
+ } else {
+ $this->add_rate(array(
+ 'id' => $rate['id'],
+ 'label' => $rate['label'],
+ 'cost' => $this->get_fee( $this->fee, $woocommerce->cart->cart_contents_total ) + $rate['cost']
+ ));
+ }
endforeach;
@@ -770,6 +781,7 @@ if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', g
$shipping_countries = array();
$shipping_postcode = array();
$shipping_exclude_postcode = array();
+ $shipping_per_item = array();
$shipping_condition = array();
$shipping_min = array();
$shipping_max = array();
@@ -781,6 +793,7 @@ if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', g
if (isset($_POST['shipping_countries']))$shipping_countries = $_POST['shipping_countries'];
if (isset($_POST['shipping_postcode'])) $shipping_postcode = array_map('strtoupper', array_map('woocommerce_clean', $_POST['shipping_postcode']));
if (isset($_POST['shipping_exclude_postcode'])) $shipping_exclude_postcode = array_map('strtoupper', array_map('woocommerce_clean', $_POST['shipping_exclude_postcode']));
+ if (isset($_POST['shipping_per_item']))$shipping_per_item = array_map('woocommerce_clean', $_POST['shipping_per_item']);
if (isset($_POST['shipping_condition']))$shipping_condition = array_map('woocommerce_clean', $_POST['shipping_condition']);
if (isset($_POST['shipping_min'])) $shipping_min = array_map('woocommerce_clean', $_POST['shipping_min']);
if (isset($_POST['shipping_max'])) $shipping_max = array_map('woocommerce_clean', $_POST['shipping_max']);
@@ -819,6 +832,9 @@ if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', g
endforeach;
+ // Per item calculation
+ if (isset($shipping_per_item[$i])) $per_item = 1; else $per_item = 0;
+
// Format min and max
$condition = $shipping_condition[$i];
switch ($condition) :
@@ -850,6 +866,7 @@ if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', g
'condition' => $shipping_condition[$i],
'postcode' => $shipping_postcode[$i],
'exclude_postcode' => $shipping_exclude_postcode[$i],
+ 'per_item' => $per_item,
'min' => $shipping_min[$i],
'max' => $shipping_max[$i],
'cost' => $shipping_cost[$i],
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment