Save Google Ads GCLID value to Abandoned Carts
-
I would love to store the gclid parameter similar to the abandoned carts similar to how this code snippet I found on StackOverflow does it for orders.
Any suggestions? Thanks!
// Adding Google Adwords gclid parameter to Woocommerce Database add_action( 'init', 'gclid_session' ); function gclid_session () { // Early initialize customer session if ( isset(WC()->session) && ! WC()->session->has_session() ) { WC()->session->set_customer_session_cookie( true ); } if ( isset( $_GET['gclid'] ) ) { $gclid_ide = isset( $_GET['gclid'] ) ? esc_attr( $_GET['gclid'] ) : ''; // Set the session data WC()->session->set( 'gclid_id', $gclid_ide ); } } // Save session data as order meta data add_action( 'woocommerce_checkout_create_order', 'set_order_gclid_meta' ); function set_order_gclid_meta( $order ) { $gclid_ide = WC()->session->get( 'gclid_id' ); // Get custom data from session if ( isset($gclid_ide) ) { $order->update_meta_data( '_gclid', $gclid_ide ); } WC()->session->__unset( 'gclid_id' ); // Remove session variable } // Show this session variables in new order email for admin and in woocommerce order page // add_action( 'woocommerce_email_after_order_table', 'show_gclid_data', 20 ); add_action( 'woocommerce_admin_order_data_after_billing_address', 'show_gclid_data' ); function show_gclid_data( $order ) { if ( $id_gclid = $order->get_meta( '_gclid' ) ) echo '<p><strong>gclid :</strong> ' . $id_gclid . '</p>'; }
The page I need help with: [log in to see the link]
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘Save Google Ads GCLID value to Abandoned Carts’ is closed to new replies.