Last active
January 31, 2018 21:19
-
-
Save iconicwp/838fa31c6b7a19fdcafb7d5b79f7666d to your computer and use it in GitHub Desktop.
Redirect add-to-cart form (Woo 3.3) from archive
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 | |
/** | |
* Add a redirect field to the form. | |
*/ | |
function iconic_add_to_cart_redirect_field() { | |
if ( ! is_archive() ) { | |
return; | |
} | |
global $wp; | |
$current_url = home_url( add_query_arg( array(), $wp->request ) ); | |
?> | |
<input type="hidden" name ="iconic_add_to_cart_redirect" value="<?php echo esc_attr( $current_url ); ?>" /> | |
<?php | |
} | |
add_action( 'woocommerce_before_add_to_cart_button', 'iconic_add_to_cart_redirect_field', 10 ); | |
/** | |
* Redirect if field is present. | |
*/ | |
function iconic_add_to_cart_redirect( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ) { | |
$add_to_cart_redirect = filter_input( INPUT_POST, 'iconic_add_to_cart_redirect' ); | |
if ( ! $add_to_cart_redirect ) { | |
return; | |
} | |
wp_safe_redirect( $add_to_cart_redirect, 302 ); | |
die; | |
} | |
add_action( 'woocommerce_add_to_cart', 'iconic_add_to_cart_redirect', 10, 6 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment