【问题标题】:Customize WooCommerce Product Name everywhere in Frontend在前端随处自定义 WooCommerce 产品名称
【发布时间】:2020-08-09 00:52:28
【问题描述】:

我想在 Woocommerce 上销售反向链接。为此,我需要隐藏保存在 WooCommerce 产品标题中的域名。有没有办法操纵显示产品名称的所有位置,例如 Shop Loop、结帐页面、基本上所有地方,这样客户就看不到 domain.com 而看到的是 d****n.com?也许有一个过滤器或钩子或其他东西。

我已经在这里尝试过Change related product names via filter in WooCommerce

但它不适用于购物车、结帐和忍者桌子。

【问题讨论】:

    标签: php wordpress woocommerce product hook-woocommerce


    【解决方案1】:

    要隐藏 WooCommerce 产品名称(或标题),请使用以下命令:

    add_filter( 'the_title', 'hide_product_title', 10, 2 );
    function hide_product_title( $title, $post_id ) {
        global $woocommerce_loop;
    
        if ( ! is_admin() && ! empty($woocommerce_loop) ) {
            $title = '';
        }
        return $title;
    }
    
    add_filter( 'woocommerce_product_get_name', 'hide_product_name', 10, 2 );
    add_filter( 'woocommerce_product_variation_get_name', 'hide_product_name', 10, 2 );
    function hide_product_name( $name, $product ) {
        if ( ! is_admin() ) {
            $name = '';
        }
        return $name;
    }
    

    现在要在任何地方隐藏作为产品标题包含的域名,请使用以下(将域名替换为星号,首尾字符除外): p>

    // Custom function to replace a string (domain name) with a repeating character (a star by default)
    function hide_domain_name( $string, $repl_char = '*' ) {
        $index_needle = strpos($string, '.');
        $replacement  = str_repeat($repl_char, ($index_needle > 2 ? $index_needle - 2 : strlen($string) - 1));
        return substr_replace($string, $replacement, 1) . substr($string, ($index_needle > 2 ? $index_needle - 1 : strlen($string) - 1));
    }
    
    add_filter( 'the_title', 'hide_product_title', 10, 2 );
    function hide_product_title( $title; $post_id ) {
        global $woocommerce_loop;
    
        if ( ! is_admin() && ! empty($woocommerce_loop) ) {
            $title = hide_domain_name( $title );
        }
        return $title;
    }
    
    add_filter( 'woocommerce_product_get_name', 'hide_product_name', 10, 2 );
    add_filter( 'woocommerce_product_variation_get_name', 'hide_product_name', 10, 2 );
    function hide_product_name( $name, $product ) {
        if ( ! is_admin() ) {
            $name = hide_domain_name( $name );
        }
        return $name;
    }
    

    不要忘记为您的产品制作自定义永久链接,因为产品标题(域名)应该出现在其中。

    代码在您的活动子主题(或活动主题)的functions.php 文件中。经过测试并且可以工作。

    【讨论】:

    • 感谢您的好评。这似乎与 Woocommerce 完美配合,但不幸的是,与 Ninja Tables Pro 输出不兼容。有什么想法可以在那里完成吗?
    • 我不知道 Ninja Tables Pro 插件,因为我的客户不使用它。由于您没有在问题中提到它是针对 Ninja Tables Pro 插件的,并且我的回答在 WooCommerce 中有效,您可以请accept 回答,谢谢。
    猜你喜欢
    • 2019-03-31
    • 2021-04-01
    • 2021-12-31
    • 1970-01-01
    • 2021-01-05
    • 2019-07-02
    • 1970-01-01
    • 2017-10-26
    • 1970-01-01
    相关资源
    最近更新 更多