簡體   English   中英

在Woocommerce產品簡短說明中替換動態自定義標簽

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

對於Woocommerce網上商店,我正在使用WooCommerce Brands插件,我試圖通過特定產品值替換產品簡短描述中的動態特定自定義標簽:

  • 產品名稱
  • 產品類別(帶有術語鏈接)
  • 產品品牌(帶有術語鏈接)

例如,要替換的標簽可能是這樣的:

  • %product_name%
  • %category_name%
  • %brand_name% ...

此外,更換的產品類別和產品品牌可以鏈接到產品類別或產品品牌存檔頁面。

我不知道在哪里尋找。 我搜遍了谷歌,但遺憾的是我發現任何相關和有用的東西。

任何幫助表示贊賞。

更新2

以下是使用掛鈎在woocommerce_short_description過濾器鈎子中的自定義函數的方法,它將在單個產品頁面簡短描述中用產品數據值替換特定的自定義標簽。

現在,由於產品可以有許多產品類別許多產品品牌 ,我只保留第一個。

編碼:

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);
}

代碼位於活動子主題(或活動主題)的function.php文件中。 經過測試和工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM