Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Repeatable Fields #10

Closed
bryceadams opened this issue Nov 17, 2014 · 15 comments
Closed

Support for Repeatable Fields #10

bryceadams opened this issue Nov 17, 2014 · 15 comments

Comments

@bryceadams
Copy link

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

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

@bryceadams
Copy link
Author

@AlchemyUnited
Copy link

@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
Copy link
Author

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
Copy link

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
Copy link
Author

excellent!

@AlchemyUnited
Copy link

@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
Copy link
Owner

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
Copy link
Author

great work on this @AlchemyUnited!

@AlchemyUnited
Copy link

@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
Copy link

monecchi commented May 24, 2016

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
Copy link

@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
Copy link

monecchi commented May 25, 2016

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
Copy link

@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
Copy link
Owner

@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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants