1

We are using Google Translate to automatically translate our website but we want to exclude products in a certain category. We already accomplished that for some products by placing the <span class="notranslate">Span</span> tag manually in the product descriptions and in the respective template files but now we want to do that for a larger quantity of products so we think using a hook would be a better way.

I want to achieve something like follows but I don't know how to correctly insert the NoTranslate span only for the product description:

add_action( 'wp', 'disable_translation' );
function disable_translation() {
    // If a product in the 'Cookware' category is being viewed...
    if ( is_product() && has_term( 'Cookware', 'product_cat' ) ) {
        //... Wrap the product description into notranslate span to disable the translation
        <span class="notranslate">???????????????????</span>
    }
}

Another way to do this probably is by editing the description.php in wp-content/plugins/woocommerce/templates/single-product/tabs as follows:

<?php if ( has_term( 'Cookware', 'product_cat' ) )  {
    echo "<span class='notranslate'>";
    }
    else {
        echo "<span>";
    } ?>
        <?php the_content(); ?>
    echo "</span>";
<?php endif; ?>

But unfortunately this resulted in the following error:

Parse error: syntax error, unexpected ‘endif’ (T_ENDIF), expecting end of file in /wp-content/plugins/woocommerce/templates/single-product/tabs/description.php on line 41

Have I misspelled something?

Could anybody kindly tell me how to do this? Thanks a lot!

1 Answer 1

1

There's a couple of ways to do it, you can edit the template file \woocommerce\templates\single-product\title.php directly and change your title format there. Otherwise, you can remove the WordPress hook in your functions.php file and add your own to override it which I borrowed from this answer

<?php
    remove_action( 'woocommerce_single_product_summary','woocommerce_template_single_title', 5 );
    add_action( 'woocommerce_single_product_summary', 'modify_woocommerce_template_single_title',5 );

    function modify_woocommerce_template_single_title() {
        if ( has_term( 'Cookware', 'product_cat' ) ) {
    ?>
    <h1 class="product_title entry-title"><span class="notranslate"><?php the_title(); ?></span></h1>
    <?php
        }
        else {
            ?><h1 class="product_title entry-title"><?php the_title(); ?></h1><?php
        }
    }
    ?>

This will only change the single product page titles. The shop page will keep the normal product titles as it uses a different hook but you can apply the same principles are remove the hook woocommerce_shop_loop_item_title and override the function woocommerce_template_loop_product_title

2
  • Thanks a lot for the detailed answer! Much appreciated. We are looking to edit the product description though, is there also a similar way to do this for the description? Nov 12, 2017 at 14:28
  • It depends what description you want to change, there's a filter you can hook into to modify the short description called 'woocommerce_short_description' Nov 12, 2017 at 15:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.