亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

更改 WooCommerce 產品描述選項卡按鈕和標題

更改 WooCommerce 產品描述選項卡按鈕和標題

PHP
九州編程 2023-08-19 16:17:41
當頁面的主體類為“parent-product_cat-vinyl”時,我想將 WooCommerce 產品描述選項卡按鈕和標題“描述”更改為“Tracklist”。到目前為止,這是我的代碼:<?phpif ( is_singular() ) {   $classes = get_body_class();   if (in_array('parent-product_cat-vinyl',$classes)) {       add_filter( 'woocommerce_product_description_tab_title','ps_rename_description_product_tab_label');       function ps_rename_description_product_tab_label() {           return 'Tracklist';       }   }}但這似乎不起作用。
查看完整描述

1 回答

?
Qyouu

TA貢獻1786條經驗 獲得超11個贊

您可以使用以下復合過濾器掛鉤($tab_key在您的情況下是description):

  • woocommerce_product_{$tab_key}_tab_title

  • woocommerce_product_{$tab_key}_heading

可以通過兩種方式完成:

有條件地使用定義的主體類:

add_filter( 'woocommerce_product_description_tab_title', 'change_product_description_tab_text' );

add_filter( 'woocommerce_product_description_heading', 'change_product_description_tab_text' );

function change_product_description_tab_text( $title ) {

    global $product;


    if( in_array( 'parent-product_cat-vinyl', get_body_class() ) ) {

        return __('Tracklist', 'woocommerce');

    }

    return $title;

}

或者有條件地針對產品類別(包括父產品類別):


// Custom conditional function that handle parent product categories too

function has_product_categories( $categories, $product_id = 0 ) {

    $parent_term_ids = $categories_ids = array(); // Initializing

    $taxonomy        = 'product_cat';

    $product_id      = $product_id == 0 ? get_the_id() : $product_id;


    if( is_string( $categories ) ) {

        $categories = (array) $categories; // Convert string to array

    }


    // Convert categories term names and slugs to categories term ids

    foreach ( $categories as $category ){

        $result = (array) term_exists( $category, $taxonomy );

        if ( ! empty( $result ) ) {

            $categories_ids[] = reset($result);

        }

    }


    // Loop through the current product category terms to get only parent main category term

    foreach( get_the_terms( $product_id, $taxonomy ) as $term ){

        if( $term->parent > 0 ){

            $parent_term_ids[] = $term->parent; // Set the parent product category

            $parent_term_ids[] = $term->term_id; // (and the child)

        } else {

            $parent_term_ids[] = $term->term_id; // It is the Main category term and we set it.

        }

    }

    return array_intersect( $categories_ids, array_unique($parent_term_ids) ) ? true : false;

}


add_filter( 'woocommerce_product_description_tab_title', 'change_product_description_tab_text' );

add_filter( 'woocommerce_product_description_heading', 'change_product_description_tab_text' );

function change_product_description_tab_text( $title ) {

    global $product;

    

    // Here set in the array the targeted product categories

    $categories = array('Vinyl');


    if ( has_product_categories( $categories, $product->get_id() ) ) {

         return __('Tracklist', 'woocommerce');

    }

    return $title;

}

代碼位于活動子主題(或活動主題)的functions.php 文件中。經過測試并有效。


查看完整回答
反對 回復 2023-08-19
  • 1 回答
  • 0 關注
  • 182 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號