dongri1989 2013-05-25 03:25
浏览 24
已采纳

如何在OpenCart 1.5.5.1中使用PHP“if”和“else”添加新的自定义链接?

I'm editing a theme named AquaCart, installed in OpenCart 1.5.5.1. I also integrated Twitter Bootstrap's fixed top navbar. I already modified the navbar and put the Login / Logout menu there, and it's doing fine. This project is for my own online store.

I don't have any PHP skills yet, only HTML+CSS. I have managed to put the login/logout button on my new navbar menu by copying codes from my current theme's header.tpl file and editing the header.php file (from the catalog\english\common\header.php).

Now, I am polishing the menu and wanted to add some custom menu/link named Sign Up!. I want this Sign Up! menu link to show as Logout link when a user is already logged in.

My current edit shows the logged-in user's name in a <li>, and a logout menu in a second <li>. This is not what I really want. I want to show the Logout link in place of the Sign Up! link when a user is logged in.

Here is my current header.tpl edit:

<ul>
    <ul>
        <li>
            <a href="#">My Account</a><!--Shall be shown only when user is logged in-->
                <ul>
                    <?php if (!$logged) { ?>
                    <?php echo $text_welcome; ?>
                    <?php } else { ?>
                    <?php echo $text_logged; ?>
                    <?php } ?>
                    <li class="divider"></li>
                    <li><a href="#">Store Front</a></li>
                    <li><a href="#">Blog Page</a></li>
            </ul>
        </li>
        <li><a href="index.php?route=account/register">Sign Up!</a></li>
    </ul>
</ul>

My header.php from the catalog\language folder of opencart. text_logged and text_welcome was already edited.

<?php
    // header.php from catalog\language\english\common\header.php
    $_['text_home']           = 'Online Shop';
    $_['text_wishlist']       = 'Wish List (%s)';
    $_['text_shopping_cart']  = 'Shopping Cart';
    $_['text_search']         = 'Search';
    $_['text_welcome']        = '<li><a href="%s">Login</a></li>
                                 <li><a href="index.php?route=account/register">Sign Up!</a></li>';
    $_['text_logged']         = '<li><a href="%s">%s</a></li>
                                 <li><a href="%s">Logout</a></li>';
    $_['text_account']        = 'My Account';
    $_['text_checkout']       = 'Checkout';
?>

The above code is rendered like so...

..and I wanted to make the sign Up! link to become Logout when a user is logged in.

Enter image description here

I don't know PHP yet, but I'm struggling on studying the PHP files of OpenCart installation, and I've found this string from my header.php file found in catalog\controller\common:

$this->data['text_logged'] = sprintf($this->language->get('text_logged'), $this->url->link('account/account', '', 'SSL'), $this->customer->getFirstName(), $this->url->link('account/logout', '', 'SSL'));

I am thinking of duplicating that, but don't know what to duplicate and modify. And what are other files involved?


UPDATE:

I've now added a new Text name "Sign Up" and "Login", so I may not be confused, and I can easily substitute it to my Text Link.

So far.. I've added: In my catalog\english\common\header.php:

$_['text_login']  = 'Login';
$_['text_signup']  = 'Sign Up!';

And in my catalog\controller\header.php.

$this->data['text_login'] = $this->language->get('text_login');
$this->data['text_signup'] = $this->language->get('text_signup');

UPDATE 2:

I've re-marked up my Menu, based from shadyxx opinion, I've changed the menu a bit. So for "not Logged-in Users"... This menu should be echoed...

Enter image description here

Marked up like this:

<div class="nav-collapse collapse">
    <ul>
        <li><a href="#">Login</a></li>
        <li><a href="#">Register</a></li>
    </ul>
</div>

And for LOGGED-IN USERS, this menu should be echoed...

Enter image description here

Marked up like this:

<div>
    <ul>
        <ul>
            <li>
            <a href="#">Account></b></a>
            <ul>
                <li>%s</a></li>
                <li class="divider"></li>
                <li>Shopping Cart</a></li>
                <li>Checkout</a></li>
            </ul>
        </li>
        <li>Logout</a></li>
        </ul>
    </ul>
</div>

In my catalog\controller\common\header.php:

$this->data['signup'] = sprintf($this->language->get('text_signup'), $this->url->link('account/register', '', 'SSL'));
$this->data['login_register'] = sprintf($this->language->get('text_login_register'), $this->url->link('account/login', '', 'SSL'), $this->url->link('account/register', '', 'SSL'));
$this->data['logged_in'] = sprintf($this->language->get('text_logged_in'), $this->url->link('account/account', '', 'SSL'), $this->customer->getFirstName(), $this->data['text_shopping_cart'] = $this->language->get('text_shopping_cart'), $this->data['shopping_cart'] = $this->url->link('checkout/cart'), $this->url->link('account/logout', '', 'SSL'));

In my catalog\language\english\common\header.php

$_['text_login_register']
= '<div>
     <ul>
         <li>Login</a></li>
         <li>Register</a></li>
     </ul>
   </div>';

$_['text_logged_in']
= '<div>
       <ul>
           <ul>
               <li>
                   <a href="#">Account<b class="caret"></b></a>
                   <ul>
                       <li><a href="%s">%s</a></li>
                       <li class="divider"></li>
                       <li><a href="%s">Shopping Cart</a></li>
                       <li><a href="%s">Checkout</a></li>
                   </ul>
               </li>
               <li><a href="%s">Logout</a></li>
           </ul>
       </ul>
   </div>';

In my header.tpl

<?php if (!$logged) { ?>
<?php echo $login_register; ?>
<?php } else { ?>
<?php echo $logged_in; ?>
<?php } ?>

So, what's not working, using the above codes is the checkout menu. The checkout menu is redirecting a user to the Shopping Cart Page.

I've just copy pasted the above codes. I've tried to understand the use of the existing code and by trial and error.

  • 写回答

1条回答 默认 最新

  • douzi6060 2013-05-30 14:07
    关注

    IMHO, you do not want to have My Account link when there is no user logged in...as it would make no sense to be shown for the visitors (not-logged-in users).

    I would show only the Sign Up! link, and after logging in, I would show only the My Account menu that would have the desired Logout link (so as it is right now).

    So keep your current template as is, and using this code block (you have given us):

    <?php if (!$logged) { ?>
    <?php echo $text_welcome; ?>
    <?php } else { ?>
    <?php echo $text_logged; ?>
    <?php } ?>
    

    We can modify your top navbar slightly... The condition <?php if (!$logged) { ?> applies if there is no user logged in, so inside it we will show the Sign Up! link, then the <?php } else { ?> gets applied if there is a user logged in. Thus we will move the My Account menu there. This code is just an example (as you didn't provide us with the navbar source code...):

    <ul id="top-navbar>
        <?php if (!$logged) { /* only visitor */?>
        <li><a href="<?php echo $signup; ?>"><?php echo $text_signup;?></a></li>
        <?php } else { /* logged in user */?>
        <li><span>My Account</span>
            <ul class="sub-menu">
                <li><span><?php echo $text_logged; ?> <?php echo $logged_username; ?></span></li>
                <li><a href="<?php echo $logout; ?>><?php echo $text_logout; ?></a></li>
            </ul>
        </li>
        <?php } ?>
    </ul>
    

    In your controller you would have to push the language strings, URLs and username to the template and in language file you would have to add the text strings needed for your menu (if you hadn't done so far...).


    EDIT:

    Your thinking is good, but the approach not. Do not put HTML into language files, nor controllers, unless really necessary or no other way is possible (which may not occure and if yes, something is wrong).

    So, put all the HTML into your template file (header.tpl) and use PHP variables:

    <?php if (!$logged) { ?>
    <div>
        <ul>
            <li><a href="<?php echo $login; ?>"><?php echo $text_login; ?></a></li>
            <li><a href="<?php echo $signup; ?>"><?php echo $text_signup; ?></a></li>
        </ul>
    </div>
    <?php } else { ?>
    <div>
        <ul>
            <li>
                <a href="#"><?php echo $text_account; ?><b class="caret"></b></a>
                <ul>
                    <li><a href="<?php echo $account; ?>"><?php echo $username;?></a></li>
                    <li class="divider"></li>
                    <li><a href="<?php echo $shopping_cart;?>"><?php echo $text_shopping_cart; ?></a></li>
                    <li><a href="<?php echo $checkout; ?>"><?php echo $text_checkout; ?></a></li>
                </ul>
            </li>
            <li><a href="<?php echo $logout; ?>"><?php echo $text_logout; ?></a></li>
        </ul>
    </div>
    <?php } ?>
    

    Now many of the URLs and texts are already defined thus we need to add only few of them.

    Open your language file (catalog\language\english\common\header.php) and add/modify these:

    $_['text_login'] = 'Log In';
    $_['text_signup'] = 'Register';
    $_['text_logout'] = 'Log Out';
    

    You can remove the previous text_login_register and text_logged_in variables with its values.

    Now open the controller (catalog\controller\common\header.php) and find the part that ends with this line:

    $this->data['checkout'] = $this->url->link('checkout/checkout', '', 'SSL');
    

    And after it, add these lines:

    $this->data['text_login'] = $this->language->get('text_login');
    $this->data['text_signup'] = $this->language->get('text_signup');
    $this->data['text_logout'] = $this->language->get('text_logout');
    
    $this->data['login'] = $this->url->link('account/login', '', 'SSL');
    $this->data['signup'] = $this->url->link('account/register', '', 'SSL');
    $this->data['logout'] = $this->url->link('account/logout', '', 'SSL');
    
    $this->data['username'] = '';
    if($this->customer->isLogged()) {
        $this->data['username'] = $this->customer->getFirstName() . ' ' . $this->customer->getLastName();
    }
    

    Now you should be done.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