mustardBees / cmb-field-select2

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

Support for Repeatable Fields #10

Closed bryceadams closed 8 years ago

bryceadams commented 10 years ago

When select2 is cloned, there are issues with the data saving. Discussed here and a bit related - https://github.com/humanmade/Custom-Meta-Boxes/issues/73

Can test by using a pw_select type in a repeatable group field.

bryceadams commented 10 years ago

Another solution for it - http://stackoverflow.com/questions/17175534/clonned-select2-is-not-responding

AlchemyUnited commented 9 years ago

@bryceadams - I'm curious to know if you've forked + hacked your own solution? Yet? Given Select2's popularity I'm surprised it does this. That said, I too need it to work with a repeater / group. I'm not really worried about fixing it per se. I just don't want to do so if someone else already has it sorted.

bryceadams commented 9 years ago

hey @AlchemyUnited, not yet, no time. would be awesome if you could look at this and i'll definitely contribute to what you come up with.

AlchemyUnited commented 9 years ago

fyi - I have this sorted out (read: proof of concept / alpha) but it entails changes to CMB2, as well as a tweak or two to this plugin. Let me test a bit more and issue the necessary pull requests, etc.

bryceadams commented 9 years ago

excellent!

AlchemyUnited commented 9 years ago

@bryceadams - here ya go...

I have PRs in with this plugin as well as CMB2. if you what to test prior to those merges these are the forks I'm working with.

https://github.com/AlchemyUnited/CMB2 << added some triggers to the UI js.

https://github.com/AlchemyUnited/cmb-field-select2 << destroys and such based on the triggers.

If you can test that would be a big help. All seem ok with me but but I've been solving more than I've been testing :)

mustardBees commented 9 years ago

Thanks for your work on this @AlchemyUnited. I see the necessary events have been accepted into CMB2. I will go ahead and test/merge your pull request shortly.

bryceadams commented 9 years ago

great work on this @AlchemyUnited!

AlchemyUnited commented 9 years ago

@mustardBees @bryceadams

Merry Christmas!!!

Something tells me there are still a couple things to fix - that no one anticipated - but we're on our way!!

Keep me posted about any issues. I'll be happy - time permitting to clean them up.

monecchi commented 8 years ago

Hey @AlchemyUnited! How about passing a function which returns an array of options for the field? I've dowloaded your forked version of the cmb-field-select2 and got it working (almost).

I'm using the select2 field within a CMB2 repeatable group, and although it renders the field with the options returned by my custom function with an array of woocommerce variable products, it does not seem to be saving any values.

woocommerce-product-variations-select2

It surprisingly works though if I set up a CMB2 default select field, and then pass the function to it. By doing that, I end up with a properly populated select field with the product variations and their ID's as the value of each <option> element.

wc-product-variations-select

However, when I try to set up the 'type' => 'pw_select', it fails on saving the values.

Any chances my function is causing the issue? Here it is:

function get_product_variation_term_options( $query_args ) {

global $post, $product;

$args = wp_parse_args( $query_args, array(
    'post_type'   => 'product_variation',
    'post_status' => array( 'private', 'publish' ),
    'posts_per_page' => -1,
    'orderby'     => 'menu_order',
    'order'       => 'ASC',
    'post_parent' => get_related_post_id() // external function that gets the post ID
) );

$post_options = array();

// return post type as options
$posts = get_posts( $args );

if ( $posts ) {
    foreach ( $posts as $post ) {

    $variation = wc_get_product($post);

    $post_options[ $post->ID ] = $variation->get_formatted_name();
    }
}
    return $post_options;
}

And here's the field set up:

    $cmb_group->add_group_field( $group_field_id, array(
        'name' => __( 'Display a Buy Button?', 'mytheme' ),
        'class'   => 'cmb-type-pw-select select2',
        'desc' => __( 'Select a product variation ', 'mytheme'),
        'id' => 'select_post_type_variations',
        'type' => 'pw_select',
        'options' => get_product_variation_term_options( array( 'post_type' => 'product_variation', 'posts_per_page' => 4 ) ), 
    ) ); 

In the front end, each repeatable group is rendered as a price box, and for each selected product variation from the select2 field, a buy button is displayed right beside the price, that then triggers woocommerce's ajax function and adds a single (variable) product to the cart. It all happens on a custom post type single page. However, without the ids values grabbed from the select2 field, the buy button function fails.

woocommerce-product-variation-buy-button-post-type

Thanks in advance.

AlchemyUnited commented 8 years ago

@monecchi - I'm a bit backed up atm but if you want me to take a swing at it let me know and we'll figure out how to pass your fork over to me.

monecchi commented 8 years ago

Thanks @AlchemyUnited. I'd really appreciate it! I've enhanced the previous comment with more details.

I've tried a few different cmb-field-select2 forks, as a few others select2 field types for CMB2, but all those expects an array of options to be manually set such as 'options' => array( '1' => 'one', '2' => 'two' ), while I was hoping to populate the options array with a function.

Anyway, no rush for this though, I've decided to fallback to CMB2 default select field until I could get it solved.

Regards.

AlchemyUnited commented 8 years ago

@monecchi - Okay. But this sounds like a challenge. And a useful one if solved. Where is your fork? Or maybe I'll give it a fresh crack and then we've can trade notes?

mustardBees commented 8 years ago

@AlchemyUnited @monecchi A massive thanks to @AlchemyUnited for the repeatable field work. It took a while to get around to looking at this in detail. I've pushed a new release which incorporates this work.

I'd appreciate any assistance testing this across more sites. It has worked well for me with the exception of a couple of issues which I've noted in a known issues/limitations section in the documentation.

monecchi commented 8 years ago

Hey @mustardBees, thank you so much for pushing the new version. I can confirm it works beautifully now! No issues with my query/get_posts function so far. @AlchemyUnited thank you for your great contribution with the repeatable field work. You guys rock!!!

funny-gif-happy-clapping-computer