Category route problem

Phoca Cart - complex e-commerce extension
User avatar
Jan
Phoca Hero
Phoca Hero
Posts: 48403
Joined: 10 Nov 2007, 18:23
Location: Czech Republic
Contact:

Re: Category route problem

Post by Jan »

I have done some quick tests and seems like this can solve the situation with question view:

Code: Select all

if($advanced == 1){
		    
		    $segmentId = '';
            $segmentCatid = '';

            for ($i = 0; $i < $total; $i++){

                if(isset($segments[$i]) && $i == 0 && in_array($segments[$i], $viewsNotOwnId)) {
                    $vars['view'] = $segments[$i];
                    continue;
                }

                if(empty($segmentCatid)){

                    $query = $db->getQuery(true)
                        ->select($db->quoteName(array('id')))
                        ->from($db->quoteName('#__phocacart_categories'))
                        ->where($db->quoteName('alias') . ' = ' . $db->quote($segments[$i]));
                    $db->setQuery($query);
                    $segmentCatid = $db->loadResult();
                    if(!empty($segmentCatid)) {
                        $segments[$i] = $segmentCatid.'-'.$segments[$i];
                        continue;
                    }

                }

                if (empty($segmentId)){

                    //$segmentProductId = '';
                    $query = $db->getQuery(true)
                        ->select($db->quoteName(array('id')))
                        ->from($db->quoteName('#__phocacart_products'))
                        ->where($db->quoteName('alias') . ' = ' . $db->quote($segments[$i]));
                    $db->setQuery($query);
                    $segmentId = $db->loadResult();

                    if(!empty($segmentId)) {
                        $segments[$i] = $segmentId.'-'.$segments[$i];
                    }
                }

            }
            $advanced = 0;
        }
But there are still problems when using more menu links to eshop (e.g. to categories view and to category view)

This is even problematic:

Code: Select all

$segments[$i] = preg_replace('/-/', ':', $segments[$i], 1);
because it changes the aliases. E.g. product name is "Product 2", product alias is "product-2" and this alias is changed to "product:2" which is not correct.

EDIT: seems like the last solution I have added (05/04/2020 15:57), solves the issue with more menu links :idea:

If there will be no other issues, I can add it to 3.5.0 stable as expermintal setting:

Image

Jan
If you find Phoca extensions useful, please support the project
User avatar
Jan
Phoca Hero
Phoca Hero
Posts: 48403
Joined: 10 Nov 2007, 18:23
Location: Czech Republic
Contact:

Re: Category route problem

Post by Jan »

It can be tested in 3.5.0RC2 version:
https://github.com/PhocaCz/PhocaCart/re ... 5.0RC2.zip

Image

Jan
If you find Phoca extensions useful, please support the project
NickS
Phoca Member
Phoca Member
Posts: 21
Joined: 25 Mar 2020, 23:54

Re: Category route problem

Post by NickS »

Thank you for your attention.

I really did not check the work of everything, but only products and categories, calculation and personal account.

I am very happy that your extension is getting better and better!
NickS
Phoca Member
Phoca Member
Posts: 21
Joined: 25 Mar 2020, 23:54

Re: Category route problem

Post by NickS »

I tested the new router-it doesn't work correctly!
This way we will always get the ID of the FIRST category in the url. If we have several categories in the url, we will never go to them.

Code: Select all

               
                if(empty($segmentCatid)){

                    $query = $db->getQuery(true)
                        ->select($db->quoteName(array('id')))
                        ->from($db->quoteName('#__phocacart_categories'))
                        ->where($db->quoteName('alias') . ' = ' . $db->quote($segments[$i]));
                    $db->setQuery($query);
                    $segmentCatid = $db->loadResult();
                    if(!empty($segmentCatid)) {
                        $segments[$i] = $segmentCatid.'-'.$segments[$i];
                        continue;
                    }

                }
                
mainpage/skoda/ - category with ID=1 (alias "skoda"), OK
mainpage/skoda/suv/ - We have found the ID=1 by alias "skoda" and are no longer looking for the next one (alias "suv")
mainpage/skoda/suv/premium/ - We have found the ID=1 and so on

