• Resolved nabaraj chapagain

    (@nabaraj-chapagain)


    Hi There

    im trying to develop small plugin for woocommerce store where i need to override some woo templates. I’ve override some of them using woocommerce_locate_template like content-product_cat and works fine but content-product.php doesn’t seems to be working!!

    can anybody let me know if im missing something? here is the code im using..

    add_filter( ‘woocommerce_locate_template’, ‘woodisplay_woocommerce_locate_template’, 10, 3 );
    function woodisplay_woocommerce_locate_template( $template, $template_name, $template_path ) {
    global $woocommerce;
    $_template = $template;
    if ( ! $template_path ) $template_path = $woocommerce->template_url;
    $plugin_path = woodisplay_plugin_path() . ‘/woocommerce/templates/’;
    // Look within passed path within the theme – this is priority
    $template = locate_template(
    array(
    $template_path . $template_name,
    $template_name
    )
    );
    // Modification: Get the template from this plugin, if it exists
    if ( ! $template && file_exists( $plugin_path . $template_name ) )
    $template = $plugin_path . $template_name;
    // Use default template
    if ( ! $template )
    $template = $_template;
    // Return what we found
    return $template;

    }

    https://wordpress.org/plugins/woocommerce/

Viewing 1 replies (of 1 total)
  • Use this function for other templates not covered by your code ie content-single-product.php

    //
    // get path for templates used in loop ( like content-product.php )
    add_filter( 'wc_get_template_part', function( $template, $slug, $name )
    { 
    
        // Look in plugin/woocommerce/slug-name.php or plugin/woocommerce/slug.php
        if ( $name ) {
            $path = plugin_dir_path( __FILE__ ) . WC()->template_path() . "{$slug}-{$name}.php";
        } else {
            $path = plugin_dir_path( __FILE__ ) . WC()->template_path() . "{$slug}.php";
        }
    
        return file_exists( $path ) ? $path : $template;
    
    }, 10, 3 );
Viewing 1 replies (of 1 total)
  • The topic ‘woocommerce template override inside custom plugins’ is closed to new replies.