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

在 Woocommerce 中添加自定義多選字段以管理產品選

Add a custom multi-select field to admin product options settings in Woocommerce(在 Woocommerce 中添加自定義多選字段以管理產品選項設置)
本文介紹了在 Woocommerce 中添加自定義多選字段以管理產品選項設置的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我遵循了這個答案

這是我的代碼:

//在鏈接產品"部分顯示自定義字段add_action('woocommerce_product_options_related', 'woocom_linked_products_data_custom_field');//保存到自定義字段add_action('woocommerce_process_product_meta', 'woocom_linked_products_data_custom_field_save');//生成自定義字段的函數函數 woocom_linked_products_data_custom_field() {全球 $woocommerce, $post;$product = wc_get_product( $post->ID );?><p class="form-field"><label for="subscription_toggle_product"><?php _e('訂閱切換產品', 'woocommerce');?></label><select class="wc-product-search" style="width: 50%;"id="subscription_toggle_product" name="subscription_toggle_product" data-placeholder="<?php esc_attr_e('搜索產品&hellip;', 'woocommerce'); ?>"data-action="woocommerce_json_search_products_and_variations" data-exclude="<?php echo intval( $post->ID ); ?>"><?php$product_id = get_post_meta( $post->ID, '_subscription_toggle_product_id', true );如果($product_id){$product = wc_get_product( $product_id );如果 ( is_object( $product ) ) {echo '<option value="' . esc_attr( $product_id ) . '"' .選擇(真,真,假).'>'.wp_kses_post( $product->get_formatted_name() ) .'</選項>';}}?></選擇></p><?php}//函數保存自定義字段函數 woocom_linked_products_data_custom_field_save( $post_id ){如果(isset($_POST['subscription_toggle_product'])){$product_field_type = $_POST['subscription_toggle_product'];update_post_meta( $post_id, '_subscription_toggle_product_id', $product_field_type );}}

解決方案

這種選擇字段僅適用于定義的 multiple 屬性和值數組.所以你不能將它用于簡單的 ID.如果您添加到您的選擇字段 multiple="multiple" 屬性,它將起作用.

此外,自從 Woocommerce 3 事情發生了變化:
- 有更好的鉤子來保存數據.
- 您現在可以使用 CRUD 對象和相關方法.

以下代碼適用于多個產品 ID(產品 ID 數組):

//在鏈接產品"部分顯示自定義選擇字段add_action('woocommerce_product_options_related', 'display_linked_products_data_custom_field');函數 display_linked_products_data_custom_field() {全球 $product_object, $post;?><p class="form-field"><label for="subscription_toggle_products"><?php _e( '訂閱切換產品', 'woocommerce');?></label><select class="wc-product-search" multiple="multiple" style="width: 50%;"id="subscription_toggle_ids" name="_subscription_toggle_ids[]" data-placeholder="<?php esc_attr_e('搜索產品&hellip;', 'woocommerce'); ?>"data-action="woocommerce_json_search_products_and_variations" data-exclude="<?php echo intval( $post->ID ); ?>"><?php$product_ids = $product_object->get_meta('_subscription_toggle_ids');foreach ( $product_ids 作為 $product_id ) {$product = wc_get_product( $product_id );如果 ( is_object( $product ) ) {echo '<option value="' . esc_attr( $product_id ) . '"' .選擇(真,真,假).'>'.wp_kses_post( $product->get_formatted_name() ) .'</選項>';}}?></選擇></p><?php}//將值保存到產品中add_action('woocommerce_admin_process_product_object', 'save_linked_products_data_custom_field_value', 10, 1);函數 save_linked_products_data_custom_field_value( $product ){$data = isset( $_POST['_subscription_toggle_ids'] ) ?array_map('intval', (array) $_POST['_subscription_toggle_ids']): array();$product->update_meta_data('_subscription_toggle_ids', $data);}

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

I have followed this answer How to add more custom field in Linked Product of Woocommerce to add a custom select field to my Linked Product screen in Woocommerce. This new field is meta key is subscription_toggle_product.

It's working fine, but once you have selected a product, you can't delete it (there is no "Empty" option or cross symbol). I haven't got a clue how I can allow the selection to be deselected. I've tried adding an <option> with an empty value, but it hasn't worked.

Here is my code:

// Display the custom fields in the "Linked Products" section
add_action( 'woocommerce_product_options_related', 'woocom_linked_products_data_custom_field' );

// Save to custom fields
add_action( 'woocommerce_process_product_meta', 'woocom_linked_products_data_custom_field_save' );

// Function to generate the custom fields
function woocom_linked_products_data_custom_field() {
    global $woocommerce, $post;
    $product = wc_get_product( $post->ID );
?>
<p class="form-field">
    <label for="subscription_toggle_product"><?php _e( 'Subscription Toggle Product', 'woocommerce' ); ?></label>
    <select class="wc-product-search" style="width: 50%;" id="subscription_toggle_product" name="subscription_toggle_product" data-placeholder="<?php esc_attr_e( 'Search for a product&hellip;', 'woocommerce' ); ?>" data-action="woocommerce_json_search_products_and_variations" data-exclude="<?php echo intval( $post->ID ); ?>">
        <?php

            $product_id = get_post_meta( $post->ID, '_subscription_toggle_product_id', true );            

            if ( $product_id ) {
                $product = wc_get_product( $product_id );
                if ( is_object( $product ) ) {
                    echo '<option value="' . esc_attr( $product_id ) . '"' . selected( true, true, false ) . '>' . wp_kses_post( $product->get_formatted_name() ) . '</option>';
                }
            }

        ?>
    </select>
</p>

<?php
}

// Function the save the custom fields
function woocom_linked_products_data_custom_field_save( $post_id ){
    if (isset($_POST['subscription_toggle_product'])) {
        $product_field_type =  $_POST['subscription_toggle_product'];
        update_post_meta( $post_id, '_subscription_toggle_product_id', $product_field_type );
    }
}

解決方案

This kind of select field only works with a defined multiple attribute and work with an array of values. so you can't use it for a simple ID. If you add to your select field multiple="multiple" attribute it will work.

Also since Woocommerce 3 things have changed:
- There are better hooks to save the data.
- You can now use CRUD Objects and related methods.

The following code will work for multiple product IDs (an array of products IDs):

// Display a custom select field in "Linked Products" section
add_action( 'woocommerce_product_options_related', 'display_linked_products_data_custom_field' );
function display_linked_products_data_custom_field() {
    global $product_object, $post;
    ?>
    <p class="form-field">
        <label for="subscription_toggle_products"><?php _e( 'Subscription Toggle Products', 'woocommerce' ); ?></label>
        <select class="wc-product-search" multiple="multiple" style="width: 50%;" id="subscription_toggle_ids" name="_subscription_toggle_ids[]" data-placeholder="<?php esc_attr_e( 'Search for a product&hellip;', 'woocommerce' ); ?>" data-action="woocommerce_json_search_products_and_variations" data-exclude="<?php echo intval( $post->ID ); ?>">
            <?php
                $product_ids = $product_object->get_meta( '_subscription_toggle_ids' );

                foreach ( $product_ids as $product_id ) {
                    $product = wc_get_product( $product_id );
                    if ( is_object( $product ) ) {
                        echo '<option value="' . esc_attr( $product_id ) . '"' . selected( true, true, false ) . '>' . wp_kses_post( $product->get_formatted_name() ) . '</option>';
                    }
                }
            ?>
        </select>
    </p>
    <?php
}

// Save the values to the product
add_action( 'woocommerce_admin_process_product_object', 'save_linked_products_data_custom_field_value', 10, 1 );
function save_linked_products_data_custom_field_value( $product ){
    $data = isset( $_POST['_subscription_toggle_ids'] ) ? array_map( 'intval', (array) $_POST['_subscription_toggle_ids'] ) : array();
    $product->update_meta_data( '_subscription_toggle_ids', $data );
}

Code goes in function.php file of your active child theme (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)
主站蜘蛛池模板: 免费观看一级毛片视频 | 日韩精品一区二区三区 | 久久一区二区免费视频 | 亚洲精品日日夜夜 | 国产免费麻豆视频 | 久久一区二区三区免费 | 一区二区三区欧美在线观看 | 欧美男人天堂 | 国产精品久久久久久久久久软件 | 久久黄色精品视频 | 亚洲va国产日韩欧美精品色婷婷 | 日本理论片好看理论片 | www.日韩| 国产成人综合久久 | 国产十日韩十欧美 | 久久精品欧美一区二区三区麻豆 | 成人av网站在线观看 | 一区二区不卡 | 成人在线国产 | 亚洲免费视频在线观看 | 在线一区观看 | 999免费网站 | 中文字幕成人av | 中文字幕av中文字幕 | 神马久久久久久久久久 | 午夜国产 | 亚洲电影专区 | 九九热精品在线 | 99久久久久久 | 99热精品久久 | 福利视频网 | 中文字幕久久久 | 91视频入口 | 中文字幕av在线一二三区 | 国产欧美在线视频 | 91色在线 | 国产中文区二幕区2012 | 欧美精品欧美精品系列 | 久久久久久久网 | 婷婷开心激情综合五月天 | 中文字幕一区在线观看视频 |