可搜索的多种产品选择Woocommerce的自定义字段 [英] Searchable multiple product select custom field for Woocommerce

查看:64
本文介绍了可搜索的多种产品选择Woocommerce的自定义字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个插件,需要在其中显示一些自定义的精选产品.到目前为止,我可以制作选项字段,但如何将它们另存为带有逗号分隔产品ID的选项字段.

I am developing a plugin where I need to display some custom select product. So far I can able to make the option field but how can i save them as option field with comma separated product ids like.

45,78,55,48, 

这是WooCommerce产品的可搜索多项选择选项的示例.

here is an example of searchable multiple select option for WooCommerce product.

这是我的代码

function crp_select_products() {
    global $post, $woocommerce;
    $product_ids = array();
    ?>
    <div class="options_group">
        <?php if ( $woocommerce->version >= '3.0' ) : ?>
            <p class="form-field">
                <label for="related_ids"><?php _e( 'Search Products', 'woocommerce' ); ?></label>
                <select class="wc-product-search" multiple="multiple" style="width: 50%;" id="related_ids" name="related_ids[]" data-placeholder="<?php esc_attr_e( 'Search for a product&hellip;', 'woocommerce' ); ?>" data-action="woocommerce_json_search_products_and_variations">
                    <?php
                        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> <?php echo wc_help_tip( __( 'Select products are for sale product.', 'woocommerce' ) ); ?>
            </p>
        <?php endif; ?>
    </div>
    <?php
}

推荐答案

首先,您的函数中缺少某些内容,无法在其中显示保存的数据.

First, there is something missing in your function, to display the saved data in it.

此后,需要在具有提交按钮的表单内显示此特殊字段.因此,这取决于您在哪里使用函数.

After, this special field need to be displayed inside a form that will have a submit button. So it depends where you are using your function.

以下是显示该自定义字段作为自定义产品设置,保存数据并在其中显示保存的数据的示例:

Here below is an example displaying that custom field as a custom product setting, save the data and display the saved data in it:

function crp_get_product_related_ids() {
    global $post, $woocommerce;

    $product_ids = get_post_meta( $post->ID, '_related_ids', true );
    if( empty($product_ids) )
        $product_ids = array();
    ?>
    <div class="options_group">
        <?php if ( $woocommerce->version >= '3.0' ) : ?>
            <p class="form-field">
                <label for="related_ids"><?php _e( 'Search Products', 'woocommerce' ); ?></label>
                <select class="wc-product-search" multiple="multiple" style="width: 50%;" id="related_ids" name="related_ids[]" data-placeholder="<?php esc_attr_e( 'Search for a product&hellip;', 'woocommerce' ); ?>" data-action="woocommerce_json_search_products_and_variations">
                    <?php
                        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> <?php echo wc_help_tip( __( 'Select products are for sale product.', 'woocommerce' ) ); ?>
            </p>
        <?php endif; ?>
    </div>
    <?php
}

add_action( 'woocommerce_product_options_general_product_data', 'add_custom_fied_in_product_general_fields', 20 );
function add_custom_fied_in_product_general_fields() {
    global $post, $woocommerce;
    crp_get_product_related_ids();
}


add_action( 'woocommerce_process_product_meta', 'process_product_meta_custom_fied', 20, 1 );
function process_product_meta_custom_fied( $product_id ){
    if( isset( $_POST['crosssell_ids'] ) ){
        update_post_meta( $product_id, '_related_ids', array_map( 'intval', (array) wp_unslash( $_POST['related_ids'] ) ) );
    }
}

代码进入您的活动子主题(或活动主题)的function.php文件中.经过测试,可以正常工作.

Code goes in function.php file of your active child theme (or active theme). Tested and works.

这篇关于可搜索的多种产品选择Woocommerce的自定义字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