• Resolved Alex Freixa

    (@alexfreixa)


    I can’t figure out how to get a global variable created and defined in the functions.php file. I’m using WooCommerce (3.7.1) in WordPress.

    I created a function called action_before_cart() and this function checks many a condition if the cart contains X products. In this function I declare a global variable called $shipping and the value after using this function should be: 0 or 1.

    Now, depending on the $shipping global variable value, in the hide_shipping_method_based_on_shipping_method function, only some selected shipping methods will be hidden during cart or checkout. I do it by unset each method by unset($rates[$method_key_id]); in a for each loop.

    So basically what Im trying to do is getting the global variable $shipping made in the function action_before_cart() and use it inside the hide_shipping_method_based_on_shipping_method function.

    add_action('woocommerce_before_cart', 'action_before_cart');
            function action_before_cart() {
    
                $hasOfficePack = false;
                $hasSundayCombo = false;
    
                $officePack = array('office pack', '77');
                $sundayCombo = array('sunday combo', '85');
    
                // Loop through cart items if there's $officePack or $sundayCombo
                foreach ( WC()->cart->get_cart() as $cart_item ) {
                    // Check for $officePack category products in cart
                    if ( has_term( $officePack, 'product_cat', $cart_item['product_id'] ) ) {
                        $hasOfficePack = true;
                    }
                    // Check for $sundayCombo category products in cart
                    if ( has_term( $sundayCombo, 'product_cat', $cart_item['product_id'] ) ) {
                        $hasSundayCombo  = true;
                    }
                }
    
                global $shipping; //the most important variable, which I want to be saved and used in the next function below
    
                if ( $hasOfficePack ) { 
                    $shipping = 0;
                }
    
                if ( $hasSundayCombo ) { 
                    $shipping = 1;
                }
    
            }
    
    add_filter( 'woocommerce_package_rates', 'hide_shipping_method_based_on_shipping_method', 1, 2 );
            function hide_shipping_method_based_on_shipping_method( $rates, $package ) {
    
                global $shipping; //the value of $shipping it's supposed to be set in the previous function
    
                if ( is_admin() && ! defined( 'DOING_AJAX' ) )
                    return;
    
                if ($shipping == 0) {
                    $method_key_ids = array('flat_rate:2', 'free_shipping:8', 'flat_rate:9');
                } elseif($shipping == 1) {
                    $method_key_ids = array('flat_rate:2', 'flat_rate:9');
                }
    
                foreach( $method_key_ids as $method_key_id ){
                    unset($rates[$method_key_id]); // Remove the targeted methods
                }
    
                return $rates;
    
            }
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How do I hide shipping methods on conditional function in woocomerce, wordpress?’ is closed to new replies.