Post by smithhorn » Sun Mar 26, 2017 2:21 am

I posted this in the General Support but the post was deleted. If this isn't the right place, please point me in the right direction!

OpenCart 2.1.0.1, Journal 2 theme

How do I apply conditional css (using javascript or jquery) based on the user's customer group?
Here is what I have tried:

Code: Select all

<?php

    if ($this->customer->getGroupId() = 9 )  {
        $('.refine-images').css('opacity' , '0.5');
    }

?>

New member

Posts

Joined
Sat Mar 25, 2017 2:07 am

Post by artcore » Sun Mar 26, 2017 5:56 am

You can't use $this in templates anymore.
Try this in common/footer.php controller

Code: Select all

if($this->customer->getGroupId() === '9') {
 $this->document->addScript('catalog/view/theme/javascript/custom.js');
} 
And add your javascript in custom.js
Use a document ready around the code! $(function(){//your code});

Btw a single equals assigns, use double or triple for comparison. Triple checks also for type.

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 smithhorn » Sun Mar 26, 2017 7:47 am

Thanks!

New member

Posts

Joined
Sat Mar 25, 2017 2:07 am

Post by smithhorn » Tue Mar 28, 2017 2:29 am

It looks like no matter what my if statement is looking for in terms or user group, the script is injected into the page. Is there a better way to find and hold the value of the user group id?

New member

Posts

Joined
Sat Mar 25, 2017 2:07 am

Post by artcore » Tue Mar 28, 2017 2:35 am

Do a var_dump($this->customer->getGroupId()); just before the 'if' so you know what type and what number it is. For non-logged in visitors it's 0 I think

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 smithhorn » Tue Mar 28, 2017 3:09 am

I've add this to the catalog/controller/common/footer.php

Code: Select all

var_dump($this->customer->getGroupId());
		if($this->customer->getGroupId() == '0'){
		        $this->document->addScript('/catalog/view/theme/journal2/js/custom.js');
		} 
and it doesn't inject the js file into the head. I've also tried other "==" and "!==" values with no success. Any ideas?

New member

Posts

Joined
Sat Mar 25, 2017 2:07 am

Post by artcore » Tue Mar 28, 2017 4:52 am

I just did that (and refreshed modification cache!) and it gave me NULL as not logged in.
BTW I just saw they added footer scripts as well so you can also have it in there instead of the head

public function addScript($href, $postion = 'header') {
$this->scripts[$postion][$href] = $href;
}

That would be $this->document->addScript('catalog/view/javascript/....', 'footer');

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 smithhorn » Tue Mar 28, 2017 6:14 am

AHHH, silly modification refresh. This is great! Thank you so much!

New member

Posts

Joined
Sat Mar 25, 2017 2:07 am
Who is online

Users browsing this forum: No registered users and 37 guests