Zen Cart Logo
Forums / Setting Up Categories, Products, Attributes / How to set up sub-category to instead go onto the first product

How to set up sub-category to instead go onto the first product

Views: 2,001

Results 1 to 20 of 20
6 May 2016, 10:56 AM
#1
dharrison avatar

dharrison

Zen Follower

Join Date:
Mar 2009
Location:
Essex, UK
Posts:
448
Plugin Contributions:
0

How to set up sub-category to instead go onto the first product

Hi

I have a question which is a bit outside the box. Basically we sell several ranges of desks. In each range there are small differences for the same product, for example crank option, motorised adjustment, frame only etc. Desks are either deluxe, professional or economical.

The person I work for wants to override the product listings and instead choose to show the first product of that category. Now I know this is not straightforward (and the person is adamant this is what he wants).

We have categories, sub-categories and product listings, so my question is can this be achieved with code? Even by category id?

I know basic PHP and can follow good instruction so would appreciate any guidance.

Many thanks

Debbie Harrison
DVH Design | Web Design blog

6 May 2016, 2:20 PM
#2
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: How to set up sub-category to instead go onto the first product

Can you explain a little more about what constitutes a category?

Are you using product attributes to describe/define the product such that these "little" differences can be applied directly to the product in front of the customer?

ZC Installation/Maintenance Support <- Site
Contribution for contributions welcome...

6 May 2016, 2:27 PM
#3
dharrison avatar

dharrison

Zen Follower

Join Date:
Mar 2009
Location:
Essex, UK
Posts:
448
Plugin Contributions:
0

Re: How to set up sub-category to instead go onto the first product

mc12345678:

Can you explain a little more about what constitutes a category?

Ok like for instance we have Express category that consists of: ergonomic desks, ergonomic classroom desks and chairs.

Choosing ergonomic desks, you have the option to choose the range: Deluxe, Professional and Economical

If say we choose economical, instead of then seeing the product listings, he would like the first product page to come up instead.

I should point out, that we already have links in place on the page to the other products in the category.

mc12345678:

Are you using product attributes to describe/define the product such that these "little" differences can be applied directly to the product in front of the customer?

We already have attributes for each desk (top size, finishes, etc) but some of the product in the range aren't related. For example even though they are Express Desks, they are different brand names and are priced differently.

Debbie Harrison
DVH Design | Web Design blog

6 May 2016, 2:38 PM
#4
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: How to set up sub-category to instead go onto the first product

So guess a little confused? If say this "last" category is selected, the desire is to show "a" product, but how is one expected to get to each product?

If all that is desired is to have the category show an image, there is the ability to have a sort of category description/image displayed at the top with the product list below.

Guess trying to understand the desired condition still to offer appropriate advice. Anything is possible.

ZC Installation/Maintenance Support <- Site
Contribution for contributions welcome...

6 May 2016, 3:16 PM
#5
dharrison avatar

dharrison

Zen Follower

Join Date:
Mar 2009
Location:
Essex, UK
Posts:
448
Plugin Contributions:
0

Re: How to set up sub-category to instead go onto the first product

mc12345678:

So guess a little confused? If say this "last" category is selected, the desire is to show "a" product, but how is one expected to get to each product?

If all that is desired is to have the category show an image, there is the ability to have a sort of category description/image displayed at the top with the product list below.

Guess trying to understand the desired condition still to offer appropriate advice. Anything is possible.

Hi I can show you a test site is that helps. The product page as it stands is http://dvhdesign.co.uk/sd/economic-height-adjustable-rang-c-37_38_47/adapt-manual-height-adjustable-desk-p-49.html

The icons on the left all link to the other product pages.

I know its a crazy setup, (Believe me, I have echoed this). :frusty:

Debbie Harrison
DVH Design | Web Design blog

6 May 2016, 3:43 PM
#6
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: How to set up sub-category to instead go onto the first product

Okay, so because of my screen "resolution" I didn't have a left side link on which to click. The specific product that is shown at the above link, that appears to be a "standard" product info page. (I say standard in quotes because I didn't go look at the source code to see if there were any tells of it being ZC.) The template used is responsive/mobile and there are all the things of a product with attributes displayed. When/if I used the crumbs links, I was taken to a product listing page that offered information about the list of products (4 in this case or 1 of 4 I think it was).

So, I'm not seeing what is offered at that site that isn't easily available on any other ZC site. Perhaps a link to the existing site of the area to be changed and words (or a picture) to explain what is desired to be different.

ZC Installation/Maintenance Support <- Site
Contribution for contributions welcome...

6 May 2016, 3:55 PM
#7
dharrison avatar

dharrison

Zen Follower

Join Date:
Mar 2009
Location:
Essex, UK
Posts:
448
Plugin Contributions:
0

Re: How to set up sub-category to instead go onto the first product

mc12345678:

Okay, so because of my screen "resolution" I didn't have a left side link on which to click. The specific product that is shown at the above link, that appears to be a "standard" product info page. (I say standard in quotes because I didn't go look at the source code to see if there were any tells of it being ZC.) The template used is responsive/mobile and there are all the things of a product with attributes displayed. When/if I used the crumbs links, I was taken to a product listing page that offered information about the list of products (4 in this case or 1 of 4 I think it was).

So, I'm not seeing what is offered at that site that isn't easily available on any other ZC site. Perhaps a link to the existing site of the area to be changed and words (or a picture) to explain what is desired to be different.

There's no point directing you to the live site, as a lot has changed including this new setup.

We don't want to see the product listings. Instead we would like to see the product page for that first product in the list. In this case http://dvhdesign.co.uk/sd/economic-height-adjustable-rang-c-37_38_47/adapt-manual-height-adjustable-desk-p-49.html

I'm intrigued as what setup you have. Please could you inform me? :smile:

Debbie Harrison
DVH Design | Web Design blog

6 May 2016, 5:20 PM
#8
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: How to set up sub-category to instead go onto the first product

Okay, so there may be a better way to do this, but what I seemed to find was the following based on some of the discussion.

Want to be able to navigate from category to category and see the "sub-categories" in the center of the page, but... When reaching a category that has product (which in ZC in default coding is the way things are expected to be either a category has active product or other sub-categories but not both active product and sub-categories), then display product "0" for that category which is likely to be based on some sort of sort operation...

I didn't chase down every possibility, and there are "cleaner" ways to do this than to literally change code like to use a notifier/observer operation, but during testing a little code mod first could make the difference/easier. :)

Anyways, so that full process is controlled by the includes/modules/pages/index/ code in that folder basically..

When one navigates to/through the categories or back to the main (default) screen, then the code in this area is evaluating the conditions to see what page (listing) should be displayed...

In your case, when a visitor tries to arrive at this "last page", you would like them diverted to the product that would have appeared first on that page... So... here's my thought, which does involve modifying a "non-overrideable" file...

If you open includes/modules/pages/index/main_template_vars.php and then at/around line 196: $tpl_page_body = 'tpl_index_product_list.php';

And just before or just after this, if you add this line which comes from includes/modules/pages/index/header_php.php:

zen_redirect(zen_href_link(zen_get_info_page($listing->fields['products_id']), ($cPath ? 'cPath=' . $cPath . '&' : '') . 'products_id=' . $listing->fields['products_id']));

Then this is likely to at least prove the theory. The next thing would be to ensure that the particular products_id is provided/available if in fact some sort order was applied, but this should redirect to the first item of the return list.

Please try this on your local development site.

As to my "setup". If you mean how am I navigating to see things currently, well, good ole' blackberry... :)

ZC Installation/Maintenance Support <- Site
Contribution for contributions welcome...

7 May 2016, 10:08 AM
#9
dharrison avatar

dharrison

Zen Follower

Join Date:
Mar 2009
Location:
Essex, UK
Posts:
448
Plugin Contributions:
0

Re: How to set up sub-category to instead go onto the first product

Thanks mc, that is much appreciated. Will try what you suggested on my test site on Monday.

As to my "setup". If you mean how am I navigating to see things currently, well, good ole' blackberry... :)

You have more patience than me, I gave up on BlackBerry ages ago. :laugh:

Debbie Harrison
DVH Design | Web Design blog

7 May 2016, 12:49 PM
#10
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: How to set up sub-category to instead go onto the first product

dharrison:

Thanks mc, that is much appreciated. Will try what you suggested on my test site on Monday.

You have more patience than me, I gave up on BlackBerry ages ago. :laugh:

Darn, I was hoping to hear back on how it worked out sooner. Guess I'd have to try it myself to see before then.

I was fortunate that I had a good enough device when I started to need it, and now things are even better. I can download and manipulate zip files, can access github and have multiple browser windows open. The only thing I don't have is proper ftp access. Webftp is possible with particular servers, but want to talk about having to be patient... oye...

ZC Installation/Maintenance Support <- Site
Contribution for contributions welcome...

9 May 2016, 5:47 AM
#11
dharrison avatar

dharrison

Zen Follower

Join Date:
Mar 2009
Location:
Essex, UK
Posts:
448
Plugin Contributions:
0

Re: How to set up sub-category to instead go onto the first product

Hi mc

I added your code first before the line and then after and after the breadcrumb menu, I just get a blank page.

Debbie Harrison
DVH Design | Web Design blog

9 May 2016, 6:08 AM
#12
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: How to set up sub-category to instead go onto the first product

dharrison:

Hi mc

I added your code first before the line and then after and after the breadcrumb menu, I just get a blank page.

Error message in the logs directory? (sequence really didn't matter.)

ZC Installation/Maintenance Support <- Site
Contribution for contributions welcome...

9 May 2016, 6:54 AM
#13
dharrison avatar

dharrison

Zen Follower

Join Date:
Mar 2009
Location:
Essex, UK
Posts:
448
Plugin Contributions:
0

Re: How to set up sub-category to instead go onto the first product

Error Log

[09-May-2016 07:45:15 Europe/Berlin] PHP Warning: Cannot modify header information - headers already sent by (output started at path\to\oursite\includes\templates\dover_fine\templates\tpl_modules_mobile_categories_tabs.php:56) in path\to\oursite\includes\functions\functions_general.php on line 46

Debbie Harrison
DVH Design | Web Design blog

9 May 2016, 1:04 PM
#14
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: How to set up sub-category to instead go onto the first product

mc12345678:

Okay, so there may be a better way to do this, but what I seemed to find was the following based on some of the discussion.

Want to be able to navigate from category to category and see the "sub-categories" in the center of the page, but... When reaching a category that has product (which in ZC in default coding is the way things are expected to be either a category has active product or other sub-categories but not both active product and sub-categories), then display product "0" for that category which is likely to be based on some sort of sort operation...

I didn't chase down every possibility, and there are "cleaner" ways to do this than to literally change code like to use a notifier/observer operation, but during testing a little code mod first could make the difference/easier. :)

Anyways, so that full process is controlled by the includes/modules/pages/index/ code in that folder basically..

When one navigates to/through the categories or back to the main (default) screen, then the code in this area is evaluating the conditions to see what page (listing) should be displayed...

In your case, when a visitor tries to arrive at this "last page", you would like them diverted to the product that would have appeared first on that page... So... here's my thought, which does involve modifying a "non-overrideable" file...

If you open includes/modules/pages/index/main_template_vars.php and then at/around line 196: $tpl_page_body = 'tpl_index_product_list.php';

And just before or just after this, if you add this line which comes from includes/modules/pages/index/header_php.php:

zen_redirect(zen_href_link(zen_get_info_page($listing->fields['products_id']), ($cPath ? 'cPath=' . $cPath . '&' : '') . 'products_id=' . $listing->fields['products_id']));

> 
> Then this is likely to at least *prove* the theory.  The next thing would be to ensure that the particular products_id is provided/available if in fact some sort order was applied, but this should redirect to the first item of the return list.
> 
> Please try this on your local development site.
> 
> As to my "setup". If you mean how am I navigating to see things currently, well, good ole' blackberry... :)

Okay, so how about this, considering a redirect is offered in the following file, we'll just do the applicable tests here and redirect before getting to the main_template_vars.php file:

In includes/modules/pages/index/header_php.php
After the following code and before the final notifier:

// if only one product in this category, go directly to the product page, instead of displaying a link to just one item:
// if filter_id exists the 1 product redirect is ignored
if (SKIP_SINGLE_PRODUCT_CATEGORIES=='True' and (!isset($_GET['filter_id']) and !isset($_GET['alpha_filter_id']))) {
if ($listing->RecordCount() == 1) {
zen_redirect(zen_href_link(zen_get_info_page($listing->fields['products_id']), ($cPath ? 'cPath=' . $cPath . '&' : '') . 'products_id=' . $listing->fields['products_id']));
}
}


Add this code:

if (($category_depth == 'products' || zen_check_url_get_terms()) && $listing->RecordCount() > 0) {
zen_redirect(zen_href_link(zen_get_info_page($listing->fields['products_id']), ($cPath ? 'cPath=' . $cPath . '&' : '') . 'products_id=' . $listing->fields['products_id']));
}

ZC Installation/Maintenance Support <- Site
Contribution for contributions welcome...

9 May 2016, 3:22 PM
#15
dharrison avatar

dharrison

Zen Follower

Join Date:
Mar 2009
Location:
Essex, UK
Posts:
448
Plugin Contributions:
0

Re: How to set up sub-category to instead go onto the first product

It works!!! :clap: Thanks so much.

Debbie Harrison
DVH Design | Web Design blog

9 May 2016, 3:47 PM
#16
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: How to set up sub-category to instead go onto the first product

dharrison:

It works!!! :clap: Thanks so much.
Good to hear. Now there are a few ways that you could proceed...

The file changed is basically a core file. That modification could be moved into a separate file as a sort of plugin to the store (observer) and applied/removed as necessary to suit the store owner's/developer's need.

The alternative is that the code could remain where it is but I would suggest adding comments (start a line with comments with // or if covering multiple lines start with /* and end with */). If the code stays in place in that file, on the next ZC upgrade, attention would need to be maintained in comparing the newly provided file(s) with this file.

ZC Installation/Maintenance Support <- Site
Contribution for contributions welcome...

9 May 2016, 3:52 PM
#17
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: How to set up sub-category to instead go onto the first product

BTW, I was going to start to suggest the code to move this into an observer, but in review of this thread, I don't see mention of the current ZC version. There are two ways to apply the change, one better in a newer store requiring a single file and an older method requiring two files.

ZC Installation/Maintenance Support <- Site
Contribution for contributions welcome...

9 May 2016, 6:22 PM
#18
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: How to set up sub-category to instead go onto the first product

mc12345678:

Okay, so how about this, considering a redirect is offered in the following file, we'll just do the applicable tests here and redirect before getting to the main_template_vars.php file:

In includes/modules/pages/index/header_php.php
After the following code and before the final notifier:

// if only one product in this category, go directly to the product page, instead of displaying a link to just one item:
// if filter_id exists the 1 product redirect is ignored
if (SKIP_SINGLE_PRODUCT_CATEGORIES=='True' and (!isset($_GET['filter_id']) and !isset($_GET['alpha_filter_id']))) {
if ($listing->RecordCount() == 1) {
zen_redirect(zen_href_link(zen_get_info_page($listing->fields['products_id']), ($cPath ? 'cPath=' . $cPath . '&' : '') . 'products_id=' . $listing->fields['products_id']));
}
}

> 
> Add this code:
> ```
if (($category_depth == 'products' || zen_check_url_get_terms()) && $listing->RecordCount() > 0) {
  zen_redirect(zen_href_link(zen_get_info_page($listing->fields['products_id']), ($cPath ? 'cPath=' . $cPath . '&' : '') . 'products_id=' . $listing->fields['products_id']));
}

Create a new file in:
includes/classes/observers
titled: auto.display_first_product_in_cat.php

(The name is chosen to mean something, it can be changed here, but will need to be changed below where the same text is used but the underscores removed)

In the file (nothing to exist before the first <?php):

<?php

/**
 * Uses the observer class modified in ZC 1.5.3 to autoload the below code and not require additional files to obtain the desired result.
 * This is (was?) also PHP 7.0 compliant, though may survive for longer than that as well.
 * 
 * This redirects the visitor to the product info page for the first product that is in the $listing query results.
 * This way, once a customer has dug into the category listing and the category is identified as having product that the first
 *   product is displayed.
 * mc12345678 - 9 May 2016
 **/

class zcObserverDisplayFirstProductInCat extends base 
{
    function __construct() 
    {
        $attachThis = array();

        $attachThis[] = 'NOTIFY_HEADER_END_INDEX';

        $this->attach($this, $attachThis);
    } // end __construct

    // NOTIFY_HEADER_END_INDEX, Code below is to redirect customer to the "first" product identified in the $listing variable.
    //   if that variable has been modified by another observer before this one, then it resets to the first item in the list.
    function updateNotifyHeaderEndIndex(&$callingClass, $notifier) 
    {
        global $category_depth, $listing, $cPath;

        if (($category_depth == 'products' || zen_check_url_get_terms()) && $listing->RecordCount() > 0) 
        {
            // This next section makes this code upgrade compatible to ZC 1.5.5+ and resets the listing in case some other 
            //  header listening observer gets run before this one does and it modifies the pointer to the results. :)
            if (method_exists($listing, 'rewind'))
            {
                reset($listing);
            } else {
                $listing->Move(0);
                $listing->MoveNext();
            }

            zen_redirect(zen_href_link(zen_get_info_page($listing->fields['products_id']), ($cPath ? 'cPath=' . $cPath . '&' : '') . 'products_id=' . (int)$listing->fields['products_id']));
        } // end if
    } // end function updateNotifyHeaderEndIndex
} // end class definition

ZC Installation/Maintenance Support <- Site
Contribution for contributions welcome...

10 May 2016, 12:07 PM
#19
dharrison avatar

dharrison

Zen Follower

Join Date:
Mar 2009
Location:
Essex, UK
Posts:
448
Plugin Contributions:
0

Re: How to set up sub-category to instead go onto the first product

Does this file change having the overrides in the 2 files that we changed?

Current ZC version is 1.5.4. I thought that was the thread that I posted in.

Regardless thanks for your help

Debbie Harrison
DVH Design | Web Design blog

10 May 2016, 12:50 PM
#20
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: How to set up sub-category to instead go onto the first product

dharrison:

Does this file change having the overrides in the 2 files that we changed?

Current ZC version is 1.5.4. I thought that was the thread that I posted in.

Regardless thanks for your help
Actually with this extra file you can restore the other two files to their original ZC grandness. :)

As previously identified the change made to $main_template_vars.php caused headers previously sent error, the change made to header_php.php is addressed by the above provided code.

As for your process of posting, you did tag the post as 1.5.4 and as might be visible in the below, the screenshot from my device, the "indicator" might be visible on the upper left instead of inline like DrByte showed. Attachment 16293

ZC Installation/Maintenance Support <- Site
Contribution for contributions welcome...