PageRenderTime 53ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/shipping/flat_rate.php

https://github.com/billortell/jigoshop
PHP | 197 lines | 150 code | 30 blank | 17 comment | 49 complexity | b8154606aec896b3b61f8ba4c0826f93 MD5 | raw file
  1. <?php
  2. /**
  3. * Flat rate shipping
  4. *
  5. * DISCLAIMER
  6. *
  7. * Do not edit or add directly to this file if you wish to upgrade Jigoshop to newer
  8. * versions in the future. If you wish to customise Jigoshop core for your needs,
  9. * please use our GitHub repository to publish essential changes for consideration.
  10. *
  11. * @package Jigoshop
  12. * @category Checkout
  13. * @author Jigowatt
  14. * @copyright Copyright (c) 2011 Jigowatt Ltd.
  15. * @license http://jigoshop.com/license/commercial-edition
  16. */
  17. class flat_rate extends jigoshop_shipping_method {
  18. public function __construct() {
  19. $this->id = 'flat_rate';
  20. $this->enabled = get_option('jigoshop_flat_rate_enabled');
  21. $this->title = get_option('jigoshop_flat_rate_title');
  22. $this->availability = get_option('jigoshop_flat_rate_availability');
  23. $this->countries = get_option('jigoshop_flat_rate_countries');
  24. $this->type = get_option('jigoshop_flat_rate_type');
  25. $this->tax_status = get_option('jigoshop_flat_rate_tax_status');
  26. $this->cost = get_option('jigoshop_flat_rate_cost');
  27. $this->fee = get_option('jigoshop_flat_rate_handling_fee');
  28. add_action('jigoshop_update_options', array(&$this, 'process_admin_options'));
  29. add_option('jigoshop_flat_rate_availability', 'all');
  30. add_option('jigoshop_flat_rate_title', 'Flat Rate');
  31. add_option('jigoshop_flat_rate_tax_status', 'taxable');
  32. }
  33. public function calculate_shipping() {
  34. $_tax = &new jigoshop_tax();
  35. $this->shipping_total = 0;
  36. $this->shipping_tax = 0;
  37. if ($this->type=='order') :
  38. // Shipping for whole order
  39. $this->shipping_total = $this->cost + $this->get_fee( $this->fee, jigoshop_cart::$cart_contents_total );
  40. if ( get_option('jigoshop_calc_taxes')=='yes' && $this->tax_status=='taxable' ) :
  41. $rate = $_tax->get_shipping_tax_rate();
  42. if ($rate>0) :
  43. $tax_amount = $_tax->calc_shipping_tax( $this->shipping_total, $rate );
  44. $this->shipping_tax = $this->shipping_tax + $tax_amount;
  45. endif;
  46. endif;
  47. else :
  48. // Shipping per item
  49. if (sizeof(jigoshop_cart::$cart_contents)>0) : foreach (jigoshop_cart::$cart_contents as $item_id => $values) :
  50. $_product = $values['data'];
  51. if ($_product->exists() && $values['quantity']>0) :
  52. $item_shipping_price = ($this->cost + $this->get_fee( $this->fee, $_product->get_price() )) * $values['quantity'];
  53. $this->shipping_total = $this->shipping_total + $item_shipping_price;
  54. if ( $_product->is_shipping_taxable() && $this->tax_status=='taxable' ) :
  55. $rate = $_tax->get_shipping_tax_rate( $_product->data['tax_class'] );
  56. if ($rate>0) :
  57. $tax_amount = $_tax->calc_shipping_tax( $item_shipping_price, $rate );
  58. $this->shipping_tax = $this->shipping_tax + $tax_amount;
  59. endif;
  60. endif;
  61. endif;
  62. endforeach; endif;
  63. endif;
  64. }
  65. public function admin_options() {
  66. ?>
  67. <thead><tr><th scope="col" width="200px"><?php _e('Flat Rates', 'jigoshop'); ?></th><th scope="col" class="desc"><?php _e('Flat rates let you define a standard rate per item, or per order.', 'jigoshop'); ?>&nbsp;</th></tr></thead>
  68. <tr>
  69. <td class="titledesc"><?php _e('Enable Flat Rate', 'jigoshop') ?>:</td>
  70. <td class="forminp">
  71. <select name="jigoshop_flat_rate_enabled" id="jigoshop_flat_rate_enabled" style="min-width:100px;">
  72. <option value="yes" <?php if (get_option('jigoshop_flat_rate_enabled') == 'yes') echo 'selected="selected"'; ?>><?php _e('Yes', 'jigoshop'); ?></option>
  73. <option value="no" <?php if (get_option('jigoshop_flat_rate_enabled') == 'no') echo 'selected="selected"'; ?>><?php _e('No', 'jigoshop'); ?></option>
  74. </select>
  75. </td>
  76. </tr>
  77. <tr>
  78. <td class="titledesc"><a href="#" tip="<?php _e('This controls the title which the user sees during checkout.','jigoshop') ?>" class="tips" tabindex="99"></a><?php _e('Method Title', 'jigoshop') ?>:</td>
  79. <td class="forminp">
  80. <input type="text" name="jigoshop_flat_rate_title" id="jigoshop_flat_rate_title" style="min-width:50px;" value="<?php if ($value = get_option('jigoshop_flat_rate_title')) echo $value; else echo 'Flat Rate'; ?>" />
  81. </td>
  82. </tr>
  83. <tr>
  84. <td class="titledesc"><?php _e('Type', 'jigoshop') ?>:</td>
  85. <td class="forminp">
  86. <select name="jigoshop_flat_rate_type" id="jigoshop_flat_rate_type" style="min-width:100px;">
  87. <option value="order" <?php if (get_option('jigoshop_flat_rate_type') == 'order') echo 'selected="selected"'; ?>><?php _e('Per Order', 'jigoshop'); ?></option>
  88. <option value="item" <?php if (get_option('jigoshop_flat_rate_type') == 'item') echo 'selected="selected"'; ?>><?php _e('Per Item', 'jigoshop'); ?></option>
  89. </select>
  90. </td>
  91. </tr>
  92. <?php $_tax = new jigoshop_tax(); ?>
  93. <tr>
  94. <td class="titledesc"><?php _e('Tax Status', 'jigoshop') ?>:</td>
  95. <td class="forminp">
  96. <select name="jigoshop_flat_rate_tax_status">
  97. <option value="taxable" <?php if (get_option('jigoshop_flat_rate_tax_status')=='taxable') echo 'selected="selected"'; ?>><?php _e('Taxable', 'jigoshop'); ?></option>
  98. <option value="none" <?php if (get_option('jigoshop_flat_rate_tax_status')=='none') echo 'selected="selected"'; ?>><?php _e('None', 'jigoshop'); ?></option>
  99. </select>
  100. </td>
  101. </tr>
  102. <tr>
  103. <td class="titledesc"><a href="#" tip="<?php _e('Cost excluding tax. Enter an amount, e.g. 2.50.', 'jigoshop') ?>" class="tips" tabindex="99"></a><?php _e('Cost', 'jigoshop') ?>:</td>
  104. <td class="forminp">
  105. <input type="text" name="jigoshop_flat_rate_cost" id="jigoshop_flat_rate_cost" style="min-width:50px;" value="<?php if ($value = get_option('jigoshop_flat_rate_cost')) echo $value; ?>" />
  106. </td>
  107. </tr>
  108. <tr>
  109. <td class="titledesc"><a href="#" tip="<?php _e('Fee excluding tax. Enter an amount, e.g. 2.50, or a percentage, e.g. 5%. Leave blank to disable.', 'jigoshop') ?>" class="tips" tabindex="99"></a><?php _e('Handling Fee', 'jigoshop') ?>:</td>
  110. <td class="forminp">
  111. <input type="text" name="jigoshop_flat_rate_handling_fee" id="jigoshop_flat_rate_handling_fee" style="min-width:50px;" value="<?php if ($value = get_option('jigoshop_flat_rate_handling_fee')) echo $value; ?>" />
  112. </td>
  113. </tr>
  114. <tr>
  115. <td class="titledesc"><?php _e('Method available for', 'jigoshop') ?>:</td>
  116. <td class="forminp">
  117. <select name="jigoshop_flat_rate_availability" id="jigoshop_flat_rate_availability" style="min-width:100px;">
  118. <option value="all" <?php if (get_option('jigoshop_flat_rate_availability') == 'all') echo 'selected="selected"'; ?>><?php _e('All allowed countries', 'jigoshop'); ?></option>
  119. <option value="specific" <?php if (get_option('jigoshop_flat_rate_availability') == 'specific') echo 'selected="selected"'; ?>><?php _e('Specific Countries', 'jigoshop'); ?></option>
  120. </select>
  121. </td>
  122. </tr>
  123. <?php
  124. $countries = jigoshop_countries::$countries;
  125. asort($countries);
  126. $selections = get_option('jigoshop_flat_rate_countries', array());
  127. ?><tr class="multi_select_countries">
  128. <td class="titledesc"><?php _e('Specific Countries', 'jigoshop'); ?>:</td>
  129. <td class="forminp">
  130. <div class="multi_select_countries"><ul><?php
  131. if ($countries) foreach ($countries as $key=>$val) :
  132. echo '<li><label><input type="checkbox" name="jigoshop_flat_rate_countries[]" value="'. $key .'" ';
  133. if (in_array($key, $selections)) echo 'checked="checked"';
  134. echo ' />'. __($val, 'jigoshop') .'</label></li>';
  135. endforeach;
  136. ?></ul></div>
  137. </td>
  138. </tr>
  139. <script type="text/javascript">
  140. jQuery(function() {
  141. jQuery('select#jigoshop_flat_rate_availability').change(function(){
  142. if (jQuery(this).val()=="specific") {
  143. jQuery(this).parent().parent().next('tr.multi_select_countries').show();
  144. } else {
  145. jQuery(this).parent().parent().next('tr.multi_select_countries').hide();
  146. }
  147. }).change();
  148. });
  149. </script>
  150. <?php
  151. }
  152. public function process_admin_options() {
  153. if(isset($_POST['jigoshop_flat_rate_tax_status'])) update_option('jigoshop_flat_rate_tax_status', jigowatt_clean($_POST['jigoshop_flat_rate_tax_status'])); else @delete_option('jigoshop_flat_rate_tax_status');
  154. if(isset($_POST['jigoshop_flat_rate_enabled'])) update_option('jigoshop_flat_rate_enabled', jigowatt_clean($_POST['jigoshop_flat_rate_enabled'])); else @delete_option('jigoshop_flat_rate_enabled');
  155. if(isset($_POST['jigoshop_flat_rate_title'])) update_option('jigoshop_flat_rate_title', jigowatt_clean($_POST['jigoshop_flat_rate_title'])); else @delete_option('jigoshop_flat_rate_title');
  156. if(isset($_POST['jigoshop_flat_rate_type'])) update_option('jigoshop_flat_rate_type', jigowatt_clean($_POST['jigoshop_flat_rate_type'])); else @delete_option('jigoshop_flat_rate_type');
  157. if(isset($_POST['jigoshop_flat_rate_cost'])) update_option('jigoshop_flat_rate_cost', jigowatt_clean($_POST['jigoshop_flat_rate_cost'])); else @delete_option('jigoshop_flat_rate_cost');
  158. if(isset($_POST['jigoshop_flat_rate_handling_fee'])) update_option('jigoshop_flat_rate_handling_fee', jigowatt_clean($_POST['jigoshop_flat_rate_handling_fee'])); else @delete_option('jigoshop_flat_rate_handling_fee');
  159. if(isset($_POST['jigoshop_flat_rate_availability'])) update_option('jigoshop_flat_rate_availability', jigowatt_clean($_POST['jigoshop_flat_rate_availability'])); else @delete_option('jigoshop_flat_rate_availability');
  160. if (isset($_POST['jigoshop_flat_rate_countries'])) $selected_countries = $_POST['jigoshop_flat_rate_countries']; else $selected_countries = array();
  161. update_option('jigoshop_flat_rate_countries', $selected_countries);
  162. }
  163. }
  164. function add_flat_rate_method( $methods ) {
  165. $methods[] = 'flat_rate'; return $methods;
  166. }
  167. add_filter('jigoshop_shipping_methods', 'add_flat_rate_method' );