slug = 'subtotal'; add_action('pi_efrs_additional_charges_tab', array($this, 'addTab'),10,1); add_action('pi_efrs_additional_charges_tab_content', array($this, 'addTabContent'),10,1); add_filter('pi_efrs_shipping_method_form_data',array($this, 'formData'), 20, 1); add_filter('pi_efrs_shipping_method_clone_form_data',array($this, 'cloneData'), 10, 2); add_action('pisol_efrs_save_shipping_method', array($this, 'saveForm'),20,1); add_filter('pi_efrs_add_additional_charges', array($this, 'addExtraCharge'),10, 3); } function addTab($data){ pisol_efrs_additional_charges_form::tabName('Cart Subtotal', $this->slug); } function addTabContent($data){ echo '
'; include 'template/subtotal-based-charges.php'; echo '
'; } function rowTemplate($data){ if(empty($data['pi_cart_subtotal_charges'] ) || !is_array($data['pi_cart_subtotal_charges'] )) return; $rows = array_values($data['pi_cart_subtotal_charges']); foreach($rows as $count => $row){ ?> Cart subtotal extraCharges($method_id, $package); return $cost + $extra_charge; } function extraCharges($post_id, $package){ $additional_charges_enabled = pisol_efrs_additional_charges_form::additionalChargesEnabled($post_id); if(!$additional_charges_enabled) return 0; $group_enabled = self::groupEnabled($post_id); if(!$group_enabled) return 0; $get_charges = self::getChargesRows($post_id); if(empty($get_charges)) return 0; $cart_subtotal = self::getCartSubtotal($post_id, $package ); $charge = self::getCharge($post_id, $get_charges, $cart_subtotal); return $charge; } static function groupEnabled($post_id){ $group_enabled = get_post_meta( $post_id, 'pi_enable_additional_charges_cart_subtotal', true ); return $group_enabled == 'on' ? true : false; } function getChargesRows($post_id){ $ranges = get_post_meta($post_id, 'pi_cart_subtotal_charges',true); if(empty($ranges) || !is_array($ranges)) return array(); return $ranges; } static function getCartSubtotal($post_id, $package ){ if(!function_exists('WC') || !is_object(WC()->cart)) return 0; $package_support = 'package'; if($package_support == 'cart'){ $subtotal = WC()->cart->get_displayed_subtotal(); }else{ $subtotal = PISOL\EFRS\Package::get_subtotal($package); } return $subtotal; } static function getCharge($post_id, $get_charges, $cart_subtotal){ $matched_charges = self::getMatchedCharges($get_charges, $cart_subtotal); if(empty($matched_charges)) return 0; $summing_method = get_post_meta($post_id, 'pi_efrs_cart_subtotal_sum_of_charges', true); switch($summing_method){ case 'all': return array_sum($matched_charges); break; case 'smallest': return min($matched_charges); break; case 'largest': return max($matched_charges); break; } } static function getMatchedCharges($get_charges, $cart_subtotal){ $matched_charges = array(); foreach($get_charges as $charge){ if(!empty($charge['min']) && !empty($charge['max']) && ($cart_subtotal >= $charge['min'] && $cart_subtotal <= $charge['max'])){ $matched_charges[] = self::evaluate_cost( $charge['charge'], $cart_subtotal); } if(!empty($charge['min']) && empty($charge['max']) && ($cart_subtotal >= $charge['min'])){ $matched_charges[] = self::evaluate_cost( $charge['charge'], $cart_subtotal); } } return $matched_charges; } static function evaluate_cost( $charge, $subtotal = 0 ) { include_once WC()->plugin_path() . '/includes/libraries/class-wc-eval-math.php'; // Allow 3rd parties to process shipping cost arguments. $locale = localeconv(); $decimals = array( wc_get_price_decimal_separator(), $locale['decimal_point'], $locale['mon_decimal_point'], ',' ); $sum = do_shortcode( str_replace( array( '[subtotal]' ), array( $subtotal, ), $charge ) ); // Remove whitespace from string. $sum = preg_replace( '/\s+/', '', $sum ); // Remove locale from string. $sum = str_replace( $decimals, '.', $sum ); // Trim invalid start/end characters. $sum = rtrim( ltrim( $sum, "\t\n\r\0\x0B+*/" ), "\t\n\r\0\x0B+-*/" ); // Do the math. return $sum ? WC_Eval_Math::evaluate( $sum ) : 0; } } new pisol_efrs_cart_subtotal_extra_charges_setting();