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

WooCommerce:從產品變體中獲取自定義字段并將其顯

WooCommerce: Get custom field from product variations and display it on the quot;additional information areaquot;(WooCommerce:從產品變體中獲取自定義字段并將其顯示在“附加信息區域上;) - IT屋-程序員軟件開發技
本文介紹了WooCommerce:從產品變體中獲取自定義字段并將其顯示在“附加信息區域"上;的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在嘗試向產品變體添加自定義字段,并在附加信息區域"中顯示產品的自定義字段值.

I'm trying to add a custom field to product variations, and display the custom field value on products in the "additional information area".

我正在使用 "WooCommerce:為每個產品添加自定義字段變化".

自定義字段工作正常,但我無法在產品頁面上顯示該值.

The custom field is working fine, but I cannot seam to display the value on the product page.

這是我目前所擁有的:

// 1. Add custom field input @ Product Data > Variations > Single Variation
add_action( 'woocommerce_variation_options_pricing', 'Add_custom_field_to_variations', 10, 3 );
function Add_custom_field_to_variations( $loop, $variation_data, $variation ) {  
    woocommerce_wp_text_input( array(
        'id' => 'custom_field[' . $loop . ']',
        'class' => 'short',
        'label' => __( 'Custom Field', 'woocommerce' ),
        'value' => get_post_meta( $variation->ID, 'custom_field', true ) 
    ));
}

// 2. Save custom field on product variation save
add_action( 'woocommerce_save_product_variation', 'Save_custom_field_variations', 10, 2 );
function Save_custom_field_variations( $variation_id, $i ) {
    $custom_field = $_POST['custom_field'][$i];
    if ( isset( $custom_field ) ) {
        update_post_meta( $variation_id, 'custom_field', esc_attr( $custom_field ) );
    }
}

// 3. Store custom field value into variation data
add_filter( 'woocommerce_available_variation', 'Add_custom_field_variation_data' );
function Add_custom_field_variation_data( $variations ) {  
    $variations['custom_field'] = '<div class="woocommerce_custom_field">Custom Field: <span>' . get_post_meta( $variations[ 'variation_id' ], 'custom_field', true ) . '</span></div>';

    return $variations;
}

// 4. Display custom field on the additional information area
function Display_product_attributes2($product_attributes, $variations){
    $product_attributes['custom_field'] = [
        'label' => __('custom', 'text-domain'),
        'value' => get_post_meta( $variation_id, '_custom_field', true ),
    ];
    return $product_attributes;
}
add_filter('woocommerce_display_product_attributes', 'Display_product_attributes2', 10, 2);

推薦答案

At 'value' =>get_post_meta( $variation_id, '_custom_field', true ) 你使用了 $variation_id 而這沒有定義.

At 'value' => get_post_meta( $variation_id, '_custom_field', true ) you make use of the $variation_id while this is not defined.

因此您可以使用 foreach 循環和 $variations->get_children(); 代替添加自定義標簽 &附加信息區"中的值

So you can make use a foreach loop and $variations->get_children(); instead to add a custom label & value in the "additional information area"

// 4. Display custom field on the additional information area
function display_product_attributes( $product_attributes, $variations ) {
    // Get childIDs in an array
    $children_ids = $variations->get_children();

    foreach ( $children_ids as $child_id ) {
        $value = get_post_meta( $child_id, 'custom_field', true );

        // True
        if ( $value ) {
            // rows
            $product_attributes[ 'custom_field ' . $child_id ] = array(
                'label' => __('custom', 'woocommerce'),
                'value' => $value,
            );
        }
    }

    return $product_attributes;
}
add_filter('woocommerce_display_product_attributes', 'display_product_attributes', 10, 2);

這篇關于WooCommerce:從產品變體中獲取自定義字段并將其顯示在“附加信息區域"上;的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Cannot use #39;Object as class name as it is reserved Cake 2.2.x(不能使用 Object 作為類名,因為它是保留的 Cake 2.2.x)
Session is lost after an OAuth redirect(OAuth 重定向后會話丟失)
Pagination Sort in Cakephp 3.x(Cakephp 3.x 中的分頁排序)
CakePHP Shared core for multiple apps(CakePHP 多個應用程序的共享核心)
Login [ Auth-gt;identify() ] always false on CakePHP 3(在 CakePHP 3 上登錄 [ Auth-identify() ] 始終為 false)
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 87 bytes)(致命錯誤:允許的內存大小為 134217728 字節已用盡(嘗試分配 87 字節))
主站蜘蛛池模板: 精品乱人伦一区二区三区 | 国产中文字幕在线 | 亚洲精品9999 | 国产精品亚洲综合 | 免费同性女女aaa免费网站 | 国产一区二区三区久久久久久久久 | 亚洲精品久久久久久一区二区 | 日韩av中文 | 麻豆av在线免费观看 | 拍真实国产伦偷精品 | 亚洲精品毛片av | 91精品国产美女在线观看 | 97av在线| 中文字幕亚洲区一区二 | 欧美一区二区成人 | www.久久久久久久久久久久 | 国产精品一区一区三区 | 91精品国产91久久久久久吃药 | 亚洲成人精选 | 91精品国产综合久久久久久丝袜 | 黄色骚片| 久久久国产一区二区三区 | 国产欧美三区 | 久久综合狠狠综合久久综合88 | 日韩欧美一级精品久久 | 雨宫琴音一区二区在线 | 免费在线观看成人av | 日韩视频在线播放 | 国产成人精品亚洲日本在线观看 | 91国内在线观看 | 国产免费视频 | 国产最新视频在线 | 中文字幕在线网 | 国产99在线 | 欧美 | 欧美激情综合五月色丁香小说 | 黄色一级大片在线免费看产 | av网站在线免费观看 | www.国产一区 | 亚洲午夜精品视频 | 美女国内精品自产拍在线播放 | 水蜜桃久久夜色精品一区 |