简体   繁体   English

在 Woocommerce 中添加自定义多选字段以管理产品选项设置

[英]Add a custom multi-select field to admin product options settings in Woocommerce

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.我遵循了这个答案How to add more custom field in Linked Product of Woocommerce将自定义选择字段添加到我在 Woocommerce 的链接产品屏幕上。 This new field is meta key is subscription_toggle_product .这个新字段的元键是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.我试过添加一个带有空值的<option> ,但它没有用。

如何允许取消选择?

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.这种选择字段仅适用于定义的multiple属性并使用值数组。 so you can't use it for a simple ID.所以你不能将它用于简单的 ID。 If you add to your select field multiple="multiple" attribute it will work.如果您添加到您的选择字段multiple="multiple"属性,它将起作用。

Also since Woocommerce 3 things have changed:此外,自从 Woocommerce 3 事情发生了变化:
- There are better hooks to save the data. - 有更好的钩子来保存数据。
- You can now use CRUD Objects and related methods. - 您现在可以使用 CRUD 对象和相关方法。

The following code will work for multiple product IDs (an array of products IDs):以下代码适用于多个产品 ID(产品 ID 数组):

// 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).代码位于活动子主题(活动主题)的 function.php 文件中。 Tested and works.测试和工作。

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 WooCommerce 产品类别设置中为精选帖子添加多选字段 - Add a multi-select field for featured Posts in WooCommerce Product Category settings 在 Woocommerce 中添加自定义结帐多选字段并更新订单元 - Add a custom checkout multi-select field and update order meta in Woocommerce 在 WooCommerce 中向管理产品批量编辑表单添加产品自定义字段 - Add a product custom field to Admin product bulk edit form in WooCommerce 在woocommerce管理订单页面中添加产品属性或自定义字段 - Add product attribute or custom field in woocommerce admin order page 如何在管理员的 Woocommerce 产品中添加自定义字段以上传 PDF 文件 - How to add custom field for upload PDF files in Woocommerce product in admin 在 Woocommerce 中添加和保存管理产品变体自定义字段 - Add and save admin product variations custom field in Woocommerce WooCommerce 添加到购物车,从自定义 select 字段添加第二个产品 - WooCommerce Add to cart, add second product from a custom select field 单个产品字段的 WooCommerce 多选 - WooCommerce Multi Select for Single Product Field 以编程方式添加自定义设置选项卡以管理 WooCommerce 中的产品数据 - Adding programmatically a custom settings tab to admin product data in WooCommerce 通过 WooCommerce 产品设置中的自定义复选框将 class 添加到 WooCommerce 页面 - Add class to WooCommerce pages via custom checkbox in WooCommerce product settings
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM