简体   繁体   English

在Woocommerce产品简短说明中替换动态自定义标签

[英]Replace dynamically custom tags in Woocommerce product short description

For a Woocommerce webshop I am using WooCommerce Brands plugin and I'm trying to replace dynamically specific custom tags in the product short description by specific product values as: 对于Woocommerce网上商店,我正在使用WooCommerce Brands插件,我试图通过特定产品值替换产品简短描述中的动态特定自定义标签:

  • The product name 产品名称
  • Product category (with the term link) 产品类别(带有术语链接)
  • Product brand (with the term link) 产品品牌(带有术语链接)

For example, The tags to be replaced could be something like: 例如,要替换的标签可能是这样的:

  • %product_name% , %product_name%
  • %category_name%
  • and %brand_name% %brand_name% ...

Also, it be great for replaced product category and product brand to have the link to the product category or product brand archive pages. 此外,更换的产品类别和产品品牌可以链接到产品类别或产品品牌存档页面。

I'm not sure where to look for. 我不知道在哪里寻找。 I've searched all over google but I can't find anything related and useful unfortunately. 我搜遍了谷歌,但遗憾的是我发现任何相关和有用的东西。

Any help is appreciated. 任何帮助表示赞赏。

Update 2 更新2

Here is the way to do it using this custom function hooked in woocommerce_short_description filter hook, that will replace specific custom tags by product data values in single product pages short description. 以下是使用挂钩在woocommerce_short_description过滤器钩子中的自定义函数的方法,它将在单个产品页面简短描述中用产品数据值替换特定的自定义标签。

Now as product can have many product categories and many product brands , I keep only the first one. 现在,由于产品可以有许多产品类别许多产品品牌 ,我只保留第一个。

The code: 编码:

add_filter('woocommerce_short_description', 'customizing_wc_short_description', 20, 1);
function customizing_wc_short_description($short_description){
    if( is_archive() ) return $short_description;
    global $product, $post;

    // 1. Product categories (a product can have many)
    $catgories = array();
    foreach( wp_get_post_terms( $post->ID, 'product_cat' ) as $term ){
        $term_link   = get_term_link( $term, 'product_cat' );
        $catgories[] = '<a class="cat-term" href="'.$term_link.'">'.$term->name.'</a>'; // Formated
    }

    // 2. Product brands (a product can have many)
    $brands = array();
    foreach( wp_get_post_terms( $post->ID, 'product_brand' ) as $term ){
        $term_link   = get_term_link( $term, 'product_brand' );
        $brands[] = '<a class="brand-term" href="'.$term_link.'">'.$term->name.'</a>'; // Formated
    }

    // 3. The data array of tags to be replaced by product values
    $data = array(
        '%product_name%'  => $product->get_name(),
        '%category_name%' => reset($catgories), // We take the first product category
        '%brand_name%'    => reset($brands),
    );

    $keys   = array_keys($data); // The Tags
    $values = array_values($data); // The replacement values

    // Replacing custom tags and returning "clean" short description
    return str_replace( $keys, $values, $short_description);
}

Code goes in function.php file of your active child theme (or active theme). 代码位于活动子主题(或活动主题)的function.php文件中。 Tested and works. 经过测试和工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM