Post by tom327 » Thu Apr 21, 2016 1:08 pm

Working with OpenCart 2.2

I want to hide the Add to Cart Button on Product pages when visitor is NOT logged in.

I researched this.
I tried this:

Code: Select all

    <?php if ($this->customer->isLogged()) { ?><a id="button-cart" class="button"><span><?php echo $button_cart; ?></span></a><?php } ?>
But it gave me two errors - one about undefined variable, and another about the function.

Then I tried:

Code: Select all

            <?php if ($logged) { ?>
			
			<a id="button-cart" class="button"><span><?php echo $button_cart; ?></span></a>
			
            <?php } ?>

While also adding the following to the Product page Controller:

Code: Select all

$data['logged'] = $this->customer->isLogged();
But, this also did not work, and I got an error - undefined variable.

Is there a way to make this work? To hide the Add to Cart button, when someone is NOT logged in?

Active Member

Posts

Joined
Thu Feb 16, 2012 7:33 am

Post by ostechnologies » Thu Apr 21, 2016 1:55 pm


Opencart Expert | sales[at]ost.agency
Skype - manish.osuniverse | Gtalk - manishmt

Extensions for Opencart @ https://www.ost.agency/product/product- ... extensions
ost.agency - ecommerce website design, development and digital company


User avatar
Active Member

Posts

Joined
Mon Apr 06, 2015 1:30 pm

Post by tom327 » Thu Apr 21, 2016 3:23 pm

Thanks for the recommendation. That looks like it will do everything I need.
But, it does not say it works with OC 2.2. I will send question to developer.

Have you used this extension? What do yo think of it?

Active Member

Posts

Joined
Thu Feb 16, 2012 7:33 am

Post by tom327 » Fri Apr 22, 2016 11:50 am

I tried the method here:

http://forum.opencart.com/viewtopic.php ... 7&p=222942

But it no longer works with OC 2.2 and has errors.

Can anyone tell me how to get this to work with Opencart 2.2 Product pages.

I just want to hide the Add to Cart, and only show it for visitors who are logged in.

I tried adding this to Product controller:
$data['logged'] = $this->customer->isLogged();

and then using this variable $logged on Product view:

<?php if ($logged) { ?> <button type="button" id="button-cart" data-loading-text="<?php echo $text_loading; ?>" class="btn btn-primary btn-lg btn-block"><?php echo $button_cart; ?> </button><?php } ?>

But I get errors about the variable being undefined?

I'm guessing more needs to be done in Product controller to get this to work? But, I'm surprised the state of being logged in is not some global variable I can check?

Active Member

Posts

Joined
Thu Feb 16, 2012 7:33 am

Post by artcore » Fri Apr 22, 2016 2:06 pm

It should work if you added this $data['logged'] to the index method. Also try refreshing the modification cache.

Attn: I no longer provide OpenCart extensions, nor future support - this includes forum posts.
Reason: OpenCart version 3+ ;D

Thanks!


User avatar
Active Member

Posts

Joined
Tue Jul 09, 2013 4:13 am
Location - The Netherlands

Post by tom327 » Sat Apr 23, 2016 3:37 am

I placed this into index method of Product Controller:

Code: Select all

$data['logged'] = $this->customer->isLogged();
And in product.tpl I placed:

Code: Select all

	      <?php if ($logged) { ?>
		  <h1>TEST</h1>
		  <?php } ?>
And still getting error:
Notice: Undefined variable: logged in D:\_\data\localweb\mytheme\catalog\view\theme\hstheme\template\product\product.tpl on line 25


Does it matter where I add

Code: Select all

$data['logged'] = $this->customer->isLogged();
in the product controller? Maybe additional modification is necessary?

Active Member

Posts

Joined
Thu Feb 16, 2012 7:33 am

Post by artcore » Sat Apr 23, 2016 5:21 am

Did you refresh the modification cache?
To rule out possible issues...
Your theme is not active? Try it with the default theme.
Do a var_dump in the controller before trying the tpl. var_dump($this->customer->isLogged());
Make sure it's not inside a condition, somewhere before the render is good just to be sure.
Try a $data['logged']=false;

Hopefully this helps ;D

Attn: I no longer provide OpenCart extensions, nor future support - this includes forum posts.
Reason: OpenCart version 3+ ;D

Thanks!


User avatar
Active Member

Posts

Joined
Tue Jul 09, 2013 4:13 am
Location - The Netherlands

Post by tom327 » Sat Apr 23, 2016 1:47 pm

Thanks artcore - you are awesome.

Based on your advice I got it to work.

It turned out, like you said,that I needed to use the Refresh button (in Admin Modifications) to have the cart use the updated Product Controller.

At first I did not understand why, because when I looked in the modification folder and the view folder in there was only header.tpl in the cache. BUT, what I did not realize at first is that the cache also has controller files. And it had a cache copy of the my product controller. Which is why the refresh was needed.

It's my first time using OC 2.0+ and this modification cache and refresh stuff is new to me. Thanks for your advice on how to solve the problem.

In terms of the code I put the following:

Code: Select all

$data['logged'] = $this->customer->isLogged();
in the product controller, in between the lines shown:

Code: Select all

 				$data['breadcrumbs'][] = array(
					'text' => $category_info['name'],
					'href' => $this->url->link('product/category', 'path=' . $this->request->get['path'] . $url)
				);
			}
		}

		$data['logged'] = $this->customer->isLogged();
		
		$this->load->model('catalog/manufacturer');



Then in my product templeate file I added this to test out how it was working:

Code: Select all

<?php if ($logged) { ?>
        <h3>You are Logged In</h3>
        <?php } else { ?>
       <h3>You are NOT Logged In</h3>
        <?php } ?>

And then of course, went into Admin, and used Refresh button (Extensions > Modifications page)

Thanks again!
and I think others will find this useful for this type of modification with OC 2.0+
Last edited by tom327 on Wed Apr 27, 2016 5:57 am, edited 1 time in total.

Active Member

Posts

Joined
Thu Feb 16, 2012 7:33 am

Post by artcore » Sat Apr 23, 2016 3:22 pm

Awesome!
I went through the same process so this one is easy to miss. It's a good thing everything is well documented ;D
I usually start out by creating an .ocmod.xml in the system folder with a commented line, hit refresh and start working on the modified file in the modification folder. No need for refreshes anymore...
My core folders/files literally never get touched :)

Attn: I no longer provide OpenCart extensions, nor future support - this includes forum posts.
Reason: OpenCart version 3+ ;D

Thanks!


User avatar
Active Member

Posts

Joined
Tue Jul 09, 2013 4:13 am
Location - The Netherlands

Post by winterglow » Wed Oct 05, 2016 9:36 am

This is just what I needed. Awesome!
I started with the modification file and it worked first time.
:D O0 :D

Newbie

Posts

Joined
Mon Jul 25, 2016 8:10 am
Who is online

Users browsing this forum: No registered users and 8 guests