{"action":"created","issue":{"url":"https://api.github.com/repos/AffiliateWP/AffiliateWP/issues/2548","repository_url":"https://api.github.com/repos/AffiliateWP/AffiliateWP","labels_url":"https://api.github.com/repos/AffiliateWP/AffiliateWP/issues/2548/labels{/name}","comments_url":"https://api.github.com/repos/AffiliateWP/AffiliateWP/issues/2548/comments","events_url":"https://api.github.com/repos/AffiliateWP/AffiliateWP/issues/2548/events","html_url":"https://github.com/AffiliateWP/AffiliateWP/issues/2548","id":293031472,"number":2548,"title":"Preventing affiliate links being stripped from URLs due to wp_redirect()","user":{"login":"amdrew","id":52581,"avatar_url":"https://avatars2.githubusercontent.com/u/52581?v=4","gravatar_id":"","url":"https://api.github.com/users/amdrew","html_url":"https://github.com/amdrew","followers_url":"https://api.github.com/users/amdrew/followers","following_url":"https://api.github.com/users/amdrew/following{/other_user}","gists_url":"https://api.github.com/users/amdrew/gists{/gist_id}","starred_url":"https://api.github.com/users/amdrew/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/amdrew/subscriptions","organizations_url":"https://api.github.com/users/amdrew/orgs","repos_url":"https://api.github.com/users/amdrew/repos","events_url":"https://api.github.com/users/amdrew/events{/privacy}","received_events_url":"https://api.github.com/users/amdrew/received_events","type":"User","site_admin":false},"labels":[{"id":72778873,"url":"https://api.github.com/repos/AffiliateWP/AffiliateWP/labels/enhancement","name":"enhancement","color":"70adee","default":true},{"id":121079169,"url":"https://api.github.com/repos/AffiliateWP/AffiliateWP/labels/i:WooCommerce","name":"i:WooCommerce","color":"96588a","default":false}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":{"url":"https://api.github.com/repos/AffiliateWP/AffiliateWP/milestones/88","html_url":"https://github.com/AffiliateWP/AffiliateWP/milestone/88","labels_url":"https://api.github.com/repos/AffiliateWP/AffiliateWP/milestones/88/labels","id":2338853,"number":88,"title":"Future Release","description":"","creator":{"login":"DrewAPicture","id":995399,"avatar_url":"https://avatars2.githubusercontent.com/u/995399?v=4","gravatar_id":"","url":"https://api.github.com/users/DrewAPicture","html_url":"https://github.com/DrewAPicture","followers_url":"https://api.github.com/users/DrewAPicture/followers","following_url":"https://api.github.com/users/DrewAPicture/following{/other_user}","gists_url":"https://api.github.com/users/DrewAPicture/gists{/gist_id}","starred_url":"https://api.github.com/users/DrewAPicture/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/DrewAPicture/subscriptions","organizations_url":"https://api.github.com/users/DrewAPicture/orgs","repos_url":"https://api.github.com/users/DrewAPicture/repos","events_url":"https://api.github.com/users/DrewAPicture/events{/privacy}","received_events_url":"https://api.github.com/users/DrewAPicture/received_events","type":"User","site_admin":false},"open_issues":252,"closed_issues":5,"state":"open","created_at":"2017-02-21T20:42:14Z","updated_at":"2018-02-01T04:31:39Z","due_on":null,"closed_at":null},"comments":2,"created_at":"2018-01-31T04:58:19Z","updated_at":"2018-02-01T22:13:20Z","closed_at":null,"author_association":"OWNER","body":"Affiliate links tend to get stripped from URLs when redirects are performed (only thinking of `wp_redirect()` at this point). Because of this, the cookies aren't created and the visit isn't stored. This seems to crop up a lot in support, mostly due to redirects, but sometimes conflicting plugins.\r\n\r\n### Example 1\r\n\r\nA site owner has redirects set up via a plugin or custom code. We've seen in support where they purposely have their affiliates link to one page on their site, and then they redirect the visitor to another page:\r\n\r\n`https://site.com/special/?ref=1` -> `https://site.com/the-actual-special-page/`\r\n\r\nA common reason to do this is so the affiliate only ever needs to remember/link to _one_ URL. The site owner can then update the redirect at a later point when there is a new promotion on a different page (and they don't want to remove the other one for whatever reason).\r\n\r\n### Example 2\r\n\r\nWooCommerce has a `Redirect to the cart page after successful addition ` setting:\r\n\r\n<img width=\"613\" alt=\"screenshot 2018-01-31 17 28 37\" src=\"https://user-images.githubusercontent.com/52581/35605223-30bf4e2a-06ac-11e8-9781-8c5d734e7209.png\">\r\n\r\nIf this link is used:\r\n`https://site.com/?add-to-cart=9&ref=123`\r\n\r\nIt will instantly redirect to:\r\n\r\n`https://site.com/cart/`\r\n\r\nBecause of the redirect, the cookies aren't stored, so no visit either.\r\n\r\n### Possible solution\r\n\r\nWe could grab the affiliate link just before the redirect is performed, and then append it back to the URL _after_ the redirect has completed. For the 2nd example it would look like this:\r\n\r\n`https://site.com/?add-to-cart=9&ref=123` -> `https://site.com/cart/?ref=123`\r\n\r\nWooCommerce has a `woocommerce_get_cart_url` filter that we can hook into to fix that scenario:\r\n\r\n``` php\r\n/**\r\n * WooCommerce redirects the customer to the cart page when a specific setting is enabled.\r\n * This is called \"Redirect to the cart page after successful addition\", found at\r\n * WooCommerce -> Settings -> Products -> Display.\r\n * If an affiliate link is used in addition to an add to cart link, the AffiliateWP\r\n * related cookies are never stored in the browser and a \"visit\" is never created. \r\n * Example link: https://site.com/shop/?add-to-cart=9&ref=3\r\n * \r\n * The code snippet takes the referral variable and value used in the URL before\r\n * the redirect, and then appends them to the cart page after the redirect, allowing\r\n * the cookies to be stored.\r\n * \r\n * @todo Make it work with pretty affiliate links\r\n *\r\n */\r\nfunction affwp_woocommerce_get_cart_url( $redirect ) {\r\n\r\n\tif ( 'yes' !== get_option( 'woocommerce_cart_redirect_after_add' ) ) {\r\n\t\treturn $redirect;\r\n\t}\r\n\r\n\tif ( ! function_exists( 'affiliate_wp' ) ) {\r\n\t\treturn $redirect;\r\n\t}\r\n\r\n\t// Get the referral variable being used in AffiliateWP.\r\n\t$referral_var = affiliate_wp()->tracking->get_referral_var();\r\n\r\n\t// Append the referral variable and value to the URL after the redirect.\r\n\t$redirect = add_query_arg( $referral_var, $_GET[$referral_var], wc_get_page_permalink( 'cart' ) );\r\n\r\n\treturn $redirect;\r\n\r\n}\r\nadd_filter( 'woocommerce_get_cart_url', 'affwp_woocommerce_get_cart_url', 10, 1 );\r\n```\r\n\r\nBut if we're going to also support redirects performed by other plugins and code snippets, we could probably catch all of them if they are using `wp_redirect()` (which WooCommerce obviously uses). \r\n\r\nBetter example:\r\n\r\n``` php\r\nfunction affwp_redirect_with_referral_link( $location, $status ) {\r\n\r\n\tif ( ! function_exists( 'affiliate_wp' ) ) {\r\n\t\treturn $location;\r\n\t}\r\n\r\n\t// Get the referral variable being used in AffiliateWP.\r\n\t$referral_var = affiliate_wp()->tracking->get_referral_var();\r\n\r\n\t// Append the referral variable and value to the URL after the redirect.\r\n\t$location = add_query_arg( $referral_var, $_GET[$referral_var], $location );\r\n\r\n\treturn $location;\r\n\r\n}\r\nadd_filter( 'wp_redirect', 'affwp_redirect_with_referral_link', 10, 2 );\r\n```\r\n\r\nI can't think of any ramifications to the above. It would solve a common pain point, provided the redirect was performed using `wp_redirect()`.\r\n\r\nIt would also need to:\r\n\r\n1. Check if an affiliate link is actually being used\r\n2. Support pretty affiliate links\r\n\r\nSince WooCommerce has an option within their plugin that can \"break\" an affiliate link it's something we should probably address soon.\r\n\r\nThis feature could be \"always on\", or another option would be to have as an option under our misc settings.\r\n\r\nWould love to hear any additional thoughts, particularly related to this conflicting with anything else, or causing more problems than it intends to solve.\r\n\r\nRelated tickets:\r\nhttps://secure.helpscout.net/conversation/515295401/75199?folderId=205529\r\nhttps://secure.helpscout.net/conversation/499019104/73495?folderId=205531"},"comment":{"url":"https://api.github.com/repos/AffiliateWP/AffiliateWP/issues/comments/362419824","html_url":"https://github.com/AffiliateWP/AffiliateWP/issues/2548#issuecomment-362419824","issue_url":"https://api.github.com/repos/AffiliateWP/AffiliateWP/issues/2548","id":362419824,"user":{"login":"pippinsplugins","id":1034109,"avatar_url":"https://avatars1.githubusercontent.com/u/1034109?v=4","gravatar_id":"","url":"https://api.github.com/users/pippinsplugins","html_url":"https://github.com/pippinsplugins","followers_url":"https://api.github.com/users/pippinsplugins/followers","following_url":"https://api.github.com/users/pippinsplugins/following{/other_user}","gists_url":"https://api.github.com/users/pippinsplugins/gists{/gist_id}","starred_url":"https://api.github.com/users/pippinsplugins/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pippinsplugins/subscriptions","organizations_url":"https://api.github.com/users/pippinsplugins/orgs","repos_url":"https://api.github.com/users/pippinsplugins/repos","events_url":"https://api.github.com/users/pippinsplugins/events{/privacy}","received_events_url":"https://api.github.com/users/pippinsplugins/received_events","type":"User","site_admin":false},"created_at":"2018-02-01T22:13:20Z","updated_at":"2018-02-01T22:13:20Z","author_association":"CONTRIBUTOR","body":"I don't see any reason not to do this. Would prevent a _lot_ of tickets. Like you mentioned, it's super common for site owners to set up redirects like this and expect them to work."}}
{"actor":{"display_login":"pippinsplugins"}}