slug = 'weight'; 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 Weight', $this->slug); } function addTabContent($data){ echo '
'; include 'template/weight-based-charges.php'; echo '
'; } function rowTemplate($data){ if(empty($data['pi_cart_weight_charges'] ) || !is_array($data['pi_cart_weight_charges'] )) return; $rows = array_values($data['pi_cart_weight_charges']); foreach($rows as $count => $row){ ?> Cart weight 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_weight = self::getCartWeight($post_id, $package); $charge = self::getCharge($post_id, $get_charges, $cart_weight); return $charge; } static function getCharge($post_id, $get_charges, $cart_weight){ $matched_charges = self::getMatchedCharges($get_charges, $cart_weight); if(empty($matched_charges)) return 0; $summing_method = get_post_meta($post_id, 'pi_efrs_cart_weight_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_weight){ $matched_charges = array(); foreach($get_charges as $charge){ if(isset($charge['min']) && $charge['min'] !== '' && !empty($charge['max']) && ($cart_weight >= $charge['min'] && $cart_weight <= $charge['max'])){ $matched_charges[] = self::evaluate_cost( $charge['charge'], $cart_weight); } if(isset($charge['min']) && $charge['min'] !== '' && empty($charge['max']) && ($cart_weight >= $charge['min'])){ $matched_charges[] = self::evaluate_cost( $charge['charge'], $cart_weight); } } return $matched_charges; } static function evaluate_cost( $charge, $weight = 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( '[weight]' ), array( $weight, ), $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; } static function getCartWeight($post_id, $package){ if(!function_exists('WC') || !is_object(WC()->cart)) return 0; $package_support = 'package'; if($package_support == 'cart'){ $weight = WC()->cart->get_cart_contents_weight(); }else{ $weight = PISOL\EFRS\Package::get_weight($package); } return $weight; } static function groupEnabled($post_id){ $group_enabled = get_post_meta( $post_id, 'pi_enable_additional_charges_cart_weight', true ); return $group_enabled == 'on' ? true : false; } function getChargesRows($post_id){ $ranges = get_post_meta($post_id, 'pi_cart_weight_charges',true); if(empty($ranges) || !is_array($ranges)) return array(); return $ranges; } } new pisol_efrs_weight_based_extra_charges_setting();