简体   繁体   中英

Woocommerce content-single-product template override wont work

I have a problem, and im struggling from yesterday with it. So, I have web with woocommerce that is still on localhost. I have problem with template override. I overrided loop start, end, content-product.php and content-single-product.php in my theme and that works ok. I have problem with content-single-product.php because I want it to make dependable on category (whatever as identification - id, name, slug). So I found few solutions on google but none will work. In my singe-product.php I replaced

 woocommerce_get_template_part( 'content', 'single-product' );

with

if ( is_product_category( 'my-cat-slug' ) )
{
   woocommerce_get_template_part( 'content', 'single-product-dogs'   );  
}
else {
   woocommerce_get_template_part( 'content', 'single-product' ); 
}

I tried putting instead of my-cat-slug actual Cat Name, I tried using !is_product_category to get inverse... Also nothing. Of course I put single-product.php to woocommerce folder in my theme where rest of working overridden files are. And I created file content-single-product-dogs.php where I copied content from content-single-product.php with some changes, as small as just TEST written somewehere.

Also I tried

if ( ! function_exists( 'woocommerce_content' ) ) {
    function woocommerce_content() {
        if ( has_term( 'my-cat-slug', 'product_cat' ) ) {
            while ( have_posts() ) : the_post();
                wc_get_template_part( 'content', 'single-product-dogs' );
            endwhile;
        }
        else {
            while ( have_posts() ) : the_post();
                wc_get_template_part( 'content', 'single-product' );
            endwhile;
        }
    }
}

That also doesn't work. Everything I use, it always falls back to default overridden content-single-product.php

I have no more ideas. Please someone help :)

PS Is it possible to change default product-image.php that will be different for that custom content-single-product-dog.php?

Thanks

If you have already integrate WooCommerce in your theme, you might to override woocommerce_content() function.

It works perfectly on WooCommerce 2.5.5

  • Place you custom template files to your_theme/woocommerce/

  • Add code-snippet listed below to your theme functions.php :

/** 
 * Override 'woocommerce_content' function
 */

if ( ! function_exists( 'woocommerce_content' ) ) {

/**
 * Output WooCommerce content.
 *
 * This function is only used in the optional 'woocommerce.php' template.
 * which people can add to their themes to add basic woocommerce support.
 * without hooks or modifying core templates.
 *
 */
function woocommerce_content() {

    if ( is_singular( 'product' ) ) {

        while ( have_posts() ) : the_post();

            // Template depends from category slug

            if ( has_term( 'my-cat-slug', 'product_cat' ) ) {

              woocommerce_get_template_part( 'content', 'single-product-dogs' );  

            } else {

              woocommerce_get_template_part( 'content', 'single-product' ); 

            }

        endwhile;

    } else { ?>

        <?php if ( apply_filters( 'woocommerce_show_page_title', true ) ) : ?>

            <h1 class="page-title"><?php woocommerce_page_title(); ?></h1>

        <?php endif; ?>

        <?php do_action( 'woocommerce_archive_description' ); ?>

        <?php if ( have_posts() ) : ?>

            <?php do_action('woocommerce_before_shop_loop'); ?>

            <?php woocommerce_product_loop_start(); ?>

                <?php woocommerce_product_subcategories(); ?>

                <?php while ( have_posts() ) : the_post(); ?>

                    <?php wc_get_template_part( 'content', 'product' ); ?>

                <?php endwhile; // end of the loop. ?>

            <?php woocommerce_product_loop_end(); ?>

            <?php do_action('woocommerce_after_shop_loop'); ?>

        <?php elseif ( ! woocommerce_product_subcategories( array( 'before' => woocommerce_product_loop_start( false ), 'after' => woocommerce_product_loop_end( false ) ) ) ) : ?>

            <?php wc_get_template( 'loop/no-products-found.php' ); ?>

        <?php endif;

    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM