首页 > 解决方案 > 仅在 WooCommerce 产品简短描述中为客人显示自定义文本

问题描述

我想知道你是否可以帮助我。我有以下代码,在 woocommerce 中向我的网站上的新访问者显示我的产品简短描述上方的文本。根据文本执行所需的操作(创建帐户)后,我想隐藏不再需要的文本。

我真的很感激任何帮助。

add_filter('woocommerce_short_description','ts_add_text_short_descr');
function ts_add_text_short_descr($description){
    echo 'To check if this item qualifies for a discount <a href="https://www.livestainable.co.za/my-account-2/"><u><strong>Log In or Create</strong></u></a> an account.</br></br>';
    return $description.$text;
}

标签: phpwordpresswoocommerceproductlogged

解决方案


要仅为来宾显示一些文本,只需! is_user_logged_in()IF语句中使用,就像在这个重新访问的代码中一样:

add_filter( 'woocommerce_short_description', 'add_text_before_short_description_for_guests' );
function add_text_before_short_description_for_guests( $description ) {
    // Only for guest users
    if ( ! is_user_logged_in() ) {
        $description = '<p>' . sprintf( __( "To check if this item qualifies for a discount %s an account."), '<a href="https://www.livestainable.co.za/my-account-2/"><u><strong>' . __("Log In or Create") . '</strong></u></a>' ) . '</p>' . $description;
    }
    return $description;
}

代码位于活动子主题(或活动主题)的 functions.php 文件中。测试和工作。


推荐阅读