簡體   English   中英

在 Woocommerce 檔案中顯示可變產品屬性和術語

[英]Display Variable Product Attributes and Terms on Woocommerce Archives

我正在嘗試使用鈎子woocommerce_shop_loop_item_title在商店頁面上完成屬性和術語列表。 目標是獲取產品的屬性和術語,然后像下面這樣顯示它:

顏色:紅、藍、綠

尺寸:小、中、大

尺寸:90*90、100*100和120*120

但沒有行之間的空格。

它應該“獲取”與產品和屬性術語一起使用的所有屬性。

我試過這個,但有致命的錯誤。

add_action( 'woocommerce_shop_loop_item_title', 'variable_att_and_terms_on_loop');
function variable_att_and_terms_on_loop() {

    foreach( $product->get_variation_attributes() as $taxonomy => $terms_slug ) {

    $taxonomy_label = wc_attribute_label( $taxonomy, $product );

    foreach($terms_slug as $term) {
        $term_name  = get_term_by('slug', $term, $taxonomy)->name;
        $attributes_and_terms_names[$taxonomy_label][$term] = $term_name;
    }
}
foreach ( $attributes_and_terms_names as $attribute_name => $terms_name ) {
    $terms_string = implode( ', ', $terms_name );
    echo '<p>' . $attribute_name . ': ' . $terms_string . '</p>';
}
}

我也試過這個:

add_action('woocommerce_shop_loop_item_title','add_attribute', 5);
function add_attribute() {
    global $product;

    $product_attributes = array( 'pa_weight', 'pa_quantity', 'pa_length', 'pa_color' );
    $attr_output = array();

    foreach( $product_attributes as $taxonomy ){
        if( taxonomy_exists($taxonomy) ){
            $label_name = get_taxonomy( $taxonomy )->labels->singular_name;
            $value = $product->get_attribute('pa_weight');

            if( ! empty($value) ){
                $attr_output[] = '<span class="'.$taxonomy.'">'.$label_name.': '.$value.'</span>';
            }
        }
    }
    echo '<div class="product-attributes">'.implode( '<br>', $attr_output ).'</div>';
}

沒有任何結果。 在嘗試了 LoicTheAztec 下面的新結果后,這就是我得到的: 在此處輸入圖片說明

2020 年更新- 刪除了嘗試從術語 slug 中獲取術語名稱時出現的錯誤。

在您的第一個代碼片段中有一些錯誤:

  • 未定義$product變量
  • 該功能需要僅限於可變產品
  • $attributes_and_terms_names變量未初始化...

這是重新訪問的代碼(行之間沒有空格)

add_action( 'woocommerce_shop_loop_item_title', 'variable_att_and_terms_on_loop');
function variable_att_and_terms_on_loop() {
    global $product;

    if( ! $product->is_type('variable') ) return; // Only for variable products

    $variation_attributes = $product->get_variation_attributes();

    if( sizeof($variation_attributes ) == 0 ) return; // Exit if empty

    $attributes = array(); // Initializing

    foreach( $product->get_variation_attributes() as $taxonomy => $terms_slug ) {
        $taxonomy_label = wc_attribute_label( $taxonomy, $product );

        $terms_name = array();

        foreach($terms_slug as $term_slug ) {
            // We try to get the term name when it's a term slug
            $term         = get_term_by('slug', $term_slug, $taxonomy);
            $terms_name[] = ! is_a($term, 'WP_Term') ? $term_slug : $term->name; 
        }
        $attributes[] = $taxonomy_label . ':&nbsp;' . implode( ', ', $terms_name );
    }

    echo '<div class="product-attributes">';
    echo '<span>' . implode('</span><br><span>', $attributes) . '</span>';
    echo '</div>';
}

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

暫無
暫無

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

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