so for me works the next code (from line 405 to line 447):

Code: Select all

               
if($advanced == 1){

		    $segmentId = '';
            $segmentCatid = '';

            for ($i = 0; $i < $total; $i++){

                if(isset($segments[$i]) && $i == 0 && in_array($segments[$i], $viewsNotOwnId)) {
                    $vars['view'] = $segments[$i];
                    continue;
                }

                    $query = $db->getQuery(true)
                        ->select($db->quoteName(array('id')))
                        ->from($db->quoteName('#__phocacart_categories'))
                        ->where($db->quoteName('alias') . ' = ' . $db->quote($segments[$i]));
                    $db->setQuery($query);
                    $segmentCatid = $db->loadResult();
//we found or did not find the category ID, we will check it later


                if (empty($segmentId)){

                    //$segmentProductId = '';
                    $query = $db->getQuery(true)
                        ->select($db->quoteName(array('id')))
                        ->from($db->quoteName('#__phocacart_products'))
                        ->where($db->quoteName('alias') . ' = ' . $db->quote($segments[$i]));
                    $db->setQuery($query);
                    $segmentId = $db->loadResult();

                    if(!empty($segmentId)) {
                        $segments[$i] = $segmentId.'-'.$segments[$i];
                    }
                }

            }
if(!empty($segmentCatid)) {
//we found one or more category IDS, and add the last ID to the beginning of the line (to the first segment)
	$segments[0] = $segmentCatid.'-'.$segments[0];
}
//disable advanced mode and then the router works as before
            $advanced = 0;
        }
         
NickS
Phoca Member
Phoca Member
Posts: 21
Joined: 25 Mar 2020, 23:54

Re: Category route problem

Post by NickS »

Some corrections:

Code: Select all

if($advanced == 1){

$segmentId = '';
$segmentCatid = '';

            for ($i = 0; $i < $total; $i++){

                if(isset($segments[$i]) && $i == 0 && in_array($segments[$i], $viewsNotOwnId)) {
                    $vars['view'] = $segments[$i];
                    continue;
                }
$segmentCatidTemp = '';
                    $query = $db->getQuery(true)
                        ->select($db->quoteName(array('id')))
                        ->from($db->quoteName('#__phocacart_categories'))
                        ->where($db->quoteName('alias') . ' = ' . $db->quote($segments[$i]));
                    $db->setQuery($query);
                    $segmentCatidTemp = $db->loadResult();
					if(!empty($segmentCatidTemp)) {
                        $segmentCatid = $segmentCatidTemp;
                        continue;
                    }				
//we found or did not find the category ID, we will check it later


                if (empty($segmentId)){

                    //$segmentProductId = '';
                    $query = $db->getQuery(true)
                        ->select($db->quoteName(array('id')))
                        ->from($db->quoteName('#__phocacart_products'))
                        ->where($db->quoteName('alias') . ' = ' . $db->quote($segments[$i]));
                    $db->setQuery($query);
                    $segmentId = $db->loadResult();

                    if(!empty($segmentId)) {
                        $segments[$i] = $segmentId.'-'.$segments[$i];
                    }
                }

            }
if(!empty($segmentCatid)) {
//we found one or more category IDS, and add the last ID to the beginning of the line (to the first segment)
	$segments[0] = $segmentCatid.'-'.$segments[0];
}
//disable advanced mode and then the router works as before
            $advanced = 0;
        }
        
User avatar
Jan
Phoca Hero
Phoca Hero
Posts: 48403
Joined: 10 Nov 2007, 18:23
Location: Czech Republic
Contact:

Re: Category route problem

Post by Jan »

The problem of the last correction is, it adds ID for quick view, but quick view should not be handled as category:

Image

Jan
If you find Phoca extensions useful, please support the project
User avatar
Jan
Phoca Hero
Phoca Hero
Posts: 48403
Joined: 10 Nov 2007, 18:23
Location: Czech Republic
Contact:

Re: Category route problem

Post by Jan »

