• Resolved MatMayer

    (@matmayer)


    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>
    	}
    }

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

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • It would be probably easier to edit your product template file. Make a copy to your theme folder, then
    add

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

    before the_content(); in your description template file, and
    echo "</span>";
    after it.

    Thread Starter MatMayer

    (@matmayer)

    Thanks for the quick reply. So I have edited 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; ?>

    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?

    Sure, you added an endif line that is causing the error. Also, the last echo is not marked as php code (it should be in between <?php and ?> ). You can remove all extra openings and endings.

    Plugin Support Hannah S.L.

    (@fernashes)

    Automattic Happiness Engineer

    We haven’t heard back from you in a while, so I’m going to mark this as resolved – if you have any further questions, you can start a new thread.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Edit product content based on category’ is closed to new replies.