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

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

顯示 WooCommerce 自定義產品屬性和單個產品的所有術語

顯示 WooCommerce 自定義產品屬性和單個產品的所有術語

PHP
拉丁的傳說 2023-06-24 17:06:56
我使用自定義函數在 WooCommerce 單一產品頁面上創建了六個自定義屬性。它們包括:圖標、標簽和術語?;趯?WooCommerce 屬性標簽替換為每個答案代碼的自定義圖像,我使用了以下函數(使用自定義 HTML 標記,與 WooCommerce Product-attributes.php 模板中的標記不同):add_action('woocommerce_single_product_summary', 'pa_custom_attributes', 25);function pa_custom_attributes(){? ? global $product;? ? $attributes = $product->get_attributes();? ? if ( ! $attributes ) return;? ? $out = '<div class="custom-attributes">';? ? foreach ( $attributes as $attribute ) {? ? ? ? if ( $attribute->get_variation() ) continue;? ? ? ? if ( $attribute->is_taxonomy() ) {? ? ? ? ? ??? ? ? ? ? ? $taxonomy = $attribute->get_name();? ? ? ? ? ? $taxo_obj = $attribute->get_taxonomy_object();? ? ? ? ? ? $name = $taxo_obj->attribute_name;? ? ? ? ? ? $label = $taxo_obj->attribute_label;? ? ? ? ? ? $label_name = wc_attribute_label( $taxonomy );? ? ? ? ? ? $out .= '<div class="' . esc_attr( $taxonomy ) . ' single-attribute">';? ? ? ? ? ? $out .= '<div><img class="attribute-image" src="'.get_stylesheet_directory_uri().'/woocommerce/attributes/'.$name.'.svg" alt="Attribute '.$label.'"/></div>';? ? ? ? ? ? $out .= '<div class="attribute-label '.$label.'">'.$label_name.'</div>';? ? ? ? ? ??? ? ? ? ? ? $out .= '<div class="attribute-values">';? ? ? ? ? ? $terms = wp_get_post_terms( $product->get_id(), $taxonomy, array('fields' => 'names') );? ? ? ? ? ? foreach ( $terms as $term_name )? ? ? ? ? ? ? ? $term_names['names'] = $term_name;? ? ? ? ? ? $out .= implode(', ', $term_names);? ? ? ? ? ? $out .= '</div></div>';? ? ? ? }?該函數工作正常,但有一個問題:如果特定屬性的項超過一個(例如顏色:紅色、藍色、綠色),則該函數在屏幕上僅打印數組的最后一個值。我閱讀了文檔并做了很多測試,檢查CSS沒有問題。有誰可以幫我理解錯誤出在哪里嗎?
查看完整描述

1 回答

?
侃侃爾雅

TA貢獻1801條經驗 獲得超16個贊

問題來自于您的代碼中的以下內容:


            $terms = wp_get_post_terms( $product->get_id(), $taxonomy, array('fields' => 'names') );


            foreach ( $terms as $term_name )

                $term_names['names'] = $term_name;


            $out .= implode(', ', $term_names);

你可以用這個替換:


            $terms = wp_get_post_terms( $product->get_id(), $taxonomy, array('fields' => 'names') );


            $term_names = []; // Initializing


            // Loop through each terms

            foreach ( $terms as $term_name ) {

                $term_names[] = $term_name;

            }


            $out .= implode(', ', $term_names);

或者用一行代碼就更好了:


            $out .= $product->get_attribute( $taxonomy );


查看完整回答
反對 回復 2023-06-24
  • 1 回答
  • 0 關注
  • 186 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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