Forums / General Questions / Check if the user is logged in?

Check if the user is logged in?

Locked

Views: 13,150

Results 1 to 11 of 11
This thread is locked. New replies are disabled.
27 Jun 2006, 4:07 AM
#1
rowan avatar

rowan

Zen Follower

Join Date:
May 2006
Location:
Australia
Posts:
107
Plugin Contributions:
2

Check if the user is logged in?

Hey, is there some sample code I can use on the product listing and info pages to check whether a user is logged in or not?

Basically, I'd like something that acts like this:

if logged_in == true {
$button_buy = ('<a href="cart">Purchase</a>');
} else {
$button_buy = ('<a href="login">Register to Purchase</a>');
}
$echo $button_buy;

Is there some predefined code I can use or something? If not, would someone care to provide an example of how to do this?

27 Jun 2006, 4:43 AM
#2
ajeh avatar

ajeh

Oba-san

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

Re: Check if the user is logged in?

There is a simple switch in the Admin to do this in the Customer Details ...

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!

27 Jun 2006, 5:10 AM
#3
rowan avatar

rowan

Zen Follower

Join Date:
May 2006
Location:
Australia
Posts:
107
Plugin Contributions:
2

Re: Check if the user is logged in?

The most appropriate option there is 'Showroom only', however it prevents guests as well as customers from purchasing. I would like guests to be able to browse, but not to purchase, this option doesn't seem to exist.

So, what's another way to do this?

27 Jun 2006, 6:48 AM
#4
rowan avatar

rowan

Zen Follower

Join Date:
May 2006
Location:
Australia
Posts:
107
Plugin Contributions:
2

Re: Check if the user is logged in?

Nevermind I've worked it out, tell me if there's a better way though.

I borrowed this line from the checkout page ("includes/modules/pages/checkout_shipping/header_php.php"):
if (!$_SESSION['customer_id']) {

then I put in the "please login button" in the first if statement, and in the else statement I left the normal "purchase" button. I also took the following code and put it near the start of "includes/modules/pages/shopping_cart/header_php.php":
// if the customer is not logged on, redirect them to the login page
if (!$_SESSION['customer_id']) {
$_SESSION['navigation']->set_snapshot();
zen_redirect(zen_href_link(FILENAME_LOGIN, '', 'SSL'));
}

Works like a charm.

27 Jun 2006, 9:04 PM
#5
bunyip avatar

bunyip

Totally Zenned

Join Date:
Sep 2004
Location:
Western Massachusetts
Posts:
2,361
Plugin Contributions:
1

Re: Check if the user is logged in?

Rowan:

The most appropriate option there is 'Showroom only', however it prevents guests as well as customers from purchasing. I would like guests to be able to browse, but not to purchase, this option doesn't seem to exist.

So, what's another way to do this?

That switch you refer to is at Configuration > My Store > Store Status...if you'd checked under Configuration > Customer Details as Linda suggested, you/d have found a setting for Customer Shop Status - View Shop and Prices. Set that to 1, and you'd have the exact result you were looking for without any code changes.

Neville
An assumption is what you arrive at when you get tired of thinking...

27 Jun 2006, 11:02 PM
#6
ajeh avatar

ajeh

Oba-san

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

Re: Check if the user is logged in?

I'd really use the built in switches for this ... it controls all add to cart and links etc throughout the whole site with just the touch of the settings in the Customers Details ...

Why re-write code that is already built into Zen Cart and has been there for years? :lookaroun

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!

28 Jun 2006, 2:05 AM
#7
rowan avatar

rowan

Zen Follower

Join Date:
May 2006
Location:
Australia
Posts:
107
Plugin Contributions:
2

Re: Check if the user is logged in?

If I set Store Status to 2, the login/register link disapears from the top navigation and puts a "Contact us" link in place of the buy button.

If I set Customer Shop Status to 1, everything is disabled except for the login page, which is definately not what I want.

Does showcase not completely prevent people from purchasing products? :dontgetit

Guests = browse: yes, purchase: no

Customers = browse: yes, purchase: yes

28 Jun 2006, 5:32 AM
#8
bunyip avatar

bunyip

Totally Zenned

Join Date:
Sep 2004
Location:
Western Massachusetts
Posts:
2,361
Plugin Contributions:
1

Re: Check if the user is logged in?

Don't try to do it via Store Status....do it with Customer Shop Status

Admin > Configuration > Customer Details > Customer Shop Status - View Shop and Prices

Customer must be approved to shop
0= Not required
1= Must login to browse
2= May browse but no prices unless logged in
3= Showroom Only

Neville
An assumption is what you arrive at when you get tired of thinking...

28 Jun 2006, 5:49 AM
#9
rowan avatar

rowan

Zen Follower

Join Date:
May 2006
Location:
Australia
Posts:
107
Plugin Contributions:
2

Re: Check if the user is logged in?

bunyip:

Don't try to do it via Store Status....do it with Customer Shop Status

Admin > Configuration > Customer Details > Customer Shop Status - View Shop and Prices

Customer must be approved to shop
0= Not required
1= Must login to browse
2= May browse but no prices unless logged in
3= Showroom Only

None of those do what I want.

I want this:
2.5 = May browse and view prices but can't purchase unless logged in.

Rowan:

Guests = browse: yes, purchase: no

Customers = browse: yes, purchase: yes

28 Jun 2006, 6:20 AM
#10
bunyip avatar

bunyip

Totally Zenned

Join Date:
Sep 2004
Location:
Western Massachusetts
Posts:
2,361
Plugin Contributions:
1

Re: Check if the user is logged in?

here's what to do to achieve your 2.5 setting:

Admin > Configuration > Store Status ... set to 0

Admin > Configuration > Customer Details > Customer Shop Status - View Shop and Prices ... set to 0

Admin > Configuration > Customer Details > Customer Approval Status - Authorization Pending ... set to 3

Edit includes/modules/create_account.php
change this section of code (line 286 in v1.3.0.2)php $sql_data_array = array('customers_firstname' => $firstname, 'customers_lastname' => $lastname, 'customers_email_address' => $email_address, 'customers_nick' => $nick, 'customers_telephone' => $telephone, 'customers_fax' => $fax, 'customers_newsletter' => $newsletter, 'customers_email_format' => $email_format, 'customers_default_address_id' => '0', 'customers_password' => zen_encrypt_password($password), 'customers_authorization' => CUSTOMERS_APPROVAL_AUTHORIZATION ); to this (only change is to the last constant): ```php
$sql_data_array = array('customers_firstname' => $firstname,
'customers_lastname' => $lastname,
'customers_email_address' => $email_address,
'customers_nick' => $nick,
'customers_telephone' => $telephone,
'customers_fax' => $fax,
'customers_newsletter' => $newsletter,
'customers_email_format' => $email_format,
'customers_default_address_id' => '0',
'customers_password' => zen_encrypt_password($password),
'customers_authorization' => CUSTOMERS_APPROVAL
);


Save the edited file as includes/modules/your_template/create_account.php (it will over-ride the default file).

Now customers will see prices, but not have add-to-cart buttons unless logged in.

Edit: oh, and the reason for changing the create_account module? if it's not changed you will have to manually authorize every customer. This way they are automatically authorized when the account is created but the store functions as you want it to.

Neville
An assumption is what you arrive at when you get tired of thinking...

28 Jun 2006, 6:30 AM
#11
rowan avatar

rowan

Zen Follower

Join Date:
May 2006
Location:
Australia
Posts:
107
Plugin Contributions:
2

Re: Check if the user is logged in?

Thanks bunyip, that will do nicely. :yes: