久久久久久久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)
主站蜘蛛池模板: 国产一区电影 | 欧美在线观看一区 | 成人视屏在线观看 | 一区在线免费视频 | 精品久久久久久亚洲精品 | 欧美电影一区 | 久久久久久久久99 | www.国产精 | 国产欧美一区二区三区久久手机版 | 黄色毛片在线看 | 亚洲一区二区中文字幕 | 99久久久无码国产精品 | 成人在线 | 国产在线一区二区三区 | 一级一级毛片免费看 | 国产精品揄拍一区二区 | 天天玩天天干天天操 | www.操.com | 久久国产精品99久久久久 | 日韩三级电影一区二区 | 欧美日韩福利视频 | 黄色在线免费观看视频 | 99久久久无码国产精品 | 亚洲国产成人av好男人在线观看 | 日韩精品一区二区三区中文在线 | 亚洲精彩视频 | 精品一区二区电影 | 亚洲一区精品视频 | 精品在线看 | 久久久久久国产精品 | 国产精品毛片一区二区在线看 | 成人亚洲网站 | 国产欧美在线播放 | 久久久国产一区二区三区四区小说 | 亚洲欧美在线一区 | 精品av| 一区二区成人 | av在线播放一区二区 | 亚洲午夜在线 | 国产高清视频在线观看播放 | 精品一区二区观看 |