hooks - Edit WooCommerce product content based on category

We are using Google Translate to automatically translate our website but we want to exclude products in a certain catego

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!

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!

Share Improve this question edited Nov 13, 2017 at 8:06 m2 eCommerce asked Nov 12, 2017 at 9:55 m2 eCommercem2 eCommerce 111 silver badge4 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745104696a4611487.html

相关推荐

  • hooks - Edit WooCommerce product content based on category

    We are using Google Translate to automatically translate our website but we want to exclude products in a certain catego

    10小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信