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

莎拉

我已遵循此答案,如何在Woocommerce的链接产品中添加更多自定义字段,以将自定义选择字段添加到Woocommerce的我的链接产品屏幕中。这个新字段是meta键subscription_toggle_product

它工作正常,但是一旦选择了产品,就无法将其删除(没有“空”选项或叉号)。我不知道如何取消选择。我尝试添加<option>带有空值的,但没有成功。

如何允许取消选择?

这是我的代码:

// 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 );
    }
}
LoicTheAztec

这种选择字段仅适用于已定义的multiple属性,并且适用于值数组。因此您不能将其用于简单的ID。如果将其添加到选择字段multiple="multiple"属性中,它将起作用。

另外,自Woocommerce 3以来,情况有所变化:
-有更好的挂钩来保存数据。
-您现在可以使用CRUD对象和相关方法。

以下代码适用于多个产品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 );
}

代码进入您的活动子主题(活动主题)的function.php文件中。经过测试和工作。

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何将自定义字段添加到woocommerce产品属性

将自定义字段添加到woocommerce产品中的特定类别

在WooCommerce中将自定义标签添加到特定产品类型的单个产品页面

WooCommerce已将自定义字段添加到其免费插件中吗?

woocommerce将自定义结帐字段添加到电子邮件

在Podspec中将自定义模块添加到目标构建设置

将自定义字段数据添加到WooCommerce订单

WooCommerce:将自定义Metabox添加到管理订单页面

将自定义维度字段添加到变量产品的每个变体设置中

如果产品在WooCommerce中缺货,则将自定义类添加到链接

在WooCommerce中将自定义URL链接添加到管理订单列表页面

将自定义字段添加到“其他信息”选项卡(WooCommerce)

将自定义产品计算价格添加到Woocommerce购物车

将自定义字段添加到产品设置运送选项卡,并在WooCommerce的其他信息选项卡上显示值

在WooCommerce 3中将自定义列添加到管理产品列表

将产品自定义字段添加到WooCommerce中的“管理产品”批量编辑表单中

在Woocommerce 3中将自定义批量操作添加到管理订单列表

在Woocommerce 3中将自定义元数据添加到管理订单编辑页面

在WooCommerce中将自定义函数添加到WC_Order

在Woocommerce中将自定义“销售价格”列添加到管理产品列表

如何在Silvershop中修改产品(将自定义字段添加到$ db)

在WooCommerce中将自定义结帐字段值添加到客户订单注释中

在WooCommerce中将自定义占位符添加到电子邮件主题

将自定义ajax按钮添加到WooCommerce管理订单列表

将自定义字段添加到WooCommerce新帐户页面

如何在NetSuite中将自定义字段添加到发票

如何在Woocommerce中将自定义字段添加到类别中?

在WooCommerce中将“相关产品”添加到自定义选项卡

将自定义字段添加到基本客户端集成选项