Advertisement
DanielHolm

Untitled

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