Advertisement
DanielHolm

Untitled

Sep 8th, 2013
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.27 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: WooCommerce Price per product shipping method.
  4. Plugin URI: http://www.weknowit.nu
  5. Description: A shipping method for WooCommerce to set shipping price based on number of articles.
  6. Version: 1.0
  7. Author: Daniel Holm, <daniel.holm@weknowit.nu>
  8. Author URI: http://www.danielholm.se
  9. License: GPL2
  10. */
  11.  
  12. /* Copyright 2013 Daniel Holm (email : daniel.holm@weknowit.nu)
  13.  
  14. This program is free software; you can redistribute it and/or modify
  15. it under the terms of the GNU General Public License, version 2, as
  16. published by the Free Software Foundation.
  17.  
  18. This program is distributed in the hope that it will be useful,
  19. but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. GNU General Public License for more details.
  22.  
  23. You should have received a copy of the GNU General Public License
  24. along with this program; if not, write to the Free Software
  25. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  26. */
  27.  
  28.  
  29. // Make sure WooCommerce is active
  30. if ( in_array( 'woocommerce/woocommerce.php', get_option( 'active_plugins' ) ) ) {
  31. add_filter( 'woocommerce_shipping_methods', 'add_shippin_per_product_method' );
  32. }
  33.  
  34. add_action( 'woocommerce_init', 'load_my_shipping_class' );
  35.  
  36.  
  37. function load_my_shipping_class() {
  38. /**
  39. * Price per product shipping method.
  40. *
  41. * @class WC_Shipping_Per_Product
  42. * @version 1.0
  43. * @author Daniel Holm, We Know IT
  44. */
  45.  
  46. class WC_Shipping_Per_Product extends WC_Shipping_Method {
  47. // construct function.
  48. function __construct() {
  49. $this->id = 'per_product';
  50. $this->method_title = __( 'Price Per Product', 'woocommerce' );
  51. $this->init();
  52. }
  53.  
  54. // run on init
  55. function init() {
  56.  
  57. // load settings
  58. $this->init_form_fields();
  59. $this->init_settings();
  60.  
  61. // define variables
  62. $this->enabled = true;
  63. $this->title = $this->get_option( 'title' );
  64. $this->minfee = $this->get_option( 'minfee' );
  65. $this->fee = $this->get_option( 'fee' );
  66. $this->codes = $this->get_option( 'codes' );
  67. $this->availability = $this->get_option( 'availability' );
  68. $this->countries = $this->get_option( 'countries' );
  69.  
  70. add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
  71. } // end of init
  72.  
  73. function init_form_fields() {
  74. global $woocommerce;
  75. $this->form_fields = array(
  76. 'enabled' => array(
  77. 'title' => __( 'Enable', 'woocommerce' ),
  78. 'type' => 'checkbox',
  79. 'label' => __( 'Enable Per Product Method', 'woocommerce' ),
  80. 'default' => 'no'
  81. ),
  82. 'title' => array(
  83. 'title' => __( 'Title', 'woocommerce' ),
  84. 'type' => 'text',
  85. 'description' => __( 'This controls the title which the user sees during checkout.', 'woocommerce' ),
  86. 'default' => __( 'Per Product', 'woocommerce' ),
  87. 'desc_tip' => true,
  88. ),
  89. 'minfee' => array(
  90. 'title' => __( 'Delivery Fee for Single product', 'woocommerce' ),
  91. 'type' => 'number',
  92. 'custom_attributes' => array(
  93. 'step' => 'any',
  94. 'min' => '0'
  95. ),
  96. 'description' => __( 'What fee do you want to charge for just one product. Leave blank to disable, 0 to for free.', 'woocommerce' ),
  97. 'default' => '',
  98. 'desc_tip' => true,
  99. 'placeholder' => '0.00'
  100. ),
  101. 'fee' => array(
  102. 'title' => __( 'Delivery Fee', 'woocommerce' ),
  103. 'type' => 'number',
  104. 'custom_attributes' => array(
  105. 'step' => 'any',
  106. 'min' => '0'
  107. ),
  108. 'description' => __( 'What fee do you want to charge for local delivery, disregarded if you choose free. Leave blank to disable.', 'woocommerce' ),
  109. 'default' => '',
  110. 'desc_tip' => true,
  111. 'placeholder' => '0.00'
  112. ),
  113. 'codes' => array(
  114. 'title' => __( 'Zip/Post Codes', 'woocommerce' ),
  115. 'type' => 'textarea',
  116. 'description' => __( 'What zip/post codes would you like to offer delivery to? Separate codes with a comma. Accepts wildcards, e.g. P* will match a postcode of PE30.', 'woocommerce' ),
  117. 'default' => '',
  118. 'desc_tip' => true,
  119. 'placeholder' => '12345, 56789 etc'
  120. ),
  121. 'availability' => array(
  122. 'title' => __( 'Method availability', 'woocommerce' ),
  123. 'type' => 'select',
  124. 'default' => 'all',
  125. 'class' => 'availability',
  126. 'options' => array(
  127. 'all' => __( 'All allowed countries', 'woocommerce' ),
  128. 'specific' => __( 'Specific Countries', 'woocommerce' )
  129. )
  130. ),
  131. 'countries' => array(
  132. 'title' => __( 'Specific Countries', 'woocommerce' ),
  133. 'type' => 'multiselect',
  134. 'class' => 'chosen_select',
  135. 'css' => 'width: 450px;',
  136. 'default' => '',
  137. 'options' => $woocommerce->countries->countries
  138. )
  139. );
  140. } // end of forms init
  141.  
  142. // show admin options
  143. function admin_options() {
  144. global $woocommerce; ?>
  145.  
  146. <h3><?php echo $this->method_title; ?></h3>
  147. <p><?php _e( 'Per product is a method to set a price per product for delivery.', 'woocommerce' ); ?></p>
  148. <table class="form-table">
  149. <?php $this->generate_settings_html(); ?>
  150. </table>
  151.  
  152. <?php
  153. }
  154.  
  155. // calculate shipping cost
  156. function calculate_shipping( $package = array() ) {
  157. global $woocommerce;
  158.  
  159. $shipping_total = 0;
  160. $fee = ( trim( $this->fee ) == '' ) ? 0 : $this->fee;
  161.  
  162. $total_items = sprintf(_n('%d', '%d', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count);
  163.  
  164. //print "This many: ".$total_items;
  165.  
  166. if ( $total_items > 1 ) {
  167. $shipping_total += $this->fee * $total_items;
  168. }
  169. else {
  170. $shipping_total += $this->minfee;
  171. }
  172.  
  173. $rate = array(
  174. 'id' => $this->id,
  175. 'label' => $this->title,
  176. 'cost' => $shipping_total
  177. );
  178.  
  179. $this->add_rate($rate);
  180. } //end of shipping calc
  181.  
  182. function is_available( $package ) {
  183. global $woocommerce;
  184.  
  185. if ($this->enabled=="no") return false;
  186.  
  187. // If post codes are listed, let's use them.
  188. $codes = '';
  189. if ( $this->codes != '' ) {
  190. foreach( explode( ',', $this->codes ) as $code ) {
  191. $codes[] = $this->clean( $code );
  192. }
  193. }
  194.  
  195. if ( is_array( $codes ) ) {
  196.  
  197. $found_match = false;
  198.  
  199. if ( in_array( $this->clean( $package['destination']['postcode'] ), $codes ) )
  200. $found_match = true;
  201.  
  202. // Wildcard search
  203. if ( ! $found_match ) {
  204.  
  205. $customer_postcode = $this->clean( $package['destination']['postcode'] );
  206. $customer_postcode_length = strlen( $customer_postcode );
  207.  
  208. for ( $i = 0; $i <= $customer_postcode_length; $i++ ) {
  209.  
  210. if ( in_array( $customer_postcode, $codes ) )
  211. $found_match = true;
  212.  
  213. $customer_postcode = substr( $customer_postcode, 0, -2 ) . '*';
  214. }
  215. }
  216.  
  217. if ( ! $found_match )
  218. return false;
  219. }
  220.  
  221. // Yay! We passed!
  222. return apply_filters( 'woocommerce_shipping_' . $this->id . '_is_available', true );
  223. }
  224. } // end of class
  225.  
  226. // add the new shipment class
  227. function add_shippin_per_product_method( $methods ) {
  228. // Class definition
  229. include( 'class-wc-shipping-per-product.php' );
  230. $methods[] = 'WC_Shipping_Per_Product';
  231. return $methods;
  232. }
  233.  
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement