8manos / wc-variations-radio-buttons

Let your customers choose product variations using radio buttons instead of dropdowns.
https://wordpress.org/plugins/wc-variations-radio-buttons/
GNU General Public License v2.0
62 stars 34 forks source link

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

Open Mantish opened 8 years ago

Mantish commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

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 commented 7 years ago

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 commented 7 years ago

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 commented 7 years ago

@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 commented 7 years ago

@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 commented 7 years ago

@gstockhk1 you can send me an email to mauricio@8manos.com thanks!

clarkeverdel commented 7 years ago

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

jvwrx commented 7 years ago

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 commented 7 years ago

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 commented 7 years ago

@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 commented 6 years ago

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 commented 6 years ago

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