PHP newbie here.. I am trying to create a function where the user is redirected back to the previous page after adding an item to their cart. However, when they are redirected back with the code below it leaves a duplicate of the directory (ie. example.com/catalog/garden-tools/garden-tools
function custom_add_to_cart_redirect() {
$_SERVER['REQUEST_URI_PATH'] = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$url = explode('/', rtrim($_SERVER['REQUEST_URI_PATH'], '/'));
return $url[3];
}
A print_r reveals the following:
Array ( [0] => [1] => thefarmshop [2] => catalog [3] => garden-tools [4] => gardening-glove )
What could the issue be?
Since the url is exploded, you get an array like:
$url[ 0 ] = "http:"$url[ 1 ] = "SOME_STRING"and so on.You need to check what is the index of the wanted string in your url array and return it.
Like
return $url[ 2 ]or whatever the index is.UPDATE
The
print_r( $array )shows the$arraystructure including indices and values.