Created
June 23, 2016 08:42
-
-
Save stevenkellow/48cfc745d80d951b6840421a5c1033c3 to your computer and use it in GitHub Desktop.
Add Referrer to WooCommerce order
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 | |
// Set cookie for new users | |
add_action( 'init', 'set_newuser_cookie'); // Run when WordPress loads | |
function set_newuser_cookie() { | |
$cookie_value = $_SERVER["HTTP_REFERER"]; // Get URL the user came to your site for | |
if ( !is_admin() && !isset($_COOKIE['origin'])) { // If not an admin or if cookie doesn't exist already | |
setcookie( 'origin', $cookie_value, time()+3600*24*30, COOKIEPATH, COOKIE_DOMAIN, false); // Set cookie for 30 days | |
} | |
} | |
// Check cookie when order made and add to order | |
add_action('woocommerce_checkout_update_order_meta', 'add_referral_meta'); // Run when an order is made in WooCommerce | |
function add_referral_meta( $order_id, $posted ){ | |
$ref_url = $_COOKIE['origin']; // Get the cookie | |
update_post_meta( $order_id, 'referrer', $ref_url ); // Add to order meta | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Does not work. Checkout page returns "Internal Server Error"