Advertisement
Guest User

elta-shipping.php

a guest
Jul 5th, 2012
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.69 KB | None | 0 0
  1. <?php
  2. /*
  3. MarketPress ΕΛ.ΤΑ weight-based Shipping Plugin
  4. Author: Aristeides Stathopoulos
  5. Version: 1.1
  6. */
  7.  
  8. class MP_Shipping_ELTA extends MP_Shipping_API {
  9.  
  10.   //private shipping method name. Lowercase alpha (a-z) and dashes (-) only please!
  11.   var $plugin_name = 'elta';
  12.  
  13.   //public name of your method, for lists and such.
  14.   var $public_name = '';
  15.  
  16.   //set to true if you need to use the shipping_metabox() method to add per-product shipping options
  17.   var $use_metabox = false;
  18.    
  19.     //set to true if you want to add per-product weight shipping field
  20.     var $use_weight = true;
  21.  
  22.   /**
  23.    * Runs when your class is instantiated. Use to setup your plugin instead of __construct()
  24.    */
  25.     function on_creation() {
  26.     //declare here for translation
  27.     $this->public_name = __('Δέματα ΕΛ.ΤΑ.', 'mp');
  28.     }
  29.  
  30.   /**
  31.    * Echo anything you want to add to the top of the shipping screen
  32.    */
  33.     function before_shipping_form($content) {
  34.         return $content;
  35.   }
  36.  
  37.   /**
  38.    * Echo anything you want to add to the bottom of the shipping screen
  39.    */
  40.     function after_shipping_form($content) {
  41.         return $content;
  42.   }
  43.  
  44.   /**
  45.    * Echo a table row with any extra shipping fields you need to add to the shipping checkout form
  46.    */
  47.     function extra_shipping_field($content) {
  48.         return $content;
  49.   }
  50.  
  51.   /**
  52.    * Use this to process any additional field you may add. Use the $_POST global,
  53.    *  and be sure to save it to both the cookie and usermeta if logged in.
  54.    */
  55.     function process_shipping_form() {
  56.  
  57.   }
  58.  
  59.     /**
  60.    * Echo a settings meta box with whatever settings you need for you shipping module.
  61.    *  Form field names should be prefixed with mp[shipping][plugin_name], like "mp[shipping][plugin_name][mysetting]".
  62.    *  You can access saved settings via $settings array.
  63.    */
  64.     function shipping_settings_box($settings) {
  65.     global $mp;
  66.     ?>
  67.     <div class="alert alert-success">
  68.         <?php _e('by choosing to send your products with ΕΛ.ΤΑ, the shipping is calculated based on your products weight.', 'mp') ?>
  69.     </div>
  70.     <div class="alert alert-info">
  71.         <strong><?php __('Attention:') ?></strong>
  72.         <?php _e('If you have not set the weight of a product, the it will be calculated as FREE shipping.', 'mp') ?>
  73.     </div>
  74.     <div class="alert alert-error">
  75.         <strong><?php _e('What you should keep in mind:', 'mp') ?></strong>
  76.         <p><?php _e('Shipping costs are automatically calculated for domestic, EU and international destinations.
  77.         However, ΕΛ.ΤΑ. does not accept for EU and international shippings packages heavier than 20-25 Kg.
  78.         In case your products are heavy, we recomend choosing from the above list of supported countries only Greece.', 'mp') ?></p>
  79.     </div>
  80.     <?php
  81.  
  82.   }
  83.  
  84.   /**
  85.    * Filters posted data from your form. Do anything you need to the $settings['shipping']['plugin_name']
  86.    *  array. Don't forget to return!
  87.    */
  88.     function process_shipping_settings($settings) {
  89.  
  90.     return $settings;
  91.   }
  92.  
  93.   /**
  94.    * Echo any per-product shipping fields you need to add to the product edit screen shipping metabox
  95.    *
  96.    * @param array $shipping_meta, the contents of the post meta. Use to retrieve any previously saved product meta
  97.    * @param array $settings, access saved settings via $settings array.
  98.    */
  99.     function shipping_metabox($shipping_meta, $settings) {
  100.  
  101.   }
  102.  
  103.   /**
  104.    * Save any per-product shipping fields from the shipping metabox using update_post_meta
  105.    *
  106.    * @param array $shipping_meta, save anything from the $_POST global
  107.    * return array $shipping_meta
  108.    */
  109.     function save_shipping_metabox($shipping_meta) {
  110.  
  111.     return $shipping_meta;
  112.   }
  113.  
  114.   /**
  115. * Use this function to return your calculated price as an integer or float
  116. *
  117. * @param int $price, always 0. Modify this and return
  118. * @param float $total, cart total after any coupons and before tax
  119. * @param array $cart, the contents of the shopping cart for advanced calculations
  120. * @param string $address1
  121. * @param string $address2
  122. * @param string $city
  123. * @param string $state, state/province/region
  124. * @param string $zip, postal code
  125. * @param string $country, ISO 3166-1 alpha-2 country code
  126. * @param string $selected_option, if a calculated shipping module, passes the currently selected sub shipping option if set
  127. *
  128. * return float $price
  129. */
  130. function calculate_shipping($price, $total, $cart, $address1, $address2, $city, $state, $zip, $country, $selected_option) {
  131.     global $mp;
  132.     $settings = get_option('mp_settings');
  133.  
  134.     //calculate extra shipping
  135.     $weights = array();
  136.     foreach ($cart as $product_id => $variations) {
  137.         $shipping_meta = get_post_meta($product_id, 'mp_shipping', true);
  138.             foreach ($variations as $variation => $data) {
  139.               if (!$data['download'])
  140.             $weights[] = $shipping_meta['weight'] * $data['quantity'];
  141.             }
  142.     }
  143.     $totalweight = array_sum($weights);
  144.  
  145.     if ($totalweight <= 1){
  146.         if ( in_array($settings['base_country'], $mp->eu_countries) ){
  147.             if (in_array($country, $mp->eu_countries)) {
  148.                 if ($country == $settings['base_country'])
  149.                     $price = 3.5;
  150.                 else $price = 25.27;
  151.             }
  152.         else {$price = 29.26;}
  153.         }
  154.     }
  155.     else if ($totalweight >= 1 && $totalweight < 999 ){
  156.         if ( in_array($settings['base_country'], $mp->eu_countries) ) {
  157.             if (in_array($country, $mp->eu_countries)) {
  158.                 if ($country == $settings['base_country']) { //greece gia times < 24 kg
  159.                     $price = (intval($totalweight) * 0.67) + 2.9;
  160.                 } else {$price = 25.27+(($totalweight-1)*4.36);}
  161.             } else {$price = 29.26+(($totalweight-1)*5.64);}
  162.         }
  163.     }
  164.     return $price;
  165.   }
  166.  
  167. }
  168.  
  169. //register plugin - uncomment to register
  170. mp_register_shipping_plugin( 'MP_Shipping_ELTA', 'elta', __('Δέματα ΕΛ.ΤΑ.', 'mp') );
  171. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement