Pass variable between woocommerce hooks

Viewed 43

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?

/* REGISTER VARIABLE FROM URL QUERY STRING */
function entry($qvar) {
    $qvar[] = "entry";
    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') && $entry != "empty") {
        $entries = GFAPI::get_entry($GLOBALS['entry']);
        $first_name = $entries["4.3"];
        $last_name = $entries["4.6"];
        $email = $entries["5"];
        $phone = $entries["6"];
        
        /* POPULATE CHECKOUT FIELDS */
        $cart = WC()->cart->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&view=entry&id=3&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->get_id() : $order->id;
    echo '<p><strong>'.__('Entry URL', 'woocommerce').':</strong> ' . get_post_meta( $order_id, 'entry_url', true ) . '</p>';
}

/* 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->get_id() : $order->id;

    $output = '';
    $url = get_post_meta( $order_id, 'entry_url', true );

    if ( !empty($billing_vid) )
        $output .= '<div><strong>' . __( "Entry URL:", "woocommerce" ) . '</strong> <span class="text">' . $url . '</span></div>';

    echo $output;
}
0 Answers
Related