View Single Post
Old 08-12-2004, 09:33 AM
instinctual instinctual is offline

Join Date: Nov 2003
Posts: 247

Default common if/then modifications I make to x-cart...

Hi all, thought I'd post a short list of if/then modifications I usually make to x-cart out of the box just so others can benefit from it as well...


1. In skin1/ either rectangle_top.tpl or head.tpl, depending on where you put your store's "header" html code, I usually put this: {if $usertype eq "C"} at the very beginning of my html code. I then put the corresponding {/if} at the end of my "header" html code, right after the last line. What does this do? It takes away your site design from the backend interface, so that you are only looking at the navigation links and x-cart interface. Do this to whichever "footer" file you are using as well - either rectangle_bottom.tpl or bottom.tpl under skin1/. This works in ANY version of x-cart.


2. In similar fashion, there are some things that I want my store admins to see, and there are some things I want to see as the developer logging in with the username "master". Some of the menu items in the right column I like to have for "master" only, and some for the other admins I create. So...that being said, following the logic in #1 - here's an if/then I usually surround items with in the admin menu files like skin1/admin/menu_admin.tpl and skin1/provider/menu.tpl : {if $login eq "master"} {/if} - you can put that around any item you ONLY want visible to the user "master". This works in ANY version of x-cart.


3. Sometimes I don't want my "store admins" to be able to create new administrators - - - sounds easy to do with the logic above, but you don't want to just take out the link "users" because that disables the ability for them to administrate their customers as well. So this fix makes it so that the "manage administrators" link dissapears while keeping the "manage customers" link intact. This is a tricky fix... I'm running out the door but will post this fix and more later tonight.


4. Ugh, onto #4, will dig up #3 again in a bit So..another common one I use is to have different "buy now" buttons for different categories of products. In this case, I am usually working with either products.tpl or products_t.tpl - depending on your site layout. In example, here is a recent if/then I used in products.tpl that will display a different "buy now" button depending on whether or not the category is #5 -

{if $usertype eq "C" and $config.Appearance.buynow_button_enabled eq "Y" and $products[product].categoryid ne "5"} {include file="customer/main/buy_now.tpl" product=$products[product]} {else} {include file="customer/main/member_buy_now.tpl" product=$products[product]} {/if}

Notice the "and $products[product].categoryid ne "5" part - - this is where you can change the categoryid number you're trying to affect. So above, I'm saying if the category # is not equal to "5" - then display the normal include - if not - and the category DOES equal five, display the special include. Of course, you'll have to copy "buy_now.tpl", make your own, and call it in the tag above. This works in ANY version of x-cart.


5. Ah, but somebody says, what about if I want to do this same thing on the "product.tpl" template? Same tag doesn't work, you may have noticed. Just use this syntax instead:

{if $product.categoryid == 4 or $product.categoryid == 5}{/if}

This works in ANY version of x-cart.


6. Ok, so here is some if/then mods I have made to the skin1/main/register_personal_info.tpl and other "register_" tpl files in that directory. I have mainly used these when I needed to add a custom field to the xcart_customers table and then have it show up in the registration process for the customer as well as the admin.

There are other places in this forum that discuss how to go about adding fields to the registration process, so I won't go in-depth there - but once you've added the fields and you are wanting to make a <select> box or something similar for the answer choices, these if/then's help.

In this next example, I have added a field called "region" to my customer table, and would like to display it, along with the customers' answer, in the profile section.

<tr valign=middle> <td align=right>Your Area</td> <td></td> <td nowrap> <select name=area {if $usertype eq "C"}disabled{/if}> <option value=""> <option value="area1" {if $userinfo.area eq "area1"}selected{/if}>area1 <option value="area2" {if $userinfo.area eq "area1"}selected{/if}>area2 <option value="area3" {if $userinfo.area eq "area3"}selected{/if}>area3 <option value="area4" {if $userinfo.area eq "area4"}selected{/if}>area4 <option value="area5" {if $userinfo.area eq "area5"}selected{/if}>area5 </select> </td> </tr>

Notice that I have several if/then's going on here - I've "disabled" some of the select boxes if you are a customer, and also provided statements that will correctly display your chosen answer if you have already filled out your profile. Pretty straightforward stuff - - you just put $userinfo. before the name of your new field you added to the customers table.


7. Ok, so this is also elsewhere on the forums, but have you ever tried to have an "on" state to a button when you were on that particular static page in your x-cart store? So when you're on the "about us" page your About Us graphic is the "over state" of the graphic?

You need a simple if/then to do it:

{if $smarty.get.pageid eq "22"}[img]button.gif[/img]{/if}

This will obviously show the image if the static page id is "22". I have used this successfully in bottom.tpl, rectangle_bottom.tpl, rectangle_top.tpl, and head.tpl, but I'm sure it would work in others as well.


8. Similar to the above - what about if you want to show something specific to the category you're on? Well, similar situation:

{if $cat eq "9"}show this{/if}

So above if the category equals "9" - it will show the "show this" text. Don't know what category # your categories are? Simply click on a category via the frontend in your browser, and in the url string you will see the category # as x-cart has assigned it. I have only successfully used this tag with THIS syntax on the products.tpl template - check above for other syntax that works on other templates.


9. Ah, one more before I hit the bed - ever wanted to display something based on whether a customer was not only logged in, but whether or not they had a "membership level" as well?

The "if logged in" part is easy:

{if $login ne ""} display this {/if}

The above tag will display the "display this" text if a person has logged in. If they have not, it will display nothing.

Now, determining whether they have a membership level is a different tag:

{if $user_membership ne ""} display this {/if}

The above tag will only display the text if a membership level is detected by x-cart. Useful if you have "member-only" pages or messages. I have used this in the past to surround pricing tags in product.tpl and products.tpl, as well as for displaying navigation tabs, etc.

It is also useful for displaying a message such as "become a member for extra savings, click here" if you want non-members to feel like they should check out becoming a member of your store when they are looking at products.

Another useful place for this tag is on your static pages - many people have complained of not having "content protection" on x-cart - well, this is how you do it. Create your static page, make sure you have "parse smarty tags in static pages" checked in your general setting for static pages, and then at the top of your html where your content begins, simply put that tag in there surrounding your "real" content, and then an {else} for your message to people who don't have the membership.

Something like this:

{if $user_membership ne "Level 1"} I'm sorry, you don't have a membership, so this content is not accessible. Please click here to become a member and get the goodies. {else} Your real html content {/if}


10. Lol, here's a fun one for a special situation - we had a client that wanted a certain payment method available to just "some" people within a membership level of xcart. As you know xcart doesn't let you specify this "out of the box" per user so we came up with a handy dandy if/then for the checkout.tpl file down where the {section} lists out the payment methods for display. Now obviously this isn't gonna work if you have a huge list of people - this was for a handful, so keep that in mind ;o) - check it out:

{section name=payment loop=$payment_methods} {if $payment_methods[payment].payment_method eq 'Special Payment Method' and $login ne 'samsmith' and $login ne 'poopybrown' and $login ne 'classclown'}<!---{/if} <TR> <TD width="1"><INPUT type="radio" name="paymentid" value="{$payment_methods[payment].paymentid}"{if $payment_methods[payment].is_default eq "0"} checked{/if}></TD> <TD nowrap>{$payment_methods[payment].payment_method}</TD> <TD>{$payment_methods[payment].payment_details}</TD> </TR>{if $payment_methods[payment].payment_method eq 'Special Payment Method' and $login ne 'samsmith' and $login ne 'poopybrown' and $login ne 'classclown'}--->{/if} {/section}

That lil' segment there only shows the payment method "Special Payment Method" if you are one of those users and are logged in, even though it's activated in xcart for the entire membership level.

Why did I use an html comment instead of a smarty comment to get rid of it? I couldn't get the if/then to work using a smarty comment - it always read the comment regardless of the if/then, leading me to believe it's not possible to write an if/then in order to print a smarty comment. ;o)


11. Ok, here's one where we had a special need. We needed customers to only be able to order certain products within one of our cateogires with a qty that WE specified, not from a dropdown, and not specifying it themselves in an open text box. We needed a product ordered in increments of 1000 - so you could order 1k, 2k, 3k, 4k, and 5k - and that's it.

Impossible you say...given that xcart controls the qty field, and the math of the cart depends upon that field. You can limit the min qty in the backend, but no way to modify the increments of qty that can be ordered per product.

So...we came up with this if/then on the product.tpl template that does the trick - this should replace the qty dropdown xcart has in there now, after replacing my "regular xcart qty <select> qty code" comment with the standard xcart <select> qty dropdown:

{if $product.productid == 10 || $product.productid == 12 || $product.productid == 17} <SELECT name="amount"{if $active_modules.Product_Options ne ''} onchange="check_wholesale(this.value);"{/if}> <option value="1000">1000</option> <option value="2000">2000</option> <option value="3000">3000</option> <option value="4000">4000</option> <option value="5000">5000</option> </select> {else} regular xcart qty <select> qty code. {* I would post it but mine is heavily modified and confusing *} {/if}

Again this is for a special situation, and as you can see we've done it by productid - so doing this for 100 products you would want to get fancy with your smarty tag and do a range of productid's.


Hope somebody finds some peace through this info, I'll keep adding on as I have time - thanks for the nudge!

***EDITED by Balinor to make Sticky *****
8,000 feet up in the Rocky Mountains of Colorado
X-Cart Versions 3.5.x - 4.0.x

IF you xcart, THEN you prosper, ELSE you fail. ELSEIF xcart fails, THEN you fix it, all the WHILE {loop}\'ing {section}\'s to feed your $smarty mind.
Reply With Quote