Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bernattorras/4ba2982f522cc584c077ac64c995e916 to your computer and use it in GitHub Desktop.
Save bernattorras/4ba2982f522cc584c077ac64c995e916 to your computer and use it in GitHub Desktop.
Function to preserve custom URL parameters on OPC pages after adding a product to the cart
<?php
/**
* Function to preserve custom URL parameters on OPC pages after adding a product to the cart
**/
add_filter( 'woocommerce_add_to_cart_redirect', 'opc_keep_affiliate_arg', 11, 1 );
function opc_keep_affiliate_arg( $url ) {
if ( ! is_ajax() && is_wcopc_checkout() ) {
$schema = is_ssl() ? 'https://' : 'http://';
$url = explode('?', $schema . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"] );
$url = remove_query_arg( array( 'add-to-cart', 'variation_id', 'quantity' ), $url[0] );
//$referral_var = affiliate_wp()->tracking->get_referral_var();
$referral_var = 'aff';
$url = add_query_arg( $referral_var, $_GET[$referral_var], $url );
}
return $url;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment