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

Add compatibility (or disable) for product bundles and subscriptions #14

Open
Mantish opened this issue Jun 28, 2016 · 14 comments
Open

Comments

@Mantish
Copy link
Member

Mantish commented Jun 28, 2016

I'm grouping these into a single issue, but the fix could be different for each plugin

https://www.woothemes.com/products/product-bundles/
https://www.woothemes.com/products/woocommerce-subscriptions/

@GoodGuyTaj
Copy link

GoodGuyTaj commented Aug 22, 2016

Hi Mantish,

I don't know how your progress on woocommerce-subscriptions is coming along, but I've been able to hack something together for at least part of the variation-subscriptions. I can post it, but it would likely benefit from further review as I'm still learning php. Are you interested?

@GoodGuyTaj
Copy link

GoodGuyTaj commented Aug 22, 2016

For the variable-subscriptions.php file starting on line 65, you should find:

                            <?php
                            $selected = isset( $_REQUEST[ 'attribute_' . sanitize_title( $attribute_name ) ] ) ? wc_clean( $_REQUEST[ 'attribute_' . sanitize_title( $attribute_name ) ] ) : $product->get_variation_default_attribute( $attribute_name );
                            wc_dropdown_variation_attribute_options( array( 'options' => $options, 'attribute' => $attribute_name, 'product' => $product, 'selected' => $selected ) );
                            echo wp_kses( end( $attribute_keys ) === $attribute_name ? '<a class="reset_variations" href="#">' . __( 'Clear selection', 'woocommerce-subscriptions' ) . '</a>' : '', array( 'a' => array( 'class' => array(), 'href' => array() ) ) );

                            ?>

I copied/modified the function:

wc_dropdown_variation_attribute_options

and changed it to

wc_dropdown_variation_attribute_options_sub

so it should read:

                            <?php
                            $selected = isset( $_REQUEST[ 'attribute_' . sanitize_title( $attribute_name ) ] ) ? wc_clean( $_REQUEST[ 'attribute_' . sanitize_title( $attribute_name ) ] ) : $product->get_variation_default_attribute( $attribute_name );
                            wc_dropdown_variation_attribute_options_sub( array( 'options' => $options, 'attribute' => $attribute_name, 'product' => $product, 'selected' => $selected ) );
                            echo wp_kses( end( $attribute_keys ) === $attribute_name ? '<a class="reset_variations" href="#">' . __( 'Clear selection', 'woocommerce-subscriptions' ) . '</a>' : '', array( 'a' => array( 'class' => array(), 'href' => array() ) ) );

                            ?>

I then added the following code to the top of the variable-subscription.php file:

function wc_dropdown_variation_attribute_options_sub( $args = array() ) { 
    $args = wp_parse_args( apply_filters( 'woocommerce_dropdown_variation_attribute_options_args', $args ), array( 
        'options' => false,  
        'attribute' => false,  
        'product' => false,  
        'selected' => false,  
        'name' => '',  
        'id' => '',  
        'class' => '',  
        'show_option_none' => __( 'Choose an option', 'woocommerce' ) 
 ) ); 

    $options = $args['options']; 
    $product = $args['product']; 
    $attribute = $args['attribute']; 
    $name = $args['name'] ? $args['name'] : 'attribute_' . sanitize_title( $attribute ); 
    $id = $args['id'] ? $args['id'] : sanitize_title( $attribute ); 
    $class = $args['class']; 

    if ( empty( $options ) && ! empty( $product ) && ! empty( $attribute ) ) { 
        $attributes = $product->get_variation_attributes(); 
        $options = $attributes[ $attribute ]; 
    } 

   // echo '<fieldset id="' . esc_attr( $id ) . '" class="' . esc_attr( $class ) . '" name="' . esc_attr( $name ) . '" data-attribute_name="attribute_' . esc_attr( sanitize_title( $attribute ) ) . '">'; 

/*    if ( $args['show_option_none'] ) { 
*        echo '<option value="">' . esc_html( $args['show_option_none'] ) . '</option>'; 
    } */

    if ( ! empty( $options ) ) { 
        if ( $product && taxonomy_exists( $attribute ) ) { 
            // Get terms if this is a taxonomy - ordered. We need the names too. 
            $terms = wc_get_product_terms( $product->id, $attribute, array( 'fields' => 'all' ) ); 

            foreach ( $terms as $term ) { 
                if ( in_array( $term->slug, $options ) ) { 
                    print_attribute_radio( $checked_value, $term->slug, $term->name, $sanitized_name );
                } 
            } 
        } else { 
            foreach ( $options as $option ) { 
                // This handles < 2.4.0 bw compatibility where text attributes were not sanitized. 
                $selected = sanitize_title( $args['selected'] ) === $args['selected'] ? selected( $args['selected'], sanitize_title( $option ), false ) : selected( $args['selected'], $option, false ); //this should probably be changed to checked similar to how the plugin you wrote has it.
                $value = esc_attr( $option );
                $id = esc_attr( $attribute . '_v_' . $value );
                $modified_label = esc_html( apply_filters( 'woocommerce_variation_option_name', $option ) );
                printf ('<div><input type="radio" name="%1$s" value="%2$s" id="%3$s"><label for="%3$s">%4$s</label></div>', $name, $value, $id, $modified_label ); // need to look at switching selected to checked
            } 
        } 
    } 

   // echo '</fieldset>'; 
} 

