Woocommerce - overriding the template through a plugin

前端 未结 3 2025
南旧
南旧 2021-01-14 16:09

I have a question: is there a way to override WooCommerce\'s default template through a plugin the same way you\'d do it with a theme? I have this code:

Clas         


        
3条回答
  •  一整个雨季
    2021-01-14 16:38

    • Using filters wc_get_template_part we can override default WooCommerce template part's.
    • Using filters woocommerce_locate_template we can override default WooCommerce template's.

    Try below example code snippet.

    ';
        // echo 'template: ' . $template . '
    '; // echo 'slug: ' . $slug . '
    '; // echo 'name: ' . $name . '
    '; // echo '
    '; // Template directory. // E.g. /wp-content/plugins/my-plugin/woocommerce/ $template_directory = untrailingslashit( plugin_dir_path( __FILE__ ) ) . 'woocommerce/'; if ( $name ) { $path = $template_directory . "{$slug}-{$name}.php"; } else { $path = $template_directory . "{$slug}.php"; } return file_exists( $path ) ? $path : $template; } /** * Template File * * @param string $template Default template file path. * @param string $template_name Template file name. * @param string $template_path Template file directory file path. * @return string Return the template file from plugin. */ function override_woocommerce_template( $template, $template_name, $template_path ) { // UNCOMMENT FOR @DEBUGGING // echo '
    ';
        // echo 'template: ' . $template . '
    '; // echo 'template_name: ' . $template_name . '
    '; // echo 'template_path: ' . $template_path . '
    '; // echo '
    '; // Template directory. // E.g. /wp-content/plugins/my-plugin/woocommerce/ $template_directory = untrailingslashit( plugin_dir_path( __FILE__ ) ) . 'woocommerce/'; $path = $template_directory . $template_name; return file_exists( $path ) ? $path : $template; }

提交回复
热议问题