mustardBees / cmb-field-select2

Select2 field type for Custom Metaboxes and Fields for WordPress
93 stars 46 forks source link

Drag and drop for pw_multiselect does not work #38

Closed fakeartist closed 7 years ago

fakeartist commented 7 years ago

Hi! I want to add a select2 multiselect field in which I want to get all my WooCommerce site's products.

I use the code below

$single_post_metaboxes->add_field(array(
    'name' => __('Select products', 'cmb2'),
    'id' => $prefix . 'products',
    'type' => 'pw_multiselect',
    'options' => get_products(array('post_type' => 'product')),
));

function get_products($query_args = array()) {
$defaults = array(
    'posts_per_page' => -1
);
$query = new WP_Query(array_replace_recursive($defaults, $query_args));
return wp_list_pluck($query->get_posts(), 'post_title', 'ID');

}

My problems are:

  1. The drag and drop doesn't work.
  2. When I save the field, it reorders the items inside.

I have CMB2 version 2.2.3.1 and this plugins' version 3.0.3.

Any idea what could be the problem?

Thanks in advance for any suggestions!

mustardBees commented 7 years ago

Hi @fakeartist. I've tested this combination of versions. Seems to be working as expected.

Have you got any console errors, any JS issues which might have a knock-on effect?

fakeartist commented 7 years ago

Hi @mustardBees and thanks for the quick answer! No JS errors appear on my console.

I also tested the code at a clean WordPress site with the latest version of WordPress (4.7.2) and WooCommerce (2.6.14). The only plugin activated is WooCommerce. I run the test code on twenty fifteen theme. The problem persists. I'm buffled!

fakeartist commented 7 years ago

Update: In your style.css, you have add the move cursor with

.cmb-type-pw-multiselect .select2-selection__choice {
    cursor: move !important;
}

In my case, I do not see a select2-selection__choise class in the html. What I see, is a select2-search-choice class. So I can show the move curson by using this:

.cmb-type-pw-multiselect .select2-search-choice,
.cmb-type-pw-multiselect .select2-selection__choice {
    cursor: move !important;
}

Does this help?

fakeartist commented 7 years ago

I also see in the console, that WooCommerce is loading in the admin the select2 library version 3.5.4. This plugin uses 4.0.3. Maybe there's a conflict?

mustardBees commented 7 years ago

@fakeartist I was able to replicate the issue when a meta box was associated with the WooCommerce product post type. As you suspected, the conflict appears to be between the version of Select2 loaded by this field and the older version of Select2 loaded by WooCommerce.

This will hopefully resolve itself when WooCommerce 2.7 drops.