GeekShop remove /product from product URL

stantyan

Mar 29th, 2017 12:00 PM

Does anyone know how to remove /product from the product URL in GeekShop?

I need the following Before: http://geekshop.devdojo.com/product/pac-man-suit After: http://geekshop.devdojo.com/pac-man-suit

I tried to edit file /routes/web.php at line 28 by removing '/' . setting('product_slug', 'products') .

https://app.box.com/s/gtpvqucau41ydcm8lr9a35cjq02zsruv

but no luck. even though it works for single product pages. All other pages still link to products with this /product in URLs, also when I made this change, www.website.com/blog page is returning error:

https://app.box.com/s/x9vae0inzj9djd2dndyd99a3sb8p56zl

I'm migrating my existing website to GeekShop and I need to remove this /product from all product URLs in order to keep my SEO intact by saving old URL structure.

Thank you in advance.

devdojo

Mar 30th, 2017 12:25 AM

Hey Stantyon,

I actually added the same code and I was able to hit that URL, I changed the route to this:

if(Schema::hasTable('settings')){
		Route::get('/{slug}', 'ProductController@read');
}

And I was able to access a product page on my local machine from geekshop.dev/pac-man-suit, were you able to hit that route. If you want to be able to hit the blog route you will have to change the order of the routes.

Let me know if that helps :)

Thanks.

stantyan

Mar 30th, 2017 02:56 AM

Hi Admin,

Thank you for your reply! Yes, it works for me for single products. However, on the home page and on category pages all the product thumbnails/cards are linking to products with /product in their URL. therefore, since I changed the route not to have it, this results in error.

https://app.box.com/s/6rzznqhgx9bcb9nzndg6fs2f7at3yxzb

I tried to look up what can be done in /resources/views/themes/white/includes/product.blade.php but they all use {{ $product->link() }}. So this {{ $product->link() }} already includes this /product in it. And I have no idea where to change it, or this product URL is hard recorded directly in database?

stantyan

Mar 30th, 2017 11:37 AM

Hi Admin,

I have edited 2 files, in /resources/views/themes/white/posts/browse.blade.php at line 33 I have removed /product from:

  • < a href="/product/{{ $product->slug }}" target="_blank" >

and in /app/Product.php at line 22 I have removed '/' . $slug . from:

  • return '/' . $slug .'/' . $this->slug;

and now on the frontend homepage and category pages, as well as blogposts browse page sidebar, product cards/thumbnails are linked to product pages without /product in their URLs.

Thank you for hints!

On a side note, just curious, is it possible, to remove /blog from blog posts as well while having products pages without /product too? I mean

http://www.website.com/this-is-a-product-page

http://www.website.com/this-is-a-blog-post-page

I tried to remove /blog from the blog routes line in /routes/web.php but it messed up everything.

devdojo

Mar 30th, 2017 01:20 PM

Hey @Stantyan,

There is actually a way to change the link for the products. If you were to go into app/Product.php, you will see a method that looks like this:

public function link(){
    $slug = setting('product_slug');
    if(empty($slug)){
        $slug = 'product';
    }
    return '/' . $slug . '/' . $this->slug;
}

And you can just change it to this:

public function link(){
    return '/' . $this->slug;
 }

and anywhere that $product->link() is used it will now link directly to that product.

In order to remove the /blog from the routes it will require more modification to the controller. Here is the problem, if someone visits a URL that is http://www.website.com/cool-beans, how does it know whether cool-beans is a product or if it is a post. One would have to take priority over the other. If you removed the /blog route and you changed the read method in this app\Http\Controllers\ProductController.php to be:

public function read($slug){
    $product_or_post = Product::where('slug', '=', $slug)->where('active', '=', 1)->first();
    $product = true;
		
		// If a product does not exist, it must be a post
    if(!isset($product_or_post->id)){
        $product_or_post = Post::where('slug', '=', $slug)->firstOrFail();
        $product = false;
    }

	// SEO Content for the page
	$seo_title = $product_or_post->title;
	$meta_description = $product_or_post->meta_description;
	$meta_keywords = $product_or_post->meta_keywords;
	$twitter_description = $product_or_post->meta_description;
	$og_title = $seo_title;
	$og_image = url(Voyager::image($product_or_post->image));

    if($product){
        $product = $product_or_post;
        return view('themes.' . config('geekshop.theme') . '.product.read', compact('product', 'seo_title', 'meta_description', 'meta_keywords', 'twitter_description', 'og_title', 'og_image'));
    } else {
        $random_posts = Post::inRandomOrder()->where('id', '<>', $post->id)->take(4)->get();
        $post = $product_or_post;
        return view('themes.' . config('geekshop.theme') . '.posts.read', compact('post', 'random_posts', 'seo_title', 'meta_description', 'meta_keywords', 'twitter_description', 'og_title', 'og_image'));
    }
}

Now, it should work, but if you have a post with the same slug name as the product, you will not be able to get to it, because that URL will resolve to the product page over the post page.

Hope that helps :)

Thanks. Talk to you soon.