Forums / General Questions / php code for "if logged in" vs "if not logged in"

php code for "if logged in" vs "if not logged in"

Locked

Views: 5,937

Results 1 to 12 of 12
This thread is locked. New replies are disabled.
15 Oct 2006, 2:44 AM
#1
p_m avatar

p_m

New Zenner

Join Date:
Sep 2006
Posts:
27
Plugin Contributions:
0

php code for "if logged in" vs "if not logged in"

the canada post shipping module doesn't work with the shipping estimator unless the customer is logged in. there's been a number of threads trying to fix this problem, but no luck.

as it is now, a not-logged-in customer who clicks the "estimate shipping" button on the shopping cart page on my site will get the little pop-up window to come up, but it won't work at all.

so i want to prevent not-logged-in customers from seeing the "estimate shipping" button in the first place.

in fact, what i'd like to do is put a different button or message in the same place, saying "please login to estimate shipping costs". that would link to the login page.

so what i need is a php command with the following logic:
(1) if login=true, then display "estimate shipping" button
(2) if login=false, then display "login for shipping" button

obviously i'll have to define BUTTON_IMAGE_LOGIN_FOR_SHIPPING_ESTIMATOR at some point. that's no problem. i just need help with how to code the if/else statement for logged in status.

the relevant code is in templates/tpl_shopping_cart_default.php :

<?php
    switch (true) {
      case (SHOW_SHIPPING_ESTIMATOR_BUTTON == '1'):
?>

<div class="forward"><?php echo '<a href="javascript:popupWindow(\'' . zen_href_link(FILENAME_POPUP_SHIPPING_ESTIMATOR) . '\')">' .
 zen_image_button(BUTTON_IMAGE_SHIPPING_ESTIMATOR, BUTTON_SHIPPING_ESTIMATOR_ALT) . '</a>'; ?></div>

<?php
      break;
      case (SHOW_SHIPPING_ESTIMATOR_BUTTON == '2'):
/**
 * load the shipping estimator code if needed
 */
?>
      <?php require(DIR_WS_MODULES . zen_get_module_directory('shipping_estimator.php')); ?>

the code is a bit more complicated than it would be because of the switch in admin-->configuration-->shipping which lets you decide whether to display a shipping estimator button (for the popup) or just the actual estimate on the same page. i'm planning to use a button, meaning

case (SHOW_SHIPPING_ESTIMATOR_BUTTON == '1'):

but i guess the proper way to code this would be to embed the switch (case 1 vs case 2) within the (if login vs if not-login) statement. that way logged in customers would see either the button or the shipping estimate, depending on how the switch is set, and not-logged in customers wouldn't see it either way.

hope that makes sense...

15 Oct 2006, 3:10 AM
#2
ajeh avatar

ajeh

Oba-san

Join Date:
Sep 2003
Location:
Ohio
Posts:
62,757
Plugin Contributions:
1

Re: php code for "if logged in" vs "if not logged in"

When this is defined: $_SESSION['customer_id']

Then the customer is logged in ...

Linda McGrath
If you have to think ... you haven't been zenned ...

**Did YOU buy the Zen Cart Team a cup of coffee and a donut today? Just click here to support the Zen Cart Team!!**

Are you using the latest? Perhaps you've a problem that's fixed in the latest version: [Upgrade today!]
Officially PayPal-Certified! Just click here

Try our Zen Cart Recommended Services - Hosting, Payment and more ...
Signup for our Announcements Forums to stay up to date on important changes and updates!

15 Oct 2006, 5:11 AM
#3
p_m avatar

p_m

New Zenner

Join Date:
Sep 2006
Posts:
27
Plugin Contributions:
0

Re: php code for "if logged in" vs "if not logged in"

hurrah! it works...

in templates/tpl_shopping_cart.php the code above now reads:

<?php if ($_SESSION['customer_id']) { ?>

<?php
    switch (true) {
      case (SHOW_SHIPPING_ESTIMATOR_BUTTON == '1'):
?>

<div class="forward"><?php echo '<a href="javascript:popupWindow(\'' . zen_href_link(FILENAME_POPUP_SHIPPING_ESTIMATOR) . '\')">' .
 zen_image_button(BUTTON_IMAGE_SHIPPING_ESTIMATOR, BUTTON_SHIPPING_ESTIMATOR_ALT) . '</a>'; ?></div>

<?php
      break;
      case (SHOW_SHIPPING_ESTIMATOR_BUTTON == '2'):
/**
 * load the shipping estimator code if needed
 */
?>
      <?php require(DIR_WS_MODULES . zen_get_module_directory('shipping_estimator.php')); ?>

<?php
        break;
      }
?>
<?php 
 } else {
 ?>
 <div class="forward"><a href="<?php echo zen_href_link(FILENAME_LOGIN, '', 'SSL'); ?>"><?php echo LOGIN_FOR_SHIPPING_ESTIMATOR; ?></a></div>
 
<?php
             }
?>

i decided to use text instead of a button, since the phrase is too long. so i added this line to english.php

define('LOGIN_FOR_SHIPPING_ESTIMATOR', 'Please login for shipping estimate');

and it works! thanks for the help.

13 Aug 2008, 7:48 AM
#4
saitomedia avatar

saitomedia

Zen Follower

Join Date:
Feb 2008
Location:
Southern California
Posts:
146
Plugin Contributions:
0

Re: php code for "if logged in" vs "if not logged in"

Dear Ajeh,

What is the php syntax for "if not logged in" ?

Thank you for your response in advance.

I tried <?php if ($messageStack->size('login') > 0) { ?> but it didn't work. :( I'm just trying anything I can find at the moment.

Is there such a thing without using "else"?

13 Aug 2008, 4:32 PM
#6
ajeh avatar

ajeh

Oba-san

Join Date:
Sep 2003
Location:
Ohio
Posts:
62,757
Plugin Contributions:
1

Re: php code for "if logged in" vs "if not logged in"

Same as what I posted above ... use the $_SESSION['customer_id'] to determin if the customer is logged in or not ...

For example, a simple IF can use:

<?php
  if ($_SESSION['customer_id']  > 0) {
// I am logged in
  } else {
// I am not logged in
  }
?>

Linda McGrath
If you have to think ... you haven't been zenned ...

**Did YOU buy the Zen Cart Team a cup of coffee and a donut today? Just click here to support the Zen Cart Team!!**

Are you using the latest? Perhaps you've a problem that's fixed in the latest version: [Upgrade today!]
Officially PayPal-Certified! Just click here

Try our Zen Cart Recommended Services - Hosting, Payment and more ...
Signup for our Announcements Forums to stay up to date on important changes and updates!

25 Aug 2008, 3:17 PM
#8
ajeh avatar

ajeh

Oba-san

Join Date:
Sep 2003
Location:
Ohio
Posts:
62,757
Plugin Contributions:
1

Re: php code for "if logged in" vs "if not logged in"

That is a very sharp looking ... :smile:

Too bad there isn't a nice link back to Zen Cart in the footer ... :cry:

Good luck with your site!

Linda McGrath
If you have to think ... you haven't been zenned ...

**Did YOU buy the Zen Cart Team a cup of coffee and a donut today? Just click here to support the Zen Cart Team!!**

Are you using the latest? Perhaps you've a problem that's fixed in the latest version: [Upgrade today!]
Officially PayPal-Certified! Just click here

Try our Zen Cart Recommended Services - Hosting, Payment and more ...
Signup for our Announcements Forums to stay up to date on important changes and updates!

26 Aug 2008, 3:21 AM
#10
ajeh avatar

ajeh

Oba-san

Join Date:
Sep 2003
Location:
Ohio
Posts:
62,757
Plugin Contributions:
1

Re: php code for "if logged in" vs "if not logged in"

Thanks for making Zen Cart look so nice ... and for letting the world know about it too! :cool:

Linda McGrath
If you have to think ... you haven't been zenned ...

**Did YOU buy the Zen Cart Team a cup of coffee and a donut today? Just click here to support the Zen Cart Team!!**

Are you using the latest? Perhaps you've a problem that's fixed in the latest version: [Upgrade today!]
Officially PayPal-Certified! Just click here

Try our Zen Cart Recommended Services - Hosting, Payment and more ...
Signup for our Announcements Forums to stay up to date on important changes and updates!