Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save fida02/923aeb5df252f85b4110df04c39a36cc to your computer and use it in GitHub Desktop.

Select an option

Save fida02/923aeb5df252f85b4110df04c39a36cc to your computer and use it in GitHub Desktop.
<?php
// Save referrer url in cookie
function set_referrer_cookie() {
$cookie_value = $_SERVER['HTTP_REFERER'] ?? null; // 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
}
}
add_action( 'init', 'set_referrer_cookie');
// Set referrer URL as cart item
function refferer_to_cart_item( $cart_item_data, $product_id, $variation_id ) {
$themefic_referrer = $_COOKIE['origin']; // Get the cookie
if ( empty( $themefic_referrer ) ) {
return $cart_item_data;
}
$cart_item_data['themefic-referrer'] = $themefic_referrer;
return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data', 'refferer_to_cart_item', 10, 3 );
// Set referrer URL as oder item
function referrer_to_order_items( $item, $cart_item_key, $values, $order ) {
if ( empty( $values['themefic-referrer'] ) ) {
return;
}
$item->add_meta_data( __( 'Referrer', 'iconic' ), $values['themefic-referrer'] );
}
add_action( 'woocommerce_checkout_create_order_line_item', 'referrer_to_order_items', 10, 4 );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment