pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

可變產品屬性:自定義每個顯示的單選按鈕文本值

Variable product attribute: Customizing each displayed radio buttons text value(可變產品屬性:自定義每個顯示的單選按鈕文本值)
本文介紹了可變產品屬性:自定義每個顯示的單選按鈕文本值的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

在 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)
主站蜘蛛池模板: 微型气象仪_气象传感器_防爆气象传感器-天合传感器大全 | 防水套管厂家-柔性防水套管-不锈钢|刚性防水套管-天翔管道 | 气动球阀_衬氟蝶阀_调节阀_电动截止阀_上海沃托阀门有限公司 | 世界箱包品牌十大排名,女包小众轻奢品牌推荐200元左右,男包十大奢侈品牌排行榜双肩,学生拉杆箱什么品牌好质量好 - Gouwu3.com | 等离子表面处理机-等离子表面活化机-真空等离子清洗机-深圳市东信高科自动化设备有限公司 | 大数据营销公司_舆情监测软件_上海SEO公司-文军营销官网 | 芜湖厨房设备_芜湖商用厨具_芜湖厨具设备-芜湖鑫环厨具有限公司 控显科技 - 工控一体机、工业显示器、工业平板电脑源头厂家 | 砂石生产线_石料生产线设备_制砂生产线设备价格_生产厂家-河南中誉鼎力智能装备有限公司 | 天津试验仪器-电液伺服万能材料试验机,恒温恒湿标准养护箱,水泥恒应力压力试验机-天津鑫高伟业科技有限公司 | 广西绿桂涂料--承接隔热涂料、隔音涂料、真石漆、多彩仿石漆等涂料工程双包施工 | 云南丰泰挖掘机修理厂-挖掘机维修,翻新,再制造的大型企业-云南丰泰工程机械维修有限公司 | 转子泵_凸轮泵_凸轮转子泵厂家-青岛罗德通用机械设备有限公司 | 分光色差仪,测色仪,反透射灯箱,爱色丽分光光度仪,美能达色差仪维修_苏州欣美和仪器有限公司 | 通辽信息港 - 免费发布房产、招聘、求职、二手、商铺等信息 www.tlxxg.net | 粘度计,数显粘度计,指针旋转粘度计 | 视频直播 -摄影摄像-视频拍摄-直播分发 | atcc网站,sigma试剂价格,肿瘤细胞现货,人结肠癌细胞株购买-南京科佰生物 | 螺杆真空泵_耐腐蚀螺杆真空泵_水环真空泵_真空机组_烟台真空泵-烟台斯凯威真空 | Type-c防水母座|贴片母座|耳机接口|Type-c插座-深圳市步步精科技有限公司 | 澳门精准正版免费大全,2025新澳门全年免费,新澳天天开奖免费资料大全最新,新澳2025今晚开奖资料,新澳马今天最快最新图库-首页-东莞市傲马网络科技有限公司 | 海水晶,海水素,海水晶价格-潍坊滨海经济开发区强隆海水晶厂 | 昆明化妆培训-纹绣美甲-美容美牙培训-昆明博澜培训学校 | 安全阀_弹簧式安全阀_美标安全阀_工业冷冻安全阀厂家-中国·阿司米阀门有限公司 | 蜘蛛车-高空作业平台-升降机-高空作业车租赁-臂式伸缩臂叉装车-登高车出租厂家 - 普雷斯特机械设备(北京)有限公司 | 正压密封性测试仪-静态发色仪-导丝头柔软性测试仪-济南恒品机电技术有限公司 | 中山市派格家具有限公司【官网】| 液晶拼接屏厂家_拼接屏品牌_拼接屏价格_监控大屏—北京维康 | 哈尔滨治「失眠/抑郁/焦虑症/精神心理」专科医院排行榜-京科脑康免费咨询 一对一诊疗 | 厂房出售_厂房仓库出租_写字楼招租_土地出售-中苣招商网-中苣招商网 | NBA直播_NBA直播免费观看直播在线_NBA直播免费高清无插件在线观看-24直播网 | 温州富欧金属封头-不锈钢封头厂家 | 高压油管,液压接头,液压附件-烟台市正诚液压附件 | 碳化硅,氮化硅,冰晶石,绢云母,氟化铝,白刚玉,棕刚玉,石墨,铝粉,铁粉,金属硅粉,金属铝粉,氧化铝粉,硅微粉,蓝晶石,红柱石,莫来石,粉煤灰,三聚磷酸钠,六偏磷酸钠,硫酸镁-皓泉新材料 | [品牌官网]贵州遵义双宁口腔连锁_贵州遵义牙科医院哪家好_种植牙_牙齿矫正_原华美口腔 | 合肥活动房_安徽活动板房_集成打包箱房厂家-安徽玉强钢结构集成房屋有限公司 | 温泉机设备|温泉小镇规划设计|碳酸泉设备 - 大连连邦温泉科技 | 高尔夫球杆_高尔夫果岭_高尔夫用品-深圳市新高品体育用品有限公司 | 防火板_饰面耐火板价格、厂家_品牌认准格林雅 | 代理记账_公司起名核名_公司注册_工商注册-睿婕实业有限公司 | 铣刨料沥青破碎机-沥青再生料设备-RAP热再生混合料破碎筛分设备 -江苏锡宝重工 | 特种电缆厂家-硅橡胶耐高温电缆-耐低温补偿导线-安徽万邦特种电缆有限公司 |