久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

將變化價格添加到 Woocommerce 中的可變產品下拉項

Add the variation price to variable product dropdown item names in Woocommerce(將變化價格添加到 Woocommerce 中的可變產品下拉項目名稱)
本文介紹了將變化價格添加到 Woocommerce 中的可變產品下拉項目名稱的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我正在使用此代碼獲取可變產品選項

I'm using this code to get the variable product options

$terms = wc_get_product_terms( $bundle_product_id, $name, array( 'fields' => 'all' ) );
foreach ( $terms as $term ) {
    if ( !in_array( $term->slug, $options ) ) {
        continue;
    }
    echo '<option value="' . esc_attr( $term->slug ) . '" ' . selected( sanitize_title( $selected_value ), sanitize_title( $term->slug ), false ) . '>' . apply_filters( 'woocommerce_variation_option_name', $term->name )  . '</option>';
}

我嘗試了很多代碼來獲取下拉菜單中名稱旁邊的價格,但都沒有正確

I tried a lot of codes to get price next to the name in dropdown menu , but nothing get right

推薦答案

只有當您在變量 product (因此只有一個下拉菜單)中只設置了一個變體的產品屬性時,它才會起作用.

如果您的變量產品中有多個下拉菜單,由于變體是不同產品屬性值的組合,因此邏輯上將無法正常工作.

If you have more than one dropdowns in your variable product, as the variations are a combination of the different product attributes values, it will not work logically.

因此,以下代碼將在唯一產品屬性下拉列表中顯示產品變體價格:

So the following code will display the product variation price in a unique product attribute dropdown:

// Utility function to get the price of a variation from it's attribute value
function get_the_variation_price_html( $product, $name, $term_slug ){
    foreach ( $product->get_available_variations() as $variation ){
        if($variation['attributes'][$name] == $term_slug ){
            return strip_tags( $variation['price_html'] );
        }
    }
}

// Add the price  to the dropdown options items.
add_filter( 'woocommerce_dropdown_variation_attribute_options_html', 'show_price_in_attribute_dropdown', 10, 2);
function show_price_in_attribute_dropdown( $html, $args ) {
    // Only if there is a unique variation attribute (one dropdown)
    if( sizeof($args['product']->get_variation_attributes()) == 1 ) :

    $options               = $args['options'];
    $product               = $args['product'];
    $attribute             = $args['attribute'];
    $name                  = $args['name'] ? $args['name'] : 'attribute_' . sanitize_title( $attribute );
    $id                    = $args['id'] ? $args['id'] : sanitize_title( $attribute );
    $class                 = $args['class'];
    $show_option_none      = $args['show_option_none'] ? true : false;
    $show_option_none_text = $args['show_option_none'] ? $args['show_option_none'] : __( 'Choose an option', 'woocommerce' );

    if ( empty( $options ) && ! empty( $product ) && ! empty( $attribute ) ) {
        $attributes = $product->get_variation_attributes();
        $options    = $attributes[ $attribute ];
    }

    $html = '<select id="' . esc_attr( $id ) . '" class="' . esc_attr( $class ) . '" name="' . esc_attr( $name ) . '" data-attribute_name="attribute_' . esc_attr( sanitize_title( $attribute ) ) . '" data-show_option_none="' . ( $show_option_none ? 'yes' : 'no' ) . '">';
    $html .= '<option value="">' . esc_html( $show_option_none_text ) . '</option>';

    if ( ! empty( $options ) ) {
        if ( $product && taxonomy_exists( $attribute ) ) {
            $terms = wc_get_product_terms( $product->get_id(), $attribute, array( 'fields' => 'all' ) );

            foreach ( $terms as $term ) {
                if ( in_array( $term->slug, $options ) ) {
                    // Get and inserting the price
                    $price_html = get_the_variation_price_html( $product, $name, $term->slug );
                    $html .= '<option value="' . esc_attr( $term->slug ) . '" ' . selected( sanitize_title( $args['selected'] ), $term->slug, false ) . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $term->name ) . ' :::?' . $price_html ) . '</option>';
                }
            }
        } else {
            foreach ( $options as $option ) {
                $selected = sanitize_title( $args['selected'] ) === $args['selected'] ? selected( $args['selected'], sanitize_title( $option ), false ) : selected( $args['selected'], $option, false );
                // Get and inserting the price
                $price_html = get_the_variation_price_html( $product, $name, $term->slug );
                $html .= '<option value="' . esc_attr( $option ) . '" ' . $selected . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $option ) . ' :::?' . $price_html ) . '</option>';
            }
        }
    }
    $html .= '</select>';

    endif;

    return $html;
}

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

Code goes in function.php file of your active child theme (or active theme). Tested and works.

這篇關于將變化價格添加到 Woocommerce 中的可變產品下拉項目名稱的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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)
主站蜘蛛池模板: 一区视频 | 97视频在线观看网站 | 久久久精品一区二区三区 | 四虎影院在线播放 | 男插女下体视频 | 国产精品视频一 | 欧美1区2区 | 91 久久| 日韩视频中文字幕 | 伊人看片 | 亚洲一区二区久久 | 国产高清在线精品一区二区三区 | 国产资源视频 | 韩国成人在线视频 | 视频一区在线 | 成人三级av| 亚洲精品在线视频 | 91精品中文字幕一区二区三区 | 亚洲精品久久久一区二区三区 | 精品国产1区2区3区 在线国产视频 | 国产在线视频在线观看 | 国产乱码精品一区二区三区忘忧草 | 久久免费精品视频 | 国产1区 | 九九亚洲 | 久久精品亚洲欧美日韩久久 | 久久免费看 | 国产精品1区2区 | 久久精品国产免费一区二区三区 | 午夜精品福利视频 | 久久精品色欧美aⅴ一区二区 | 久久蜜桃资源一区二区老牛 | 亚洲精品一区久久久久久 | 欧美精品一区二区三区在线播放 | 国产亚洲欧美在线视频 | 久久久婷婷 | 国产伦精品一区二区三区高清 | 999精品在线观看 | 欧美精品一区二区三区在线播放 | 男女视频在线免费观看 | 亚洲一区视频 |