Last active
March 24, 2026 15:33
-
-
Save plugin-republic/9127f0380fe3ea3833617256bb9c6804 to your computer and use it in GitHub Desktop.
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
| <?php | |
| // Update shipping class if any of the specified fields has any value. | |
| // Uncomment the marked line below if you want to check fields for a specific value. | |
| add_action( 'woocommerce_before_calculate_totals', function( $cart ) { | |
| if ( is_admin() && ! defined( 'DOING_AJAX' ) ) { | |
| return; | |
| } | |
| $target_field_ids = [ 1642, 1643, 1644 ]; // the field IDs | |
| $shipping_class = 'easy'; // the shipping class slug | |
| $term = get_term_by( 'slug', $shipping_class, 'product_shipping_class' ); | |
| if ( ! $term ) { | |
| return; | |
| } | |
| foreach ( $cart->get_cart() as $cart_item ) { | |
| if ( empty( $cart_item['product_extras']['groups'] ) ) { | |
| continue; | |
| } | |
| $matched = false; | |
| foreach ( $cart_item['product_extras']['groups'] as $group ) { | |
| foreach ( $target_field_ids as $field_id ) { | |
| // if ( isset( $group[ $field_id ]['value'] ) && $group[ $field_id ]['value'] === 'Specific Value' ) { | |
| if ( ! empty( $group[ $field_id ]['value'] ) ) { | |
| $matched = true; | |
| break 2; | |
| } | |
| } | |
| } | |
| if ( $matched ) { | |
| $cart_item['data']->set_shipping_class_id( $term->term_id ); | |
| $cart_item['data']->save(); | |
| } | |
| } | |
| }, 20 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment