Forum

Thread tagged as: Question, Shop

Shop page titles

Hi How do I make my product name or category name dynamically appear in the page title? I found information when using a blog post by setting <title><?php perch_blog_post_field(perch_get('s'), 'postTitle'); ?></title> but can't seem to find an equivalent for the shop.

I'm using runway btw.

William Chicken

William Chicken 1 points

  • 5 years ago

This is what I have..

Header.php

    <title><?php perch_layout_var('title');?></title>

Categories layout

<?php
      $title = perch_categories(array(
         'template' => 'categorytitle.html',
         'filter' => 'catTitle',
         'match' => 'eq',
         'value' => perch_get('s')
      ), true);
         perch_layout('global/cat-header', [
         'body-class' => 'home',
         'title'=>$title,
]); ?>

categorytitle.html

<perch:categories id="category" set="products">
    <perch:category id="catTitle" />
</perch:categories>
Drew McLellan

Drew McLellan 2638 points
Perch Support

You're filtering using the category title - is that really what you have in the URL? Normally that would be the category slug or category path.

I think you probably want something like:

$title = perch_categories(array(
         'template' => 'categorytitle.html',
         'filter' => 'catPath',
         'match' => 'eq',
         'value' => perch_get('s')
), true);

where the value of s on your URL is a path like products/trousers

Something like 'products/trousers' or on the page I'm testing with 'greetings-cards/birthdays' is exactly what I want in my title.

I retrieve my url with

<? perch_shop_products([
     'count' => 10,
     'category' => array('products/' . perch_get('cat')) ,
  ]); ?>

So is anything wrong in my html template. I've tried changing it also to catPath but no change.

I'm getting this in debug with both set to catPath

SELECT setID FROM perch2_category_sets WHERE setSlug='' LIMIT 1 0 SELECT main.* FROM perch2_categories main WHERE 1=1 AND (catPath='') ORDER BY catTreePosition ASC nil

Rachel Andrew

Rachel Andrew 394 points
Perch Support

Have you tried hardcoding the path to identify whether the problem is that what you think is being passed in is not? As your debug shows that the path is not being passed in.

Ok I've gone back to my layout and tried to rework it as I was creating more and more pages to get to my sub pages with names like ' sub sub category ' which didn't seem right. I also had many else if perch get() statements in order to get me there in my category php.
This is probably why I couldn't access the link for my title. So i'm trying to rework it to be more dynamic but need a little help or explanation.

I now have this in my category page

      perch_categories([
        'template' => 'category.html',
        'set' => 'products',
      ]);

That provides my links which open this on my product.php page

if(perch_get('s')) {
      perch_shop_products([
        'template' => 'products/list.html',
                'category' => 'products/'.perch_get('s'),
      ]);


    }else{

    perch_shop_product(perch_get('product'), [
                'template' => 'products/list.html',
  ]);
}

but I can only go one category deep. After that no products show. My routes are working ok as each link does open and debug shows it is loading my product list.html template. What am I missing in the code above?

Drew McLellan

Drew McLellan 2638 points
Perch Support

Can you give us a sense of what your URLs look like? What pattern are you using to match the category paths? Does it account for multi-segmented paths?

Hi Just so you know, to better understand Runway and Shop I have downloaded and updated your nest run demo.
My links are like so
https://localhost/products/memberships/ This one shows content
https://localhost/products/memberships/passes/ This one shows no content
my routes are
category/[slug:s]
products/[slug:s]
products/[slug:cat2]/[slug:s]
products/[slug:cat3]/[slug:cat2]/[slug:s]
I have it set so it goes to products after the category listing page as that is the route from the set.

Drew McLellan

Drew McLellan 2638 points
Perch Support

In the case of

products/[slug:cat2]/[slug:s]

are you piecing the path back together using cat2 and s?

In my routes, yes

Drew McLellan

Drew McLellan 2638 points
Perch Support

How about in your page? How are you reading those in?

It's like I showed above. I'm wondering if i'm missing a concept here.
I'm trying to make it so my client can create a new sub category or sub sub category or go as deep as they like and when they do the link will take them to the page. I am able to show these categories and make them links with paths. But is the reason nothing shows when I go as deep as category/membership/passes because a new sub category page for passes should be created by the client as well or can this be dynamically created with just one layout. I'm obviously trying for the latter but maybe thats where i'm going wrong.

Drew McLellan

Drew McLellan 2638 points
Perch Support

I don't see you doing that above, which is why I'm asking.

I think you would be better to use a multi-segment token in your route so that you don't need to worry about how many levels of category you're dealing with.

Add a custom token (https://docs.grabaperch.com/runway/routing/custom-tokens/)

'path' => '[a-z0-9\-%\+/]+',

then use

products/[path:catpath]

and

perch_shop_products([
        'template' => 'products/list.html',
                'category' => 'products/'.perch_get('catpath'),
      ]);

I've done that, the links are still working but no products show. Here is my debug, hope it helps.

Debug Message
[23] SELECT p.pagePath, pr.routePattern, pr.routeRegExp, p.pageTemplate FROM perch2_pages p LEFT JOIN perch2_page_routes pr ON p.pageID=pr.pageID ORDER BY pr.routeOrder ASC, p.pagePath ASC
Matched route: products/[path:catpath]
Using master page: /templates/pages/shop/product.php
Page arguments:
Array
(
    [0] => /products/memberships/passes
    [catpath] => memberships/passes
    [1] => memberships/passes
)
[1] SELECT * FROM perch2_pages WHERE pagePath='products' LIMIT 1
[1] SELECT * FROM perch2_shop_cart WHERE cartID=9
[1] SELECT * FROM perch2_shop_cart WHERE cartID=9
[3] SELECT regionKey, regionHTML FROM perch2_content_regions WHERE regionPage='products' OR regionPage='*' ORDER BY regionPage DESC
[1] SELECT regionID, regionTemplate, regionPage, regionRev AS rev FROM perch2_content_regions WHERE regionKey='Site name' AND (regionPage='products' OR regionPage='*')
[1] SELECT * FROM ( SELECT idx.itemID, c.regionID, idx.pageID, c.itemJSON, idx2.indexValue as sortval FROM perch2_content_index idx JOIN perch2_content_items c ON idx.itemID=c.itemID AND idx.itemRev=c.itemRev AND idx.regionID=c.regionID JOIN perch2_content_index idx2 ON idx.itemID=idx2.itemID AND idx.itemRev=idx2.itemRev AND idx2.indexKey='_order' WHERE ((idx.regionID=6 AND idx.itemRev=1)) AND idx.itemID=idx2.itemID AND idx.itemRev=idx2.itemRev ) as tbl GROUP BY itemID, pageID, itemJSON, sortval ORDER BY sortval ASC
[1] Using template: /templates/content/home/site_name.html
Using template: /templates/pages/attributes/default.html
Using sub-template: /templates/pages/attributes/seo.html
[1] SELECT groupID FROM perch2_navigation WHERE groupSlug='members' LIMIT 1
[3] SELECT np.pageID, np.pageParentID, p.pagePath, p.pageTitle, p.pageNavText, p.pageNew, p.pageOrder, np.pageDepth, p.pageSortPath, np.pageTreePosition, p.pageAccessTags, p.pageAttributes FROM perch2_navigation_pages np, perch2_pages p WHERE p.pageID=np.pageID AND np.groupID=1 AND p.pageNew=0 AND np.pageDepth >=0 AND np.pageDepth<=1 ORDER BY np.pageTreePosition ASC
[0] SELECT np.pageTreePosition FROM perch2_pages p, perch2_navigation_pages np WHERE np.pageID=p.pageID AND np.groupID=1 AND p.pagePath='products' LIMIT 1
[2] Using template: /templates/navigation/members-item.html
[7] SELECT * FROM perch2_pages WHERE pageNew=0 AND pageHidden=0 AND pageDepth >=0 AND pageDepth<=1 ORDER BY pageTreePosition ASC
[1] SELECT pageTreePosition FROM perch2_pages WHERE pagePath='products' LIMIT 1
[1] SELECT pageID FROM perch2_pages WHERE pageTreePosition IN ('000-013', '000') ORDER BY pageTreePosition DESC
[7] Using template: /templates/navigation/item.html
[12] SELECT DISTINCT idx.itemID FROM perch2_shop_index idx JOIN perch2_shop_products main ON idx.itemID=main.productID AND idx.itemKey='productID' AND ((idx.indexKey='status' AND idx.indexValue='1')) GROUP BY idx.itemID HAVING COUNT(idx.itemID)=1
[4] SELECT DISTINCT idx.itemID FROM perch2_shop_index idx JOIN perch2_shop_products main ON idx.itemID=main.productID AND idx.itemKey='productID' AND ((idx.indexKey='parentID' AND idx.indexValue='')) GROUP BY idx.itemID HAVING COUNT(idx.itemID)=1
[nil] SELECT tbl.* FROM ( SELECT idx.itemID, main.*, idx2.indexValue as sortval FROM perch2_shop_index idx JOIN perch2_shop_products main ON idx.itemID=main.productID AND idx.itemKey='productID' JOIN perch2_shop_index idx2 ON idx.itemID=idx2.itemID AND idx.itemKey='productID' AND idx2.indexKey='_id' AND idx.itemID IN ('1', '2', '23', '25', '26', '27', '29', '3', '34', '42', '43', '44') AND idx.itemID IN ('1', '42', '43', '44') WHERE 1=1 AND ((idx.indexKey='slug' AND idx.indexValue='')) AND idx.itemID=idx2.itemID AND idx.itemKey=idx2.itemKey ) as tbl WHERE (productDeleted IS NULL) GROUP BY itemID, sortval ORDER BY sortval ASC
[56] SELECT DISTINCT settingID, settingValue FROM perch2_settings WHERE userID=0
Using template: /templates/shop/products/list.html
[2] SELECT DISTINCT e.* FROM perch2_events e LIMIT 2
[2] Using template: /templates/events/sidebar.html
[1] SELECT c.* FROM perch2_events_categories c, perch2_events_to_categories e2c WHERE c.categoryID=e2c.categoryID AND e2c.eventID='1'
[1] SELECT c.* FROM perch2_events_categories c, perch2_events_to_categories e2c WHERE c.categoryID=e2c.categoryID AND e2c.eventID='2'
[7] SELECT * FROM perch2_pages WHERE pageNew=0 AND pageHidden=0 AND pageDepth >=0 AND pageDepth<=1 ORDER BY pageTreePosition ASC
[1] SELECT pageTreePosition FROM perch2_pages WHERE pagePath='products' LIMIT 1
[1] SELECT pageID FROM perch2_pages WHERE pageTreePosition IN ('000-013', '000') ORDER BY pageTreePosition DESC
[7] Using template: /templates/navigation/item.html
Request time: 0.1577
Process time: 0.1561
Memory: 4.2834
Drew McLellan

Drew McLellan 2638 points
Perch Support

Ok, that looks promising. Can you show us the full code and you now have it?

Fixed - I still had perch_get('s') on my if statement but this is it for anyone else trying.

if(perch_get('catpath')) {
      perch_shop_products([
                'template' => 'products/list.html',
        'category' => 'products/'.perch_get('catpath'),
      ]);


    }else{

    perch_shop_product(perch_get('product'), [
    'template' => 'products/list.html',
  ]);
}

Thanks Drew I will be able to sleep now hopefully )

Still up. Ok to complete the original topic. Category name in page title..
I have this working fine with catpath but I want to grab the final part of the path, e.g. if it is more than one deep as in membership/passes I want to display just ' passes '.

perch_layout('global.above', array(
            'section_heading' => 'shop',
            'title' => perch_get('catpath'),
        ));

can I just filter this or do I need a template?

Drew McLellan

Drew McLellan 2638 points
Perch Support

Do you need the title of the category or its slug?

Title of the category.
this is what I now have

    $title = perch_categories([
                        'set' => 'products',
                        'template' => 'categorytitle.html',
                        'filter' => 'catPath',
                        'match' => 'eq',
                        'value' => perch_get('s')
              ], true);
                    perch_layout('global.above', array(
                         'section_heading' => 'shop',
                         'title' => $title
                     ));

as before pretty much
also my template category title.html has just this

<perch:category id="catTitle" />
Drew McLellan

Drew McLellan 2638 points
Perch Support

I think you'll want to change

'value' => perch_get('s')

to

'value' => 'products/'.perch_get('catpath')

Finally I have a working solution.

    $title = perch_category('products/' . perch_get('catpath'), [
            'template' => 'categorytitle.html'
                ], true);
         perch_layout('global.above', array(
                'section_heading' => 'shop',
                'title' => $title
            ));

I was using perch categories instead of category and my code was the wrong way around. It would only pull in titles when I took off the

'value' => 'products/'.perch_get('catpath')

Phew, got to get this working with individual product display now.
Brilliant thank you for your patience )