Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: Option 'ajax' is not allowed for Select2 when attached to a <select> element woocommerce

any body help me wasted too much time on this error but no luck

Error: Option 'ajax' is not allowed for Select2 when attached to a element.

this error show in my developer console after updating woocommerce to 3.0.3 this error broken some of functionalities like could not add the upsell and cross up text boxed are noting.

When I chnaged my theme then this go away and I can't change my theme.

Please help.

like image 779
Zain Ali Avatar asked May 09 '17 09:05

Zain Ali


1 Answers

I had the same error in my wordpress website, and I fixed that so.

1st I want to describe where this error comes from. From WC Beta 2, they've migrated to Select2 V4. Select2 V4 is mostly compatible with Select2 V3 with a few exceptions, the main one being how AJAX search inputs work. WooCommerce has two instances of these which are affected and need some HTML markup changes to function. For Example

<input type="hidden" id="grant_access_id" name="grant_access_id" data-multiple="true" class="wc-product-search" style="width: 400px;" data-placeholder="<?php esc_attr_e( 'Search for a downloadable product&hellip;', 'woocommerce' ); ?>" data-action="woocommerce_json_search_downloadable_products_and_variations" />

was changed with

<select id="grant_access_id" class="wc-product-search" name="grant_access_id[]" multiple="multiple" style="width: 400px;" data-placeholder="<?php esc_attr_e( 'Search for a downloadable product&hellip;', 'woocommerce' ); ?>" data-action="woocommerce_json_search_downloadable_products_and_variations"></select>

In wordpress I saw that select2 takes control, and I tried to disable select2 load in my functions.php. You can see my script below

add_action( 'admin_enqueue_scripts', 'remove_views_select2' );

function remove_views_select2($hook) {
    if ( ( $hook == 'post.php' || $hook == 'post-new.php' ) ) {
        wp_deregister_script( 'select2' );
        // wp_register_script( 'views-select2-script' , 'http://your-site.com/wp-content/plugins/meta-box/js/select2/select2.min.js', array('jquery'), 3.2);
    }
}

So that works excellent in my case. Good luck ! 😉

like image 117
Rafayel Armeni Avatar answered Nov 10 '22 01:11

Rafayel Armeni