PaulMcF87
PaulMcF87

Reputation: 405

Overriding widget via functions.php

Woocommerce comes with a default product filter widget, located in

/wp-content/plugins/woocommerce/includes/widgets/class-wc-widget-layered-nav-filters.php

I have a lot of products/categories etc and using this looks very messy. I have tried to tidy it up and turn it into an accordion menu which seems to work as can be seen from code at bottom.

I am struggling, however, to add it into my theme.

I have created my own widget using:

<?php>
    class Custom_WC_Widget_Layered_Nav extends WC_Widget_Layered_Nav {
        Public function widget( $args, $instance ) {
          //taken details prom default plugin and inserted here
		}
    ?>	

    <script>
        var acc = document.getElementsByClassName("widget-title");
        var i;

        for (i = 0; i < acc.length; i++) {
            acc[i].onclick = function() {
                this.classList.toggle("active");
                var panel = this.nextElementSibling;
                if (panel.style.maxHeight){
                panel.style.maxHeight = null;
                } else {
                panel.style.maxHeight = panel.scrollHeight + "px";
                } 
            }
        }
    </script>

I have saved this in:

/wp-content/themes/my-theme/woocommerce/includes/widgets/custom-wc-widget-layered-nav.php

I then added the following code into functions.php:

add_action( 'widgets_init', 'err_override_woocommerce_widgets', 15 );
function err_override_woocommerce_widgets() {
    if ( class_exists( 'WC_Widget_Layered_Nav' ) ) {
        unregister_widget( 'WC_Widget_Layered_Nav' );
        include_once( 'wp-content/themes/my-theme/woocommerce/includes/widgets/custom-wc-widget-layered-nav.php' );
        register_widget( 'Custom_WC_Widget_Layered_Nav' );
    }
} 

The new code in the functions.php seems to give errors:

Fatal error: Class 'Custom_WC_Widget_Layered_Nav' not found in /homepages/8/d692312546/htdocs/Mytheme/wp-includes/class-wp-widget-factory.php on line 106

Is there anything I can change to avoid this error?

as you can see below, the coding seems to work and have desired affect.

var acc = document.getElementsByClassName("widget-title");
    var i;

    for (i = 0; i < acc.length; i++) {
        acc[i].onclick = function() {
            this.classList.toggle("active");
            var panel = this.nextElementSibling;
            if (panel.style.maxHeight){
            panel.style.maxHeight = null;
            } else {
            panel.style.maxHeight = panel.scrollHeight + "px";
            } 
        }
    }
h1.widget-title {
    background-color: #eee;
    color: #444;
    cursor: pointer;
    padding: 5px;
    width: 100%;
    text-align: left;
    border: none;
    outline: none;
    transition: 0.4s;
    margin-top:5px;
}

/* Add a background color to the button if it is clicked on (add the .active class with JS), and when you move the mouse over it (hover) */
h1.widget-title.active, h1.widget-title:hover {
    background-color: #ccc;
}

/* Style the accordion panel. Note: hidden by default */
aside>ul {
    padding: 0 18px;
    background-color: white;
    max-height: 0;
    overflow: hidden;
    transition: max-height 0.2s ease-out;
}
h1.widget-title:after {
    content: '\02795'; /* Unicode character for "plus" sign (+) */
    font-size: 13px;
    color: #777;
    float: right;
    margin-left: 5px;
}

h1.widget-title.active:after {
    content: "\2796"; /* Unicode character for "minus" sign (-) */
}

#secondary .widget {
    font-size: 0.86em;
}
.widget-area > aside {
    background: white;
    border: 1px solid #ededed;
    border-radius: 6px;
    padding: 5px 20px;
    margin-bottom: 5px;
    width:20em
}
.widget-title {
    font-size: 1.43em;
}
<div class="widget-area" id="secondary" role="complementary">
    <aside class="widget woocommerce widget_layered_nav" id="Woocommerce-layered-nav-1">
        <h1 class="widget-title">Title1</h1>
        <ul>
            <li class="layered-nav-term">
                <a href="http....co.uk">nav term</a>
                <span class="count">(10)</span>
            </li>
        </ul>
    </aside>
    <aside class="widget woocommerce widget_layered_nav" id="Woocommerce-layered-nav-2">
        <h1 class="widget-title">Title2</h1>
        <ul>
            <li class="layered-nav-term">
                <a href="http....co.uk">nav term2</a>
                <span class="count">(10)</span>
            </li>
        </ul>
    </aside>
</div>

Upvotes: 2

Views: 3972

Answers (1)

EniGma
EniGma

Reputation: 2464

According to this article

you have to change the path according to where you saved this widget:

add_action( 'widgets_init', 'err_override_woocommerce_widgets', 15 );
 
function err_override_woocommerce_widgets() {
  // Ensure our parent class exists to avoid fatal error (thanks Wilgert!)
 
  if ( class_exists( 'WC_Widget_Layered_Nav' ) ) {

        unregister_widget( 'WC_Widget_Layered_Nav' );
 
        include_once( 'woocommerce/includes/widgets/custom-wc-widget-layered-nav.php' );
 
        register_widget( 'Custom_WC_Widget_Layered_Nav' );
  }
 
}

Hope it works!

UPDATE:

What comes in mind is first save the script inside your js folder and name it something like custom.js then you have to register that script as follow:

// Register your assets during `wp_enqueue_scripts` hook inside `functions.php`.
function custom_register_scripts() {
    // Give the path of the script
    wp_register_script('js-custom', 'path/to/js/custom.js',array('jquery'));
}

then you can enqueue your script into where your widget is.

public function widget( $args, $instance ) {
        // Enqueue needed assets inside the `widget` function.
        wp_enqueue_script('js-custom');

        // Output widget contents.
    }

Upvotes: 4

Related Questions