0
votes

I want to make some changes to widget named WC-widget-price-filter, I have created a new file with the same name with exact same root in my child theme directory:

my-theme/woocommerce/includes/widgets/class-wc-widget-price-filter.php

Changes doesn't appears, should I call the file from my child theme's functions.php ? and if so how can I ?

Thank you in advance!


So I tried to insert this code into my functions.php and made only 1 minor change which didn't appear. Done something wrong?

class My_Widget_Price_Filter extends WC_Widget_Price_Filter {
   /**
 * Output widget.
 *
 * @see WP_Widget
 *
 * @param array $args     Arguments.
 * @param array $instance Widget instance.
 */
public function widget( $args, $instance ) {
    global $wp;

    if ( ! is_shop() && ! is_product_taxonomy() ) {
        return;
    }

    $min_price = isset( $_GET['min_price'] ) ? wc_clean( wp_unslash( $_GET['min_price'] ) ) : null; // WPCS: input var ok, CSRF ok.
    $max_price = isset( $_GET['max_price'] ) ? wc_clean( wp_unslash( $_GET['max_price'] ) ) : null; // WPCS: input var ok, CSRF ok.
    if ( ! wc()->query->get_main_query()->post_count && null === $min_price && null === $max_price ) {
        return;
    }

    wp_enqueue_script( 'wc-price-slider' );

    // Find min and max price in current result set.
    $prices = $this->get_filtered_price();
    $min    = floor( $prices->min_price );
    $max    = ceil( $prices->max_price );

    if ( $min === $max ) {
        return;
    }

    $this->widget_start( $args, $instance );

    if ( '' === get_option( 'permalink_structure' ) ) {
        $form_action = remove_query_arg( array( 'page', 'paged', 'product-page' ), add_query_arg( $wp->query_string, '', home_url( $wp->request ) ) );
    } else {
        $form_action = preg_replace( '%\/page/[0-9]+%', '', home_url( trailingslashit( $wp->request ) ) );
    }

    $min_price = null !== $min_price ? $min_price : apply_filters( 'woocommerce_price_filter_widget_min_amount', $min );
    $max_price = null !== $max_price ? $max_price : apply_filters( 'woocommerce_price_filter_widget_max_amount', $max );

    echo '<form method="get" action="' . esc_url( $form_action ) . '">
        <div class="price_slider_wrapper">
            <div class="price_slider" style="display:none;"></div>
            <div class="price_slider_amount">
                <input type="text" id="min_price" name="min_price" value="' . esc_attr( $min_price ) . '" data-min="' . esc_attr( apply_filters( 'woocommerce_price_filter_widget_min_amount', $min ) ) . '" placeholder="' . esc_attr__( 'Min price', 'woocommerce' ) . '" />
                <input type="text" id="max_price" name="max_price" value="' . esc_attr( $max_price ) . '" data-max="' . esc_attr( apply_filters( 'woocommerce_price_filter_widget_max_amount', $max ) ) . '" placeholder="' . esc_attr__( 'Max price', 'woocommerce' ) . '" />
                <button type="submit" class="button">' . esc_html__( 'Filter', 'woocommerce' ) . '</button>
                <div class="price_label" style="display:none;">
                    ' . esc_html__( 'Price:', 'woocommerce' ) . ' <span class="from"></span> &mdash; <span class="to"></span>
                </div>
                ' . wc_query_string_form_fields( null, array( 'min_price', 'max_price', 'paged' ), '', true ) . '
                <div class="clear"></div>
            </div>
        </div>
    </form>'; // WPCS: XSS ok.

    $this->widget_end( $args );
}
}

function My_Widget_price_filter_register() {
unregister_widget( 'WC_Widget_Price_Filter' );
register_widget( 'My_Widget_Price_Filter' );
add_action( 'widgets_init', 'My_Widget_price_filter_register' );
} 
1
Please add the widget code in your question…LoicTheAztec
Hey there, I simply want to copy the WHOLE file with all the code within it and make some changes to it..RoeeDo
You can't override a core function via your theme (like if it was a Woocommerce template)… It simply doesn't work and has no effect. As you have made some changes in the code of this cloned function, you should add the modified function in your question, to see what we can do in an other way. Changes can be done with action and filter hooks instead, via your themes function.php file.LoicTheAztec
Okey ! Well I thought I can override widgets just like templates.. Thank you !!!RoeeDo

1 Answers

2
votes

You need to extend the Woocomerce widget in order to edit the content and then deregister the default widget to register yours:

Class My_Widget_Price_Filter extends WC_Widget_Price_Filter {
    function widget( $args, $instance ) {

        /**
         * Output widget.
         *
         * @see WP_Widget
         *
         * @param array $args
         * @param array $instance
         */
        public function widget( $args, $instance ) {
            global $wp;

            if ( ! is_shop() && ! is_product_taxonomy() ) {
                return;
            }

            if ( ! wc()->query->get_main_query()->post_count ) {
                return;
            }

            wp_enqueue_script( 'wc-price-slider' );

            // Find min and max price in current result set.
            $prices = $this->get_filtered_price();
            $min    = floor( $prices->min_price );
            $max    = ceil( $prices->max_price );

            if ( $min === $max ) {
                return;
            }

            $this->widget_start( $args, $instance );

            if ( '' === get_option( 'permalink_structure' ) ) {
                $form_action = remove_query_arg( array( 'page', 'paged', 'product-page' ), add_query_arg( $wp->query_string, '', home_url( $wp->request ) ) );
            } else {
                $form_action = preg_replace( '%\/page/[0-9]+%', '', home_url( trailingslashit( $wp->request ) ) );
            }

            $min_price = isset( $_GET['min_price'] ) ? esc_attr( $_GET['min_price'] ) : apply_filters( 'woocommerce_price_filter_widget_min_amount', $min );
            $max_price = isset( $_GET['max_price'] ) ? esc_attr( $_GET['max_price'] ) : apply_filters( 'woocommerce_price_filter_widget_max_amount', $max );

            echo '<form method="get" action="' . esc_url( $form_action ) . '">
                <div class="price_slider_wrapper">
                    <div class="price_slider" style="display:none;"></div>
                    <div class="price_slider_amount">
                        <input type="text" id="min_price" name="min_price" value="' . esc_attr( $min_price ) . '" data-min="' . esc_attr( apply_filters( 'woocommerce_price_filter_widget_min_amount', $min ) ) . '" placeholder="' . esc_attr__( 'Min price', 'woocommerce' ) . '" />
                        <input type="text" id="max_price" name="max_price" value="' . esc_attr( $max_price ) . '" data-max="' . esc_attr( apply_filters( 'woocommerce_price_filter_widget_max_amount', $max ) ) . '" placeholder="' . esc_attr__( 'Max price', 'woocommerce' ) . '" />
                        <button type="submit" class="button">' . esc_html__( 'Filter', 'woocommerce' ) . '</button>
                        <div class="price_label" style="display:none;">
                            ' . esc_html__( 'Price:', 'woocommerce' ) . ' <span class="from"></span> &mdash; <span class="to"></span>
                        </div>
                        ' . wc_query_string_form_fields( null, array( 'min_price', 'max_price' ), '', true ) . '
                        <div class="clear"></div>
                    </div>
                </div>
            </form>';

            $this->widget_end( $args );
        }
    }
}

function my_widget_price_filter_register() {
    unregister_widget( 'WC_Widget_Price_Filter' );
    register_widget( 'My_Widget_Price_Filter' );
} add_action( 'widgets_init', 'my_widget_price_filter_register' );