問題描述
在 WooCommerce 中,我使用
目前,四個單選選項中每個選項的變體名稱都是紅色",我希望每個選項都有不同的顏色.
為了實現這一點,我必須對代碼進行哪些更改?
謝謝
2021 更新
這是您重新訪問的代碼,它將僅顯示在標簽"周圍.屬性單選按鈕自定義顯示文本 標記具有基于屬性 slug 和
$term_slug<的不同類值的不同類值/code>
.
因此,您將能夠僅將一些 CSS 樣式顏色應用于每個單選按鈕顯示的pa_tab"屬性的自定義文本,將這些 CSS 規則添加到您的活動主題 style.css
...>
這是重新訪問的代碼:
//從產品屬性選項名稱中獲取變體ID的自定義函數函數 get_variation_id_from_option_name( $term_slug, $taxonomy, $product_id ) {全球 $wpdb;返回 $wpdb->get_var( $wpdb->prepare( "選擇 pm.post_idFROM {$wpdb->prefix}postmeta pmLEFT JOIN {$wpdb->prefix}posts p ON pm.post_id = p.IDWHERE pm.meta_key LIKE '%s'AND pm.meta_value = '%s'AND p.post_parent = %d", '屬性_' .$taxonomy, $term_slug, $product_id ) );}//在變體下拉菜單中顯示產品變體價格.add_filter('woocommerce_variation_option_name', 'display_price_in_variation_option_name');函數 display_price_in_variation_option_name( $option_name ) {全球$產品;$taxonomy = 'pa_tab';//HERE 定義目標產品屬性分類法 pa_color$term = get_term_by( 'name', $option_name, $taxonomy );if ( is_admin() || !is_a( $term, 'WP_Term' ) || !is_a( $product, 'WC_Product' ) ) {返回 $option_name;}$variation_id = get_variation_id_from_option_name( $term->slug, $taxonomy, $product->get_id() );如果( $variation_id > 0 ){$variation = wc_get_product( $variation_id );$price_html = wc_price( wc_get_price_to_display( $variation ) );if ( has_term( $option_name, $taxonomy, $product->get_id() ) ) {$output = ' <span class="'.$taxonomy.'-price">'.strip_tags( $price_html ) .'</span><span class="'.$taxonomy.'-'.$term->slug.'">- ('.$option_name.')</span>';} 別的 {$輸出 = ' ' .$option_name;}返回 $output;}返回 $option_name;}
代碼位于活動子主題(或主題)的 function.php 文件或任何插件文件中.
此代碼已經過測試且有效.
生成的html代碼是這樣的:
<div><輸入類型=收音機"name="attribute_pa_color";值=選項藍"id=pa_tab_v_option-blue"><label for="pa_tab_v_option-blue"><span class="pa_tab-price">$99.00</span><span class="pa_tab-option-blue">-(選項藍色)</span>標簽><div><輸入類型=收音機"name="attribute_pa_color";值=選項-綠色"id="pa_tab_v_option-green"><label for="pa_tab_v_option-green"><span class="pa_tab-price">$299.00</span><span class="pa_tab-option-green">-(選項綠色)</span>標簽>
<!-- .../... ... --></td>
<塊引用>因此,您將這個特定的單選按鈕定位為顯示自定義文本的 CSS 規則如下:
span.pa_tab-price {字體系列:拉托,無襯線;字體大小:中等;}span.pa_tab-option-blue, span.pa_tab-option-green,span.pa_tab-option-purple, span.pa_tab-option-orange {字體系列:拉托,無襯線;字體大小:中等;字體樣式:正常;字體粗細:300;}span.pa_tab-option-blue {顏色:藍色;}span.pa_tab-option-green {顏色:綠色;}span.pa_tab-option-purple {顏色:紫色;}span.pa_tab-option-orange {顏色為橙色;}
這只是一個例子
In WooCommerce I am using WC Variations Radio Buttons plugin (by 8manos) to replace the typical dropdown selectors with Radio Buttons.
I've added the below code to my child themes function.php
:
// Display the product variation price inside the variations dropdown.
add_filter( 'woocommerce_variation_option_name', 'display_price_in_variation_option_name' );
function display_price_in_variation_option_name( $term ) {
global $wpdb, $product;
if ( empty( $term ) ) return $term;
if ( empty( $product->id ) ) return $term;
$result = $wpdb->get_col( "SELECT slug FROM {$wpdb->prefix}terms WHERE name = '$term'" );
$term_slug = ( !empty( $result ) ) ? $result[0] : $term;
$query = "SELECT postmeta.post_id AS product_id
FROM {$wpdb->prefix}postmeta AS postmeta
LEFT JOIN {$wpdb->prefix}posts AS products ON ( products.ID = postmeta.post_id )
WHERE postmeta.meta_key LIKE 'attribute_%'
AND postmeta.meta_value = '$term_slug'
AND products.post_parent = $product->id";
$variation_id = $wpdb->get_col( $query );
$parent = wp_get_post_parent_id( $variation_id[0] );
if ( $parent > 0 ) {
$_product = new WC_Product_Variation( $variation_id[0] );
return '' ."<font size='3' face='Lato'>". wp_kses( woocommerce_price( $_product->get_price() ), array() ) . "<font size='3' color='red' face='Lato' style='normal' weight='300'>".' - ('.$term.')';
}
return $term;
}
I've been able to style all four variation names just to see if it was possible. Although, I need each of them to be 4 different colours. That's where I can use some help.
The image below shows what I want (different colours for each "Option"):
Ignore "Color" variation. Just need to modify the "Tab" variation.
For the moment, the variation name in each of the four radio options is 'red', and I would like a different color for each.
What do I have to change in my code, to achieve that?
Thanks
解決方案 2021 Update
Here is your revisited code that will display only around the "Tab" attribute radio buttons custom displayed text a <span>
tag with a different class value based on a combination of the attribute slug and the $term_slug
.
So you will be able to apply some CSS style colors to each radio button displayed custom text for the 'pa_tab' attribute only, adding those CSS rules to your active theme style.css
…
Here is that revisited code:
// Custom function that get the variation id from product attribute option name
function get_variation_id_from_option_name( $term_slug, $taxonomy, $product_id ) {
global $wpdb;
return $wpdb->get_var( $wpdb->prepare( "
SELECT pm.post_id
FROM {$wpdb->prefix}postmeta pm
LEFT JOIN {$wpdb->prefix}posts p ON pm.post_id = p.ID
WHERE pm.meta_key LIKE '%s'
AND pm.meta_value = '%s'
AND p.post_parent = %d
", 'attribute_' . $taxonomy, $term_slug, $product_id ) );
}
// Display the product variation price inside the variations dropdown.
add_filter( 'woocommerce_variation_option_name', 'display_price_in_variation_option_name' );
function display_price_in_variation_option_name( $option_name ) {
global $product;
$taxonomy = 'pa_tab'; // HERE Define the targetted product attribute taxonomy pa_color
$term = get_term_by( 'name', $option_name, $taxonomy );
if ( is_admin() || ! is_a( $term, 'WP_Term' ) || ! is_a( $product, 'WC_Product' ) ) {
return $option_name;
}
$variation_id = get_variation_id_from_option_name( $term->slug, $taxonomy, $product->get_id() );
if ( $variation_id > 0 ) {
$variation = wc_get_product( $variation_id );
$price_html = wc_price( wc_get_price_to_display( $variation ) );
if ( has_term( $option_name, $taxonomy, $product->get_id() ) ) {
$output = ' <span class="'.$taxonomy.'-price">' . strip_tags( $price_html ) . '</span><span class="'.$taxonomy.'-'.$term->slug.'"> - ('.$option_name.')</span>';
} else {
$output = ' ' . $option_name;
}
return $output;
}
return $option_name;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and works.
The generated html code is something like this:
<td class="value">
<div>
<input type="radio" name="attribute_pa_color" value="option-blue" id="pa_tab_v_option-blue">
<label for="pa_tab_v_option-blue">
<span class="pa_tab-price">$99.00</span>
<span class="pa_tab-option-blue"> - (Option Blue)</span>
</label>
</div>
<div>
<input type="radio" name="attribute_pa_color" value="option-green" id="pa_tab_v_option-green">
<label for="pa_tab_v_option-green">
<span class="pa_tab-price">$299.00</span>
<span class="pa_tab-option-green"> - (Option Green)</span>
</label>
</div>
<!-- ... / ... ... -->
</td>
So you target this specific radio buttons displayed custom text with CSS rules something like:
span.pa_tab-price {
font-family: lato, sans-serif;
font-size: medium;
}
span.pa_tab-option-blue, span.pa_tab-option-green,
span.pa_tab-option-purple, span.pa_tab-option-orange {
font-family: lato, sans-serif;
font-size: medium;
font-style: normal;
font-weight: 300;
}
span.pa_tab-option-blue {
color: blue;
}
span.pa_tab-option-green {
color: green;
}
span.pa_tab-option-purple {
color: purple;
}
span.pa_tab-option-orange {
color: orange;
}
This is just an example
這篇關于可變產品屬性:自定義每個顯示的單選按鈕文本值的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!
相關文檔推薦
Add programmatically a downloadable file to Woocommerce products(以編程方式將可下載文件添加到 Woocommerce 產品)
Get today#39;s total orders count for each product in Woocommerce(獲取今天 Woocommerce 中每種產品的總訂單數)
Add Custom registration fields in WooCommerce and phone field validation issue(在 WooCommerce 和電話字段驗證問題中添加自定義注冊字段)
Add a select field that will change price in Woocommerce simple products(在 Woocommerce 簡單產品中添加一個將更改價格的選擇字段)
Add custom columns to admin products list in WooCommerce 3(在 WooCommerce 3 中將自定義列添加到管理產品列表)
Customizing checkout quot;Place Orderquot; button output html(自定義結帳“下訂單按鈕輸出html)
-
• php使用json_encode gbk編碼下漢字...
-
• php解決json gbk編碼中文null問題...
-
• Resource id #3有關問題...
-
• whereJsonContains Laravel 5.6 不起作...
-
• Laravel SwiftMailer:預期響應代碼...
-
• 輸出一個php多維數組到一個...
-
• 使用 php/windows 安裝 ImageMagi...
-
• 覆蓋多個模塊中的 magento 塊...
-
• 確定 Laravel 5 中的文件是否存...
-
• Laravel 5 isDirty() 總是返回 fa...
-
• Laravel 5 Form::model(...) 默認轉義...
-
• Laravel 在用戶級別自定義 se...
-
• php使用json_encode gbk編碼下漢字...
-
• php解決json gbk編碼中文null問題...
-
• Resource id #3有關問題...
-
• whereJsonContains Laravel 5.6 不起作...
-
• Laravel SwiftMailer:預期響應代碼...
-
• 輸出一個php多維數組到一個...
-
• 使用 php/windows 安裝 ImageMagi...
-
• 覆蓋多個模塊中的 magento 塊...
-
• 確定 Laravel 5 中的文件是否存...
-
• Laravel 5 isDirty() 總是返回 fa...
-
• Laravel 5 Form::model(...) 默認轉義...
-
• Laravel 在用戶級別自定義 se...
旅游公司
服裝服飾
機械設備
電子產品
政府協會
網絡營銷
環保科技
科技公司
家政服務
營銷型
環保
軟件開發
傳媒公司
金融服務
雙語
培訓機構
零部件
教育培訓
博客主題
軸承
新聞資訊
視頻
進銷存系統
bootstrap
商城模板
商務合作
廣告設計
驗證碼
門戶
ar
OElove
漫畫網
全景
商城
區塊鏈
虛擬幣
你畫我猜
卡券
動畫特效
在線客服
地板
域名停放
canvas
html5
svg
博客
攝影
導航
小說源碼
幸運抽獎
蘋果cms
微擎微贊
微商
訂單系統
小程序
電影源碼
微信程序
帝國cms
掃碼點餐
jquery
angular
視頻打賞
thinkphp
360
動畫模板
淘寶客
音樂
分發系統
o2o
微擎
主站蜘蛛池模板:
一区不卡在线观看
|
日韩精品一区在线观看
|
国产高清精品一区
|
久久国产成人午夜av影院武则天
|
伊人精品视频
|
久在线视频
|
午夜影视网
|
欧美一区二区在线观看
|
亚洲欧美成人影院
|
91成人午夜性a一级毛片
|
久久久久久国产精品免费免费狐狸
|
国产欧美在线
|
久久一热
|
爱综合|
国产精品嫩草影院精东
|
日韩精品一区二区三区视频播放
|
毛片一区二区三区
|
日韩中文字幕
|
中文字幕av亚洲精品一部二部
|
欧美区日韩区
|
91久操视频
|
日韩一级
|
日韩成年人视频在线
|
中文字幕在线视频观看
|
久久久国产亚洲精品
|
日韩一区二区三区视频
|
亚洲国产欧美91
|
久久久99精品免费观看
|
av网址在线播放
|
四虎影院新地址
|
成人免费高清
|
国产精品视频在线免费观看
|
国产精品亚洲视频
|
中文字幕亚洲区
|
全免一级毛片
|
美女在线视频一区二区三区
|
狠狠插狠狠操
|
精品一区二区三区在线视频
|
av网站免费观看
|
精品一区二区在线观看
|
国产精品视频在线观看
|