Post by Joe1234 » Fri Jun 17, 2022 10:04 pm

I have the following code that gives me the ability to go to the previous and next product within the same category. I have SEO URL set up. The problem is everything works fine EXCEPT when I enter a product from a category page. So if I land on a product from a module/front page/related/direct link/whatever the next and previous buttons will work fine. But if I go through the menu system and enter a product from a category page the next and previous buttons don't work, they go to a "page not found" page error.

In the situation where it wont work, when I hover over the previous/next buttons you see that it is a regular url instead of the seo. Also, in the URL input I can see the category SEO is added in the URL, it's domain/dir/cat/product.

In the situation where it does work, when I hover over the previous/next buttons you see that it is the SEO URL. Also, in the URL input the category SEO is not within the URL, it's just the domain/dir/product.

Code: Select all

	public function PreNextbyCat($data = array()){
        $filter = '';
        if (!empty($data['filter_category_id'])) {
              if (!empty($data['filter_sub_category'])) {
                 $implode_data = array();

                 $implode_data[] = "p2c.category_id = '" . (int)$data['filter_category_id'] . "'";

                 $this->load->model('catalog/category');

                 $categories = $this->model_catalog_category->getCategoriesByParentId($data['filter_category_id']);

                 foreach ($categories as $category_id) {
                    $implode_data[] = "p2c.category_id = '" . (int)$category_id . "'";
                 }

                 $filter .= " AND (" . implode(' OR ', $implode_data) . ")";
              } else {
                 $filter .= " AND p2c.category_id = '" . (int)$data['filter_category_id'] . "'";
              }
           }

		$query1 = $this->db->query("SELECT category_id AS TARGET FROM " . DB_PREFIX . "product_to_category WHERE product_id = '" . $data['product_id'] . "' LIMIT 1 OFFSET 2");
	    $targetcat =  $query1->row['TARGET'];

		$nextquery = "SELECT p.product_id as next,p2d.name next_name FROM ".DB_PREFIX."product p LEFT JOIN ".DB_PREFIX."product_to_category p2c ON(p.product_id = p2c.product_id) LEFT JOIN ".DB_PREFIX."product_description p2d ON(p.product_id = p2d.product_id) LEFT JOIN ".DB_PREFIX."product_to_store p2s ON(p.product_id = p2s.product_id) WHERE p2d.language_id = '".(int)$this->config->get('config_language_id')."' AND p2s.store_id = '".(int)$this->config->get('config_store_id')."' AND p.status = '1' AND p.date_available <= NOW() AND p2c.category_id = '" .$targetcat. "' AND p.product_id > '".$data['product_id']."' ".$filter." ORDER BY p.product_id ASC LIMIT 1";
		$prevquery = "SELECT p.product_id as prev,p2d.name prev_name FROM ".DB_PREFIX."product p LEFT JOIN ".DB_PREFIX."product_to_category p2c ON(p.product_id = p2c.product_id) LEFT JOIN ".DB_PREFIX."product_description p2d ON(p.product_id = p2d.product_id) LEFT JOIN ".DB_PREFIX."product_to_store p2s ON(p.product_id = p2s.product_id) WHERE p2d.language_id = '".(int)$this->config->get('config_language_id')."' AND p2s.store_id = '".(int)$this->config->get('config_store_id')."' AND p.status = '1' AND p.date_available <= NOW() AND p2c.category_id = '" .$targetcat. "' AND p.product_id < '".$data['product_id']."' ".$filter." ORDER BY p.product_id DESC LIMIT 1";

        $prev = $this->db->query($prevquery)->row;
        $next = $this->db->query($nextquery)->row;
        $query = array_merge($prev,$next);
        return $query;

     }
The controller looks like

Code: Select all

 
				 $data['next_link'] = $this->url->link('product/product', $category_link . 'product_id=' . $results['next']);
			
How can I fix this so it works no matter where I'm coming from?
Last edited by Joe1234 on Tue Aug 09, 2022 10:24 am, edited 2 times in total.

v3.0.3.9 php 8.1
I'm here for a reason, if your response is contact a/the developer, just don't reply.


Active Member

Posts

Joined
Sat Jan 01, 2022 5:47 am

Post by thekrotek » Sun Aug 07, 2022 5:14 pm

Hard to say, but most likely the problem is with the query you pass to link() function. It simply can not find the matching alias (keyword) in the database.

Professional OpenCart extensions, support and custom work.
Contact me via email or Skype by support@thekrotek.com


User avatar
Expert Member

Posts

Joined
Sun Jul 03, 2016 12:24 am


Post by JNeuhoff » Sun Aug 07, 2022 5:51 pm

Could you share your website URL with us please? I am not sure what exactly you are talking about since a normal product pages doesn't have a next or previous link.

Export/Import Tool * SpamBot Buster * Unused Images Manager * Instant Option Price Calculator * Number Option * Google Tag Manager * Survey Plus * OpenTwig


User avatar
Guru Member

Posts

Joined
Wed Dec 05, 2007 3:38 am


Post by Joe1234 » Tue Aug 09, 2022 10:23 am

I fixed the problem. I changed
This:

Code: Select all

$data['next_link'] = $this->url->link('product/product', $category_link . 'product_id=' . $results['next']);
To:

Code: Select all

$data['next_link'] = $this->url->link('product/product', 'product_id=' . $results['next']);
I'm not 100% if there was one other thing I changed, but I can't remember right now.

v3.0.3.9 php 8.1
I'm here for a reason, if your response is contact a/the developer, just don't reply.


Active Member

Posts

Joined
Sat Jan 01, 2022 5:47 am
Who is online

Users browsing this forum: No registered users and 99 guests