Edit HTML of woo commerce featured products widget

2019-07-18 11:25发布

问题:

I want to modify HTML code of woo-commerce featured product widget. Problem is that I can't find it inside my template folder/plugin folder.

Does someone knows how to modify featured product widget of woo-commerce?

回答1:

This is modified via the content-widget-product.php template, located in /woocommerce/templates/.

To override this template, copy: woocommerce/templates/content-widget-product.php to yourtheme/woocommerce/content-widget-product.php, and make any necessary modifications in the copied file (not the original).

For information on correctly modifying Woocommerce templates, please see the docs on overriding WooCommerce templates.



回答2:

I've done this with random products, but you can change whatever class you want, just change the file names...

add to functions.php

add_action( 'widgets_init', 'err_override_woocommerce_widgets', 15 );
function err_override_woocommerce_widgets() {
    if ( class_exists( 'WC_Widget_Random_Products' ) ) {
        unregister_widget( 'WC_Widget_Random_Products' );
        include_once( 'woocommerce/classes/class-wc-widget-random-products.php' );
        register_widget( 'err_WC_Widget_Random_Products' );
    }
}   

Note: don't forget to change the path, which is relative to functions.php!

Copy

wp-content/plugins/woocommerce/classes/widgets/class-wc-widget-random-products.php

to

wp-content/templates/%theme name%/woocommerce/classes/class-wc-widget-random-products.php

Note: change %theme name% to your theme name!

Add the widget to your sidebar/footer/etc, change anything in your class in it's new place, and you're done.