title: Pass variable between woocommerce hooks
body: <p>I'm trying to save the URL of a gravity forms entry to the metadata of the associated woocommerce order. I passed the entry ID to the checkout page via a query string and am able to access the data from the woocommerce_checkout_fields hook but not woocommerce_checkout_update_order_meta. So my solution was to store the query value in a global variable to pass to other functions. But from every function besides woocommerce_checkout_fields the global variable returns 'empty' even though it is returning a value inside woocommerce_checkout_fields. How would I pass the value of $entry to other functions?</p>
  <pre class="lang-php prettyprint-override"><code>/* REGISTER VARIABLE FROM URL QUERY STRING */
  function entry($qvar) {
      $qvar[] = &quot;entry&quot;;
      return $qvar;
  }
  
  add_filter('query_vars', 'entry');
  
  $entry_ID = '';
  function display_entry($fields) {
      /* GET DATA FROM ENTRY ID PASSED VIA QUERY STRING*/
      $entry = get_query_var('entry', 'empty');
      $GLOBALS['entry_id'] = $entry;
      if ( is_page('checkout') &amp;&amp; $entry != &quot;empty&quot;) {
          $entries = GFAPI::get_entry($GLOBALS['entry']);
          $first_name = $entries[&quot;4.3&quot;];
          $last_name = $entries[&quot;4.6&quot;];
          $email = $entries[&quot;5&quot;];
          $phone = $entries[&quot;6&quot;];
          
          /* POPULATE CHECKOUT FIELDS */
          $cart = WC()-&gt;cart-&gt;get_cart();
          foreach( $cart as $cart_item ){
               $product_id = $cart_item['product_id'];
              if ($product_id == 646) {
                  $fields['billing']['billing_first_name']['default'] = $first_name;
                  $fields['billing']['billing_last_name']['default'] = $last_name;
                  $fields['billing']['billing_email']['default'] = $email;
                  $fields['billing']['billing_phone']['default'] = $phone;
              }
          }
      }
      return $fields;
  }
  
  add_filter('woocommerce_checkout_fields', display_entry);
  
  /* SAVE ENTRY URL AS ORDER METADATA */
  add_action( 'woocommerce_checkout_update_order_meta', 'save_order_meta' );
  function save_order_meta( $order_id ) {
      add_post_meta( $order_id, 'entry_url', 'https://jmgmediagroup.com/wp-admin/admin.php?page=gf_entries&amp;view=entry&amp;id=3&amp;lid='.$GLOBALS['entry_id'] );
  }
  
  /* SHOW ENTRY URL IN WC ORDER ADMIN AREA */
  add_action( 'woocommerce_admin_order_data_after_billing_address', 'display_entry_url_in_admin_order_meta', 10, 1 );
  function display_entry_url_in_admin_order_meta( $order ) {
      $order_id = method_exists( $order, 'get_id' ) ? $order-&gt;get_id() : $order-&gt;id;
      echo '&lt;p&gt;&lt;strong&gt;'.__('Entry URL', 'woocommerce').':&lt;/strong&gt; ' . get_post_meta( $order_id, 'entry_url', true ) . '&lt;/p&gt;';
  }
  
  /* SHOW ENTRY URL IN EMAIL NOTIFICATIONS*/
  add_action('woocommerce_email_customer_details','add_verification_id_to_emails_notifications', 15, 4 );
  function add_verification_id_to_emails_notifications( $order, $sent_to_admin, $plain_text, $email ) {
      $order_id = method_exists( $order, 'get_id' ) ? $order-&gt;get_id() : $order-&gt;id;
  
      $output = '';
      $url = get_post_meta( $order_id, 'entry_url', true );
  
      if ( !empty($billing_vid) )
          $output .= '&lt;div&gt;&lt;strong&gt;' . __( &quot;Entry URL:&quot;, &quot;woocommerce&quot; ) . '&lt;/strong&gt; &lt;span class=&quot;text&quot;&gt;' . $url . '&lt;/span&gt;&lt;/div&gt;';
  
      echo $output;
  }
  </code></pre>