Created
January 26, 2017 18:18
-
-
Save panoslyrakis/8f40cfe5e817c897ef031b6db197b216 to your computer and use it in GitHub Desktop.
Show weight interval in cart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: WPMU MP - Show weight interval | |
Plugin URI: http://premium.wpmudev.org/ | |
Description: Show weight interval in cart | |
Version: 1.0.0 | |
Author: Panos Lyrakis (WPMUDEV) | |
Author URI: http://premium.wpmudev.org | |
License: GNU General Public License (Version 2 - GPLv2) | |
*/ | |
if( ! class_exists( 'WPMUDEV_MP_Weight_Interval' ) ){ | |
class WPMUDEV_MP_Weight_Interval{ | |
private static $_instance = null; | |
public static function get_instance() { | |
if ( is_null( self::$_instance ) ) { | |
self::$_instance = new WPMUDEV_MP_Weight_Interval(); | |
} | |
return self::$_instance; | |
} | |
private function __construct() { | |
add_filter( 'mp_cart/cart_meta/shipping_total', array( $this, 'display_weight_interval_in_cart' ), 10 ,2 ); | |
} | |
/** | |
* Display shipping interval after shipping row in cart | |
* | |
* @since 1.0.0 | |
* @access public | |
* @return string | |
*/ | |
public function display_weight_interval_in_cart( $shipping_line, $MP_Cart ){ | |
$rates = $this->get_rates(); | |
$weight = $MP_Cart->shipping_weight(); | |
$settings = get_option( 'mp_settings' ); | |
$metric_system = $settings['shipping']['system'] == 'english' ? 'lb' : 'kg'; //mp_get_setting( 'shipping[system]' ); | |
$metric_system = apply_filters( 'wpmudev/mp/weight-interval/metric_system',$metric_system, $settings['shipping']['system'] ); | |
$rate = array(); | |
foreach ( $rates as $rate ) { | |
if ( $weight <= mp_arr_get_value( 'minweight', $rate, 0 ) ) { | |
break; | |
} | |
} | |
$inerval = number_format( (float)$rate['minweight' ] - (float)$weight, 2 ); | |
if( $interval >= 0 ){ | |
$interval_line .= '<div class="mp_cart_resume_item mp_cart_resume_item-wieght-interval"> | |
<span class="mp_cart_resume_item_label">' . __( 'Weight interval', 'mp' ) . '</span> | |
<span class="mp_cart_resume_item_amount">' . $inerval . ' ' . $metric_system . '</span> | |
</div>'; | |
$interval_line = apply_filters( 'wpmudev/mp/weight-interval/interval',$interval_line, $interval, $metric_system, $rates, $weight ); | |
$shipping_line .= $interval_line; | |
} | |
return $shipping_line; | |
} | |
/** | |
* Get rates sorted by price from highest to lowest | |
* | |
* @since 1.0.0 | |
* @access public | |
* @return array | |
*/ | |
public function get_rates() { | |
$rates = (array) $this->get_setting( 'rates', array() ); | |
usort( $rates, array( &$this, 'sort_rates' ) ); | |
return $rates; | |
} | |
/** | |
* Gets a setting specific to the gateway | |
* | |
* @since 1.0.0 | |
* @access public | |
* @param string $setting | |
* @param mixed $default | |
* @return mixed | |
*/ | |
public function get_setting( $setting, $default = false ) { | |
return mp_get_setting( "shipping->weight_rate->{$setting}", $default ); | |
} | |
} | |
add_action( 'plugins_loaded', function(){ | |
WPMUDEV_MP_Weight_Interval::get_instance(); | |
} ); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment