仅在 WooCommerce 产品简短描述中为客人显示自定义文本

Show a custom text in WooCommerce product short description only for guests

我想知道你是否可以帮助我。我有以下代码,在我的网站上向新访问者显示 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;
}

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

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 文件。已测试并有效。