Support » Plugin: YITH WooCommerce Badge Management » override a class

  • Resolved arbremojo

    (@arbremojo)


    Hi !

    I would like to override the constructor of the YITH_WCBM_Frontend class from the class.yith-wcbm-frontend.php file. Because there’s a if(!class_exists(‘YITH_WCBM_Frontend’)), I thought I could just copy and paste the class in functions.php of my child theme and it would work. But it doesn’t, it throws an error which says : “Cannot declare class YITH_WCBM_Frontend, because the name is already in use in …/themes/north-wp-child/functions.php”.
    I also tried to simply copy and paste the entire file in my-child-theme/yith-woocommerce-badges-management, but it doesn’t work either, the file isn’t called.
    I tried to make an extends of the original class in functions.php too. But it doesn’t work…

    The !class_exists seems not to work on plugin territory (well, that’s what I figured out by looking a bit on the internet)

    Do you have any idea? 🙂

    • This topic was modified 7 months, 3 weeks ago by arbremojo.
    • This topic was modified 7 months, 3 weeks ago by arbremojo.
Viewing 2 replies - 1 through 2 (of 2 total)
  • I figured it out, I copied and pasted the class in functions.php and changed the name of the my class, and added this at the end outside the class to instantiate it :

    if(class_exists('YITH_WCBM_Frontend'))
    {
        new custom_YITH_WCBM_Frontend;
    }
    Plugin Author YITHEMES

    (@yithemes)

    Hi,
    and thank you for writing in!

    We’re happy you found a solution.

    The “class_exists” function works fine also in plugins: in this case you should consider the functions.php theme file is loaded after plugins.
    So, when the functions.php file is loaded, the plugin classes are already declared.

    In general, it’s not a good practice copying and pasting a whole plugin class to modify it, since the class could change in next plugin release.
    Otherwise, if you need to add/remove some features you could use actions and filters to change the plugin behavior.

    Feel free to ask in future if you’ll need any further help 😉

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘override a class’ is closed to new replies.