Try to test this code:

Code: Select all

if($advanced == 1){

            $segmentId = '';
            $segmentCatid = '';
            // As default first part is category but it can even be view
            // If it is a view, shift they key to next part
            $segmentCatidKey = 0;

            for ($i = 0; $i < $total; $i++){

                if(isset($segments[$i]) && $i == 0 && in_array($segments[$i], $viewsNotOwnId)) {
                    $segmentCatidKey = 1;// First part is a view (e.g. Quick View), shift the key to next part
                    $vars['view'] = $segments[$i];
                    continue;
                }

                $segmentCatidTemp = '';
                $query = $db->getQuery(true)
                    ->select($db->quoteName(array('id')))
                    ->from($db->quoteName('#__phocacart_categories'))
                    ->where($db->quoteName('alias') . ' = ' . $db->quote($segments[$i]));
                $db->setQuery($query);
                $segmentCatidTemp = $db->loadResult();
                if(!empty($segmentCatidTemp)) {
                    $segmentCatid = $segmentCatidTemp;
                    continue;
                }

                //we found or did not find the category ID, we will check it later
                if (empty($segmentId)){

                    //$segmentProductId = '';
                    $query = $db->getQuery(true)
                        ->select($db->quoteName(array('id')))
                        ->from($db->quoteName('#__phocacart_products'))
                        ->where($db->quoteName('alias') . ' = ' . $db->quote($segments[$i]));
                    $db->setQuery($query);
                    $segmentId = $db->loadResult();

                    if(!empty($segmentId)) {

                        $segments[$i] = $segmentId.'-'.$segments[$i];
                    }
                }

            }
            
            if(!empty($segmentCatid)) {
                //we found one or more category IDS, and add the last ID to the beginning of the line (to the first segment)
                $segments[$segmentCatidKey] = $segmentCatid.'-'.$segments[$segmentCatidKey];
            }
            //disable advanced mode and then the router works as before
            $advanced = 0;
        }
I have added only the function to schift the key of $segmentCatid variable in case, it is not a category but view. It should not affect the code, you have add as last.

It looks like it can work even with subcategories:

Image

0 ... view
1 ... 2 id of subcategory, "category" is alias of parent category
2 ... alias of subcategory (id is included in parent)
4 ... 3 id of product including its alias ... so the link is OK


But next problem is returning status:
phoca-cart/category/subcategory/not-existing-product

Category exists, subcategory exists but "not-existing-product" does not exists:

- it should return 404 for not found category
- or return 404 for not found product
- but it returns 200 with displayed subcategory

The same with not existing subcategory:
phoca-cart/category/not-existing-subcategory


Jan
If you find Phoca extensions useful, please support the project
NickS
Phoca Member
Phoca Member
Posts: 21
Joined: 25 Mar 2020, 23:54

Re: Category route problem

Post by NickS »

Thanks! The new router works OK!
But next problem is returning status:
phoca-cart/category/subcategory/not-existing-product

Category exists, subcategory exists but "not-existing-product" does not exists:

- it should return 404 for not found category
- or return 404 for not found product
- but it returns 200 with displayed subcategory
I don't see any problem with this, the router works similarly on a stable version of Phoca Cart:
https://www.phoca.cz/phocacartdemo/3-audi/10-audi-a1
https://www.phoca.cz/phocacartdemo/3-audi/audi-a1 - returns 200 with displayed subcategory
NickS
Phoca Member
Phoca Member
Posts: 21
Joined: 25 Mar 2020, 23:54

Re: Category route problem

Post by NickS »

Hi! Do you plan to add a new router to RC version?
User avatar
Jan
Phoca Hero
Phoca Hero
Posts: 48403
Joined: 10 Nov 2007, 18:23
Location: Czech Republic
Contact:

Re: Category route problem

Post by Jan »

Hi, yes, it is there. I will be released in 3.5.0 Stable version. It will be possible to enable/disable it per expert options, so it can be disabled, if there will be some issues and it can be improved in some of the next versions.

Jan
If you find Phoca extensions useful, please support the project
Post Reply