Last active
November 13, 2025 10:18
-
-
Save rajeshsingh520/8db5aee68c811b04ebcb7ada68a1862d to your computer and use it in GitHub Desktop.
make shipping free when specific optional fee is selected
This file contains hidden or 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
| class PISOL_Zero_Shipping_On_Fee { | |
| static $instance = null; | |
| public static function get_instance() { | |
| if ( null === self::$instance ) { | |
| self::$instance = new self(); | |
| } | |
| return self::$instance; | |
| } | |
| protected function __construct() { | |
| add_action('wp_loaded', array($this, 'clear_wc_shipping_rates_cache')); | |
| add_filter( 'woocommerce_package_rates', array( $this, 'maybe_zero_shipping_rates' ), PHP_INT_MAX, 2 ); | |
| } | |
| function clear_wc_shipping_rates_cache(){ | |
| if(!isset(WC()->cart) || !function_exists('WC')) return; | |
| if ( !WC()->customer ) return; // this allows it to work with woocommerce point of sales plugin | |
| $packages = WC()->cart->get_shipping_packages(); | |
| foreach ($packages as $key => $value) { | |
| $shipping_session = "shipping_for_package_$key"; | |
| unset(WC()->session->$shipping_session); | |
| } | |
| } | |
| public function maybe_zero_shipping_rates( $rates, $package ) { | |
| if(!class_exists('Pi_cefw_Apply_fees')){ | |
| return $rates; | |
| } | |
| if ( ! Pi_cefw_Apply_fees::feesChecked( 1137 ) ) { | |
| return $rates; | |
| } | |
| foreach ( $rates as $rate_id => $rate ) { | |
| if ( ! empty( $this->allowed_shipping_method_ids ) && ! in_array( $rate_id, $this->allowed_shipping_method_ids, true ) ) { | |
| continue; | |
| } | |
| if ( is_object( $rate ) ) { | |
| if ( method_exists( $rate, 'set_cost' ) ) { | |
| $rate->set_cost( 0 ); | |
| } else { | |
| $rate->cost = 0; | |
| } | |
| if ( property_exists( $rate, 'taxes' ) ) { | |
| $rate->taxes = array(); | |
| } | |
| $label = $rate->get_label ? $rate->get_label() : ( isset( $rate->label ) ? $rate->label : '' ); | |
| if ( ! empty( $label ) ) { | |
| $new_label = $label . ' (shipping waived)'; | |
| if ( method_exists( $rate, 'set_label' ) ) { | |
| $rate->set_label( $new_label ); | |
| } else { | |
| $rate->label = $new_label; | |
| } | |
| } | |
| } else { | |
| if ( is_array( $rate ) ) { | |
| $rates[ $rate_id ]['cost'] = 0; | |
| $rates[ $rate_id ]['taxes'] = array(); | |
| if ( isset( $rates[ $rate_id ]['label'] ) ) { | |
| $rates[ $rate_id ]['label'] .= ' (shipping waived)'; | |
| } | |
| } | |
| } | |
| } | |
| return $rates; | |
| } | |
| } | |
| PISOL_Zero_Shipping_On_Fee::get_instance(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment