Page 1 of 1

OpenCart Adding Class to Product Options

Posted: Thu Oct 01, 2015 7:22 am
by WintersWolf
Currently trying to wrap my head around something in OpenCart. It's been years since I have done anything with PHP so having a little trouble. I'm redirecting clicks of href links in to the same div the page is in but some href links need to be ignored because they add elements to the page. I'm ignoring them with a not() selector via my own class I am adding to links I want to ignore, though having trouble adding a class to some links.

For example on the products form for adding products there's an option tab which lets you add different options such as check boxes and date/time. The drop down menu for adding these options has some href links that I can't seem to figure out how to add a class to.

Here's a pastebin to the product_form.php: http://pastebin.com/Fchxw8XG

Here's a look at the actual pages html through chrome dev tools: http://i.imgur.com/SwqO1Tp.png

I just need to figure out how to add a class called navtab to these dropdown links so I can ignore them.

Re: OpenCart Adding Class to Product Options

Posted: Thu Oct 01, 2015 7:35 am
by straightlight
SInce the class name is called navtab, you could pull the href link by using this jQuery statement:

Code: Select all

navtab = $('.navtab').attr('href');

alert(navtab);
This will popup every click event you use on the navtab class. If you want to remove the popup box, simply remove the alert line. From the defined navtab variable, you could add IF statement for all the URLs you want to pull or you could use AJAX to call PHP in order to pull valid URLs from a PHP array.

Re: OpenCart Adding Class to Product Options

Posted: Thu Oct 01, 2015 8:59 am
by WintersWolf
straightlight wrote:SInce the class name is called navtab, you could pull the href link by using this jQuery statement:

Code: Select all

navtab = $('.navtab').attr('href');

alert(navtab);
This will popup every click event you use on the navtab class. If you want to remove the popup box, simply remove the alert line. From the defined navtab variable, you could add IF statement for all the URLs you want to pull or you could use AJAX to call PHP in order to pull valid URLs from a PHP array.
Not sure I really understood, I don't think that really helps what I am trying to do however it did give me an idea of how I could fix the problem. So instead of finding where the href links are created I instead put in a little script that detects when links are clicked in that div and adds a class to them, and then my other script I am using to load all links to other pages within the same div ignores these links.

So this is my solution:

Code: Select all

$(function(){
    var option = $('#tab-option');
    option.delegate('a','click',function(){
        option.addClass('navtab');
        $(this).addClass('navtab');
    });
});
After that this runs to ignore all links with a class of navtab:

Code: Select all

$("#products").on("click", "a:not(.navtab, #)", function (e) {
    $("#products").load($(this).attr("href"));
    e.preventDefault();
});

Re: OpenCart Adding Class to Product Options

Posted: Thu Oct 01, 2015 11:08 am
by straightlight
Technically, you did added this line:

Code: Select all

$("#products").load($(this).attr("href"));
to your code which my example post above demonstrates. Glad you found the answer to your own discovery. :)

Re: OpenCart Adding Class to Product Options

Posted: Thu Oct 01, 2015 11:51 am
by WintersWolf
straightlight wrote:Technically, you did added this line:

Code: Select all

$("#products").load($(this).attr("href"));
to your code which my example post above demonstrates. Glad you found the answer to your own discovery. :)
Ah yeah, thanks for giving me a nudge in the right direction. Not sure I would of figured it out otherwise! :P