Firefog
Firefog

Reputation: 3174

Searchable multiple product select custom field for Woocommerce

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, 

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

enter image description here

Here is my code

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
}

Upvotes: 1

Views: 2941

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 254363

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'] ) ) );
    }
}

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

enter image description here

Upvotes: 4

Related Questions