@Mantish
Copy link
Member Author

Mantish commented Aug 22, 2016

Hi @GoodGuyTaj , thanks for your help!
I haven't made any progress as I don't have access to any of these plugins, so your code would be very useful.

Can you make a pull request? I'll be happy to review it.

@firelite
Copy link

Is there a way I can quickly implement this into my live site? I need WC Variations Radio Buttons to work with my products that are using variations and the WooCommmerce Subscriptions addon. I couldn't figure out @GoodGuyTaj 's hack. Any working code examples and implementation will be extremely helpful. Thanks!

@Felix-N
Copy link

Felix-N commented Dec 6, 2016

I'm running into the same issue. Any way I can support you?

As a secondary question: I'd like to change all inputs up to 3 options to radio buttons, and leave all others as dropdowns (I could manually choose as well which variant to display how) - it this possible?

@Mantish
Copy link
Member Author

Mantish commented Dec 6, 2016

@Felix-N one way to support may be by sharing a license so I can install the plugins and test.
About your other question, it's not possible, sorry

@gstockhk1
Copy link

@Mantish Do you mean the WC Subscriptions plugin? Happy to share a copy of the current version with you. What is the best way to get the zip file to you?

FYI The @GoodGuyTaj solution didn't work for me either. But I appreciate the effort!

@Mantish
Copy link
Member Author

Mantish commented Dec 9, 2016

@gstockhk1 you can send me an email to [email protected] thanks!

@clarkeverdel
Copy link

Hi Mantish, do you have a solution to fix the radio buttons with subscriptions? Thanks.

@jvwrx
Copy link

jvwrx commented Mar 24, 2017

If it's not possible (or too hard for now) to work with subscription products, I really wish if it didn't affect them at all. Is it possible for the plugin to only work on "standard" variable products?

Today I discovered that our variable subscription products with different pricing per variable weren't updating the price on screen. The correct price was added in the cart but the price on the product page always stayed the lowest price.

(Thanks for all your work on this, it's a great plugin but needs to stay disabled on my site for now)

@ilikesnow
Copy link

ilikesnow commented Jun 25, 2017

I was able to get Variable Subscriptions to work as radio buttons using the WC Variations Radio Buttons plugin and adding this jQuery code to my theme's .js file:

` $('form.variations_form .variations select').each(function(i, select){
var $select = $(select);
$select.find('option').each(function(j, option){

        var $option = $(option);
        // Create a radio:
        var $radio = $('<input type="radio" />');
        // Set name and value:
        $radio.attr('name', $select.attr('name')).attr('value', $option.val());
        // Set checked if the option was selected
        if ($option.attr('selected')) $radio.attr('checked', 'checked');
        // Insert radio before select box:
        $select.before($radio);
        // Insert a label:
        $select.before(
          $("<label />").attr('for', $select.attr('name')).text($option.text())
        );

    });
    $select.remove();
});

`
It's from an old thread, so it may have issues and I have not fully tested it, but it seems to work.
https://stackoverflow.com/questions/2029267/jquery-convert-select-to-radio-buttons

@quietcitydesign
Copy link

@ilikesnow Do you think you could repost your script wrapped in the <> Insert code, so it can be read and copied properly? Pasting it as is breaks all of the line returns.

@dlfdlf56
Copy link

Is there a code update/snippet for WooCommerce Product Bundles? If not, I'm happy to send you a copy of product bundles if you still need it. I would like to have bundles and radio buttons work together as I prefer the radio buttons to the dropdowns.

@gerbaud-design
Copy link

gerbaud-design commented Jun 7, 2018

Hi,
I am trying to use your plugin with "WooCommerce Product Bundles" by wpclevernet which it broke despite the code part about not loading the scipt for bundles. I managed to fix it by changing the detected type into "woosb" like so : is_type('woosb') and this didn't make the radio buttons work obviously but it stopped breaking the bundles from that plugin.
I am php-dumb and don't know how to put a "or" in this code to make it safe for both case, so no proper contribution with pull request and all but this is my two cents.
Thanks for the good work, I hope you can soon make it bundle compatible since I found nothing else that can do it for free.
Guillaume

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