Jump to content

Group or arrange products, based on name or similiar on products.tpl.


-M-

Recommended Posts

We want to make a small change in regards to our products.tpl page for servers.

 

In it's current state it displays the items as entered in the WHMCS admin panel under the 'products' page.

...however we want the servers "grouped". The CSS/HTML side I can do myself, but I want to know how this is possible to achieve.

 

For example we want to group servers in the following categories: specials (aanbieding), raid (RAID) and non-raid (zonder RAID).

 

I think the best way to do this is, is by targeting the $product.name variable. Since this variable includes the terms 'aanbieding', 'RAID' and 'zonder RAID'. However when I try to target this like:

 

                   {if $product.name eq {'Aanbieding DPS'}}
		                      Example outcome
                   {/if}

 

Nothing happens. It only works when the full name of the $product.name is entered. For example: 'Aanbieding DPS - Intel hexa-core processor 3.66 Ghz. 128 GB RAM - 1000 GB HDD'

It only works for that.

 

There must be a way to target a partial string without entering the exact string in order to target these. Especially considering we have multiple special offers. Also for future usage it would be easier to use.

 

So in short; how do I target only the products which include the text 'Aanbieding DPS' in the $product.name?

And what solution is there in targeting the strings 'RAID' and 'zonder RAID' or should I change the wording a bit to make it easier to target?

 

Thank you in advance!

 

Regards

Link to comment
Share on other sites

in general, if you want to see if a variable contains a specific phrase (anywhere), you could use...

 

                    {if $product.name|strstr:'Aanbieding DPS'}
                                 Example outcome
                   {/if}

however, I suspect you might run into an issue with this technique with 'RAID' where it's mentioned in two separate groups, but the way around that might be to see if the variable starts with your phrase - under those circumstances, you may need to use...

 

                    {if $product.name|strpos:'RAID'===0}
                                 Example outcome
                   {/if}

and then with zonder RAID you can use either method to search for 'zonder'.

 

I assume that ultimately, you'll end up with an {if}{elseif} inside the {foreach} loop - if i've understood what you're trying to do correctly.

Edited by brian!
Link to comment
Share on other sites

Ah, I needed to use 'strstr' in between. I was already wondering why it didn't work.

 

Thanks (once) again Brian. Now I can fool around a bit more with it to get it displayed as I want it.

 

Sidenote; I hold my breath every time a new upgrade or patch is being released, because I need to go over all the changes (mainly in regards to the templates) by hand each line. :S

Link to comment
Share on other sites

Well Brian!, you were right about the RAID issue, as you thought.

 

Unfortunately the solution for the RAID doesn't work:

 

                   {if $product.name|strpos:'RAID'===0}
                                 Example outcome
                   {/if}  

 

I don't know PHP, but I looked up what "strpos" and "===0" meant, so I understand it now and I now also understand why it doesn't work.

 

Unlike "Aanbieding", it doesn't start at the front. But it's located at the end. Probably there is a workaround for this, however there is also one product which has RAID10 at the end. Sigh.

 

So in short, before there is more confusion, I have the following product names:

 

- 1 product Aanbieding (located in the front)

- 5 products with RAID (located at the end)

- 4 products with 'zonder RAID' (located at the end)

- 1 product with 'RAID10' (located at the end)

 

Is there an easy way to sort the RAID and RAID10?

 

If it's to hard / difficult to create a solution for this, I will just remove the RAID10 text from the product name. But I rather not.

 

(sorry for the confusion)

 

Regards

 

//edit

 

Maybe it's a better idea to target the full name. Though it's more work, but then I can target exactly the things correctly?

I presume that's easier than making a fix for this problem?

Edited by MvdL1979
Link to comment
Share on other sites

well you can use smarty to search the end of a string too - but thinking out of the box a little, there might be a simpler way to do this - i'll give you both. :)

 

one option would be to use strstr in all four statements - but add an extra space at the end of the 'RAID' group product names... visually, it won't make a difference, but it simplifies the searching.

 

    {if $product.name|strstr:'Aanbieding DPS'}Example outcome 1
   {elseif $product.name|strstr:'RAID '}Example outcome 2
   {elseif $product.name|strstr:'zonder'}Example outcome 3
   {elseif $product.name|strstr:'RAID10'}Example outcome 4
   {/if}

 

the other way, and possibly the correct way to do it, would be to use substr...

 

    {if $product.name|strstr:'Aanbieding DPS'}Example outcome 1
   {elseif $product.name|strstr:'zonder'}Example outcome 2
   {elseif $product.name|substr:-4 eq 'RAID'}Example outcome 3
   {elseif $product.name|substr:-6 eq 'RAID10'}Example outcome 4
   {/if}

the problem is 'zonder RAID' and 'RAID' - as both end in 'RAID' -... it might be solved by putting 'zonder RAID' before 'RAID' in your {if} statements order (as shown in the 2nd example).

 

Maybe it's a better idea to target the full name. Though it's more work, but then I can target exactly the things correctly?

I presume that's easier than making a fix for this problem?

I think if you can live with zonder being before the other 'RAID' groups, this should work.

Edited by brian!
Link to comment
Share on other sites

I appreciate the effort and the solutions brian! :)

 

Before you posted this, I tried it with something like the following (which I mentioned in the edit section):

 

                   {if $product.name|strstr:'1x Intel Xeon L5410 quad-core 2.33 Ghz. - 8 GB RAM - Zonder RAID' 
                    || $product.name|strstr:'1x Intel Xeon X3360 quad-core 2.83 Ghz. - 4 GB RAM - Zonder RAID'
                    || $product.name|strstr:'2x Intel Xeon L5420 quad-core 2.50 Ghz. - 8 GB RAM - Zonder RAID'
                   }

 

(this is just a partial example though)

 

This works as far as I can tell. Obviously it's more work when new offers, servers are added, but it works.

I will save this current .tpl and give yours a try, because the solution you provided is way cleaner then mine and more "future"-proof. :)

 

By the way, I don't know if it's useful information, but 'Zonder' means without. I could add 'Met' RAID to the others (which means 'includes').

 

Regards

Link to comment
Share on other sites

This works as far as I can tell. Obviously it's more work when new offers, servers are added, but it works.

it's probably worth knowing that I think there is a limit to the length of {if} statements... it may be something ridiculous like 100 conditions, so I doubt you'll get anywhere near it with this!

 

I will save this current .tpl and give yours a try, because the solution you provided is way cleaner then mine and more "future"-proof. :)

it occurred to me that there might be another way to do this.

 

1. create 4 arrays in Smarty - one for each server group - in each, add the pid of the products assigned to that group.

2. then you can use {if in_array} instead of searching for terms in the product name.

 

the advantage of this would be that you could modify product names without it interfering with the {if} statement in the template - the product IDs will always be the same.

 

if the substr method fails, then this would probably be a valid alternate solution and i'll explain how to do it... the idea of using a long list of {if} statements searching for strings (as per #6) should, in my opinion, be a last resort.

 

By the way, I don't know if it's useful information, but 'Zonder' means without. I could add 'Met' RAID to the others (which means 'includes').

I knew that - my sister-in-law is Dutch, so i've picked up the odd Dutch word over the years! :)

... but it doesn't help with the searching to add 'Met'... if you used '+RAID' and '-RAID' instead, then that would be useful! :lol:

Link to comment
Share on other sites

  • 1 month later...

Apparently I forgot to report back, sorry about that... :S

 

Anyways your coding works perfectly, but there is an issue when I add new servers. Since they aren't in the products.tpl they simply won't show up.

Therefor I am now trying to target servers which include (or not include) the following words.

 

Words which should be included are: 'Intel' and '- RAID'

Words which should not be included are: 'AMD', 'Zonder RAID', 'Aanbieding' and 'Special'

 

So I tried the following:

 

{if $product.name eq "Intel" AND $product.name eq "- RAID" AND $product.name neq "AMD" AND $product.name neq "Zonder RAID" AND $product.name neq "Aanbieding" AND $product.name neq "Special"}

 

and

 

{if $product.name eq "Intel" && $product.name eq "- RAID" && $product.name neq "AMD" && $product.name neq "Zonder RAID" && $product.name neq "Aanbieding" && $product.name neq "Special"}

 

But both tries it will output nothing. Probably I am using it incorrectly?

Is this even possible at all?

 

If I could make this work, I could apply this in the similar way for other server types and make this more future proof when products (servers) are added in general.

 

Thank you in advance.

Link to comment
Share on other sites

Hi,

 

Anyways your coding works perfectly, but there is an issue when I add new servers. Since they aren't in the products.tpl they simply won't show up.

when you say they aren't in products.tpl, you mean they are servers added to a different product group? products.tpl would only show the products in the active group, so if you are adding servers (products) to another group, Smarty won't be able to access them via existing arrays.

 

But both tries it will output nothing. Probably I am using it incorrectly?

the code will be failing for a couple or reasons...

 

1. product name will never equal "Intel" because 'Intel' is only part of the product name - so you'd have to use strstr to see if the product name contains the string (not equal to it).

2. using and and neq can cause a logic failure - at least confusion for me anyway.

3. if the new server products aren't in the array...

 

Is this even possible at all?

If I could make this work, I could apply this in the similar way for other server types and make this more future proof when products (servers) are added in general.

whether it's possible depends on if these new server products are available to products.tpl - or to put it another way, are they in the $products array ? if not, all the Smarty code in the world won't make this work.

 

any further info you can give about these new servers ?

Link to comment
Share on other sites

when you say they aren't in products.tpl, you mean they are servers added to a different product group? products.tpl would only show the products in the active group, so if you are adding servers (products) to another group, Smarty won't be able to access them via existing arrays.

 

Sorry for the confustion. The products do show up, but I want to sort the result.

 

 

the code will be failing for a couple or reasons...

 

1. product name will never equal "Intel" because 'Intel' is only part of the product name - so you'd have to use strstr to see if the product name contains the string (not equal to it).

2. using and and neq can cause a logic failure - at least confusion for me anyway.

3. if the new server products aren't in the array...

 

 

whether it's possible depends on if these new server products are available to products.tpl - or to put it another way, are they in the $products array ? if not, all the Smarty code in the world won't make this work.

 

any further info you can give about these new servers ?

 

I hope I can explain better. Currently I am using this to display servers with a RAID configuration:

 

                   {if $product.name|strstr:'1x Intel Xeon X3440 quad-core 2.53 Ghz. - 16 GB RAM - RAID' 
                    || $product.name|strstr:'1x Intel Xeon X5150 dual-core 2.66 Ghz. - 8 GB RAM - RAID'
                    || $product.name|strstr:'1x Intel Xeon X3363 quad-core 2.83 Ghz. - 8 GB RAM - RAID'
                    || $product.name|strstr:'2x Intel Xeon E5450 quad-core 3.00 Ghz. - 32 GB RAM - RAID'
                    || $product.name|strstr:'2x Intel Xeon X5550 quad-core 2.67 Ghz. - 16 GB RAM - RAID'
                    || $product.name|strstr:'1x Intel Xeon L5410 quad-core 2.33 Ghz. - 8 GB RAM - RAID'
                   }

 

I also have other servers sorted, for example without RAID and AMD servers.

 

However I want to make this future proof. If I now add a new server in the WHMCS admin panel, I also have to add that rule to the above code.

If I could sort them on specific word combinations, it would be easier for me and future proof. Because all new servers will always have these set of rules.

 

That's why I tried using:

 

{if $product.name eq "Intel" AND $product.name eq "- RAID" AND $product.name neq "AMD" AND $product.name neq "Zonder RAID" AND $product.name neq "Aanbieding" AND $product.name neq "Special"}

 

But apparently that's wrong. :)

 

So I want to sort the results based on the following:

 

The product name requires the following to be in there: "Intel" and "- RAID"

The product name requies the following not to be in there "AMD", "Zonder RAID", "Special" and "Aanbieding"

 

I hope this explanation helps things?

...sorry for the confusion, once again... :)

 

Regards

 

 

//update

 

I also just tried the following:

 

{if $product.name|strstr: 'Intel' AND $product.name|strstr: '- RAID'}

 

It will display several servers, including the ones I don't want to be displayed in that location.

So can I combine the above line of code with words which should not be in there, for example "Aanbieding", "Special" and "Zonder RAID"?

Edited by MvdL1979
Update...
Link to comment
Share on other sites

what happens with...

 

{if $product.name|strstr:'Intel' AND $product.name|strstr:'- RAID' AND !$product.name|strstr:'AMD' AND !$product.name|strstr:'Zonder RAID' AND !$product.name|strstr:'Aanbieding' AND !$product.name|strstr:'Special'}

that looks so wrong to me, but it's close to lunchtime so I might be mistaken! :?:

 

try it and if it fails, i'll take another look after i've eaten! :)

Link to comment
Share on other sites

what happens with...

 

{if $product.name|strstr:'Intel' AND $product.name|strstr:'- RAID' AND !$product.name|strstr:'AMD' AND !$product.name|strstr:'Zonder RAID' AND !$product.name|strstr:'Aanbieding' AND !$product.name|strstr:'Special'}

that looks so wrong to me, but it's close to lunchtime so I might be mistaken! :?:

 

try it and if it fails, i'll take another look after i've eaten! :)

 

It seems to work...! I removed "Special" to see if it worked (if the result got displayed) and it did. So your code is really working. :)

If the code works then all is good, right? :)

 

Anyways, have a good lunch! :)

Link to comment
Share on other sites

It seems to work...! I removed "Special" to see if it worked (if the result got displayed) and it did. So your code is really working. :)

sometimes, I even surprise myself! :idea:

 

If the code works then all is good, right? :)

yes - if the idea works for one, it should work for the others.

Link to comment
Share on other sites

I hope I can explain better. Currently I am using this to display servers with a RAID configuration:

 

I also have other servers sorted, for example without RAID and AMD servers.

You could be overcomplicating it - can you not just use the inbuilt 'sort order' value in whmcs and just number them (which you can change at any time) in the order you want ?

 

i.e. you put sort order to be

10101010 for server type 1

10101020 for server type 2

10101030 for server type 3

and then if you introduce a 2.5 version you just give it order 10101025

?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated