Created
January 18, 2022 07:04
-
-
Save fida02/923aeb5df252f85b4110df04c39a36cc to your computer and use it in GitHub Desktop.
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 | |
| // 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