Advertisement
DanielHolm

Per Product ship method

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