Advertisement
DanielHolm

Untitled

Sep 1st, 2013
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.22 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.      * Local Delivery Shipping Method
  35.      *
  36.      * A simple shipping method allowing local delivery as a shipping method
  37.      *
  38.      * @class       WC_Shipping_Per_Product
  39.      * @version     1.0
  40.      * @author      Daniel Holm, We Know IT
  41.      */
  42.  
  43.     class WC_Shipping_Per_Product extends WC_Shipping_Method {
  44.         // construct function.
  45.         function __construct() {
  46.             $this->id           = 'per_product';
  47.             $this->method_title = __( 'Price Per Product', 'woocommerce' );
  48.             $this->init();
  49.         }
  50.  
  51.         // run on init
  52.         function init() {
  53.  
  54.             // load settings
  55.             $this->init_form_fields();
  56.             $this->init_settings();
  57.  
  58.             // define variables
  59.             $this->enabled      = true;
  60.             $this->title        = $this->get_option( 'title' );
  61.             $this->minfee       = $this->get_option( 'minfee' );
  62.             $this->fee          = $this->get_option( 'fee' );
  63.             $this->codes        = $this->get_option( 'codes' );
  64.             $this->availability = $this->get_option( 'availability' );
  65.             $this->countries    = $this->get_option( 'countries' );
  66.  
  67.             add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
  68.         } // end of init
  69.  
  70.         function init_form_fields() {
  71.             global $woocommerce;
  72.             $this->form_fields = array(
  73.                 'enabled' => array(
  74.                     'title'         => __( 'Enable', 'woocommerce' ),
  75.                     'type'          => 'checkbox',
  76.                     'label'         => __( 'Enable Per Product Method', 'woocommerce' ),
  77.                     'default'       => 'no'
  78.                 ),
  79.                 'title' => array(
  80.                     'title'         => __( 'Title', 'woocommerce' ),
  81.                     'type'          => 'text',
  82.                     'description'   => __( 'This controls the title which the user sees during checkout.', 'woocommerce' ),
  83.                     'default'       => __( 'Per Product', 'woocommerce' ),
  84.                     'desc_tip'      => true,
  85.                 ),
  86.                 'minfee' => array(
  87.                     'title'         => __( 'Delivery Fee for Single product', 'woocommerce' ),
  88.                     'type'          => 'number',
  89.                     'custom_attributes' => array(
  90.                         'step'  => 'any',
  91.                         'min'   => '0'
  92.                     ),
  93.                     'description'   => __( 'What fee do you want to charge for just one product. Leave blank to disable, 0 to for free.', 'woocommerce' ),
  94.                     'default'       => '',
  95.                     'desc_tip'      => true,
  96.                     'placeholder'   => '0.00'
  97.                 ),
  98.                 'fee' => array(
  99.                     'title'         => __( 'Delivery Fee', 'woocommerce' ),
  100.                     'type'          => 'number',
  101.                     'custom_attributes' => array(
  102.                         'step'  => 'any',
  103.                         'min'   => '0'
  104.                     ),
  105.                     'description'   => __( 'What fee do you want to charge for local delivery, disregarded if you choose free. Leave blank to disable.', 'woocommerce' ),
  106.                     'default'       => '',
  107.                     'desc_tip'      => true,
  108.                     'placeholder'   => '0.00'
  109.                 ),
  110.                 'codes' => array(
  111.                     'title'         => __( 'Zip/Post Codes', 'woocommerce' ),
  112.                     'type'          => 'textarea',
  113.                     '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' ),
  114.                     'default'       => '',
  115.                     'desc_tip'      => true,
  116.                     'placeholder'   => '12345, 56789 etc'
  117.                 ),
  118.                 'availability' => array(
  119.                     'title'         => __( 'Method availability', 'woocommerce' ),
  120.                     'type'          => 'select',
  121.                     'default'       => 'all',
  122.                     'class'         => 'availability',
  123.                     'options'       => array(
  124.                         'all'       => __( 'All allowed countries', 'woocommerce' ),
  125.                         'specific'  => __( 'Specific Countries', 'woocommerce' )
  126.                     )
  127.                 ),
  128.                 'countries' => array(
  129.                     'title'         => __( 'Specific Countries', 'woocommerce' ),
  130.                     'type'          => 'multiselect',
  131.                     'class'         => 'chosen_select',
  132.                     'css'           => 'width: 450px;',
  133.                     'default'       => '',
  134.                     'options'       => $woocommerce->countries->countries
  135.                 )
  136.                 );
  137.         } // end of forms init
  138.  
  139.         // show admin options
  140.         function admin_options() {
  141.             global $woocommerce; ?>
  142.  
  143.             <h3><?php echo $this->method_title; ?></h3>
  144.             <p><?php _e( 'Per product is a method to set a price per product for delivery.', 'woocommerce' ); ?></p>
  145.             <table class="form-table">
  146.                 <?php $this->generate_settings_html(); ?>
  147.             </table>
  148.  
  149.             <?php
  150.         }
  151.  
  152.         // calculate shipping cost         
  153.         function calculate_shipping( $package = array() ) {
  154.             global $woocommerce;
  155.  
  156.             $shipping_total = 0;
  157.             $fee = ( trim( $this->fee ) == '' ) ? 0 : $this->fee;
  158.  
  159.             $total_items = sprintf(_n('%d', '%d', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count);
  160.  
  161.             //print "This many: ".$total_items;
  162.  
  163.             if ( $total_items > 1 ) {
  164.                 $shipping_total += $this->fee * $total_items;
  165.             }
  166.             else {
  167.                 $shipping_total += $this->minfee;
  168.             }
  169.  
  170.             $rate = array(
  171.                 'id'        => $this->id,
  172.                 'label'     => $this->title,
  173.                 'cost'      => $shipping_total
  174.             );
  175.  
  176.             $this->add_rate($rate);
  177.         } //end of shipping calc
  178.  
  179.         function is_available( $package ) {
  180.             global $woocommerce;
  181.  
  182.             if ($this->enabled=="no") return false;
  183.  
  184.             // If post codes are listed, let's use them.
  185.             $codes = '';
  186.             if ( $this->codes != '' ) {
  187.                 foreach( explode( ',', $this->codes ) as $code ) {
  188.                     $codes[] = $this->clean( $code );
  189.                 }
  190.             }
  191.  
  192.             if ( is_array( $codes ) ) {
  193.  
  194.                 $found_match = false;
  195.  
  196.                 if ( in_array( $this->clean( $package['destination']['postcode'] ), $codes ) )
  197.                     $found_match = true;
  198.  
  199.                 // Wildcard search
  200.                 if ( ! $found_match ) {
  201.  
  202.                     $customer_postcode = $this->clean( $package['destination']['postcode'] );
  203.                     $customer_postcode_length = strlen( $customer_postcode );
  204.  
  205.                     for ( $i = 0; $i <= $customer_postcode_length; $i++ ) {
  206.  
  207.                         if ( in_array( $customer_postcode, $codes ) )
  208.                             $found_match = true;
  209.  
  210.                         $customer_postcode = substr( $customer_postcode, 0, -2 ) . '*';
  211.                     }
  212.                 }
  213.  
  214.                 if ( ! $found_match )
  215.                     return false;
  216.             }
  217.  
  218.             // Yay! We passed!
  219.             return apply_filters( 'woocommerce_shipping_' . $this->id . '_is_available', true );
  220.         }
  221.  
  222.     } // end of WooCommerce Class
  223.  
  224.     function add_shippin_per_product_method( $methods ) {
  225.         // Class definition
  226.         include( 'class-wc-shipping-per-product.php' );
  227.         $methods[] = 'WC_Shipping_Per_Product';
  228.         return $methods;
  229.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement