• I’m wondering if you have a solution which will create radio buttons for variable subscriptions as well as variable products. It used to work prior to upgrading the variable subscription plugin 2.0.9
    thanks,
    rich

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter richberman

    (@richberman)

    add the code below to wc-variations-radio-buttons/templates/single-product/add-to-cart/ folder. name the file “variable-subscription.php”

    <?php
    /**
    * Variable subscription product add to cart
    *
    * @author Prospress
    * @package WooCommerce-Subscriptions/Templates
    * @version 2.0.9
    */
    if ( ! defined( ‘ABSPATH’ ) ) {
    exit;
    }

    if ( ! function_exists( ‘print_attribute_radio’ ) ) {
    function print_attribute_radio( $checked_value, $value, $label, $name ) {
    // This handles < 2.4.0 bw compatibility where text attributes were not sanitized.
    $checked = sanitize_title( $checked_value ) === $checked_value ? checked( $checked_value, sanitize_title( $value ), false ) : checked( $checked_value, $value, false );

    $input_name = ‘attribute_’ . esc_attr( $name ) ;
    $esc_value = esc_attr( $value );
    $id = esc_attr( $name . ‘_v_’ . $value );
    $filtered_label = apply_filters( ‘woocommerce_variation_option_name’, $label );
    printf( ‘<div><input type=”radio” name=”%1$s” value=”%2$s” id=”%3$s” %4$s><label for=”%3$s”> %5$s</label></div>’, $input_name, $esc_value, $id, $checked, $filtered_label );
    }
    }

    global $product;

    $attribute_keys = array_keys( $attributes );
    $user_id = get_current_user_id();

    do_action( ‘woocommerce_before_add_to_cart_form’ ); ?>

    <form class=”variations_form cart” method=”post” enctype=’multipart/form-data’ data-product_id=”<?php echo absint( $product->id ); ?>” data-product_variations=”<?php echo esc_attr( json_encode( $available_variations ) ) ?>”>
    <?php do_action( ‘woocommerce_before_variations_form’ ); ?>

    <?php if ( empty( $available_variations ) && false !== $available_variations ) : ?>
    <p class=”stock out-of-stock”><?php esc_html_e( ‘This product is currently out of stock and unavailable.’, ‘woocommerce-subscriptions’ ); ?></p>
    <?php else : ?>
    <?php if ( ! $product->is_purchasable() && 0 != $user_id && ‘no’ != wcs_get_product_limitation( $product ) && wcs_is_product_limited_for_user( $product, $user_id ) ) : ?>
    <?php $resubscribe_link = wcs_get_users_resubscribe_link_for_product( $product->id ); ?>
    <?php if ( ! empty( $resubscribe_link ) && ‘any’ == wcs_get_product_limitation( $product ) && wcs_user_has_subscription( $user_id, $product->id, wcs_get_product_limitation( $product ) ) && ! wcs_user_has_subscription( $user_id, $product->id, ‘active’ ) && ! wcs_user_has_subscription( $user_id, $product->id, ‘on-hold’ ) ) : // customer has an inactive subscription, maybe offer the renewal button ?>
    ” class=”button product-resubscribe-link”><?php esc_html_e( ‘Resubscribe’, ‘woocommerce-subscriptions’ ); ?>
    <?php else : ?>
    <p class=”limited-subscription-notice notice”><?php esc_html_e( ‘You have an active subscription to this product already.’, ‘woocommerce-subscriptions’ ); ?></p>
    <?php endif; ?>
    <?php else : ?>
    <table class=”variations” cellspacing=”0″>
    <tbody>
    <?php foreach ( $attributes as $name => $options ) : ?>
    <tr>
    <td class=”label”><label for=”<?php echo sanitize_title( $name ); ?>”><?php echo wc_attribute_label( $name ); ?></label></td>
    <?php
    $sanitized_name = sanitize_title( $name );
    if ( isset( $_REQUEST[ ‘attribute_’ . $sanitized_name ] ) ) {
    $checked_value = $_REQUEST[ ‘attribute_’ . $sanitized_name ];
    } elseif ( isset( $selected_attributes[ $sanitized_name ] ) ) {
    $checked_value = $selected_attributes[ $sanitized_name ];
    } else {
    $checked_value = ”;
    }
    ?>
    <td class=”value”>
    <?php
    if ( ! empty( $options ) ) {
    if ( taxonomy_exists( $name ) ) {
    // Get terms if this is a taxonomy – ordered. We need the names too.
    $terms = wc_get_product_terms( $product->id, $name, array( ‘fields’ => ‘all’ ) );

    foreach ( $terms as $term ) {
    if ( ! in_array( $term->slug, $options ) ) {
    continue;
    }
    print_attribute_radio( $checked_value, $term->slug, $term->name, $sanitized_name );
    }
    } else {
    foreach ( $options as $option ) {
    print_attribute_radio( $checked_value, $option, $option, $sanitized_name );
    }
    }
    }

    echo end( $attribute_keys ) === $name ? apply_filters( ‘woocommerce_reset_variations_link’, ‘‘ . __( ‘Clear’, ‘woocommerce’ ) . ‘‘ ) : ”;
    ?>
    </td>
    </tr>
    <?php endforeach; ?>
    </tbody>
    </table>
    <?php do_action( ‘woocommerce_before_add_to_cart_button’ ); ?>

    <div class=”single_variation_wrap” style=”display:none;”>
    <?php
    /**
    * woocommerce_before_single_variation Hook
    */
    do_action( ‘woocommerce_before_single_variation’ );

    /**
    * woocommerce_single_variation hook. Used to output the cart button and placeholder for variation data.
    * @since 2.4.0
    * @hooked woocommerce_single_variation – 10 Empty div for variation data.
    * @hooked woocommerce_single_variation_add_to_cart_button – 20 Qty and cart button.
    */
    do_action( ‘woocommerce_single_variation’ );

    /**
    * woocommerce_after_single_variation Hook
    */
    do_action( ‘woocommerce_after_single_variation’ );
    ?>
    </div>

    <?php do_action( ‘woocommerce_after_add_to_cart_button’ ); ?>
    <?php endif; ?>
    <?php endif; ?>

    <?php do_action( ‘woocommerce_after_variations_form’ ); ?>
    </form>

    <?php do_action( ‘woocommerce_after_add_to_cart_form’ ); ?>

    Hey Rich. Same problem – I need radio buttons for a variable subscription. I tried your code but it broke the product page. Nothing renders after the product page heading.

    Can you try pasting the code within code tags so the formatting remains intact?

    Cheers
    Len

    @quietcity I was able to clean up the code @richberman posted for us. It works! Thanks, Rich!

    Here’s the cleaned code in code tags in case anyone still needs it:

    <?php
    /**
    * Variable subscription product add to cart
    *
    * @author Prospress
    * @package WooCommerce-Subscriptions/Templates
    * @version 2.0.9
    */
    if ( ! defined( 'ABSPATH' ) ) {
    	exit;
    }
    
    if ( ! function_exists( 'print_attribute_radio' ) ) {
    	function print_attribute_radio( $checked_value, $value, $label, $name ) {
    		// This handles < 2.4.0 bw compatibility where text attributes were not sanitized.
    		$checked = sanitize_title( $checked_value ) === $checked_value ? checked( $checked_value, sanitize_title( $value ), false ) : checked( $checked_value, $value, false );
    
    		$input_name = 'attribute_' . esc_attr( $name ) ;
    		$esc_value = esc_attr( $value );
    		$id = esc_attr( $name . '_v_' . $value );
    		$filtered_label = apply_filters( 'woocommerce_variation_option_name', $label );
    		printf( '<div><input type="radio" name="%1$s" value="%2$s" id="%3$s" %4$s><label for="%3$s"> %5$s</label></div>', $input_name, $esc_value, $id, $checked, $filtered_label );
    	}
    }
    
    global $product;
    
    $attribute_keys = array_keys( $attributes );
    $user_id = get_current_user_id();
    
    do_action( 'woocommerce_before_add_to_cart_form' ); ?>
    
    <form class="variations_form cart" method="post" enctype='multipart/form-data' data-product_id="<?php echo absint( $product->id ); ?>" data-product_variations="<?php echo esc_attr( json_encode( $available_variations ) ) ?>">
    	<?php do_action( 'woocommerce_before_variations_form' ); ?>
    
    	<?php if ( empty( $available_variations ) && false !== $available_variations ) : ?>
    		<p class="stock out-of-stock"><?php esc_html_e( 'This product is currently out of stock and unavailable.', 'woocommerce-subscriptions' ); ?></p>
    	<?php else : ?>
    		<?php if ( ! $product->is_purchasable() && 0 != $user_id && 'no' != wcs_get_product_limitation( $product ) && wcs_is_product_limited_for_user( $product, $user_id ) ) : ?>
    			<?php $resubscribe_link = wcs_get_users_resubscribe_link_for_product( $product->id ); ?>
    				<?php if ( ! empty( $resubscribe_link ) && 'any' == wcs_get_product_limitation( $product ) && wcs_user_has_subscription( $user_id, $product->id, wcs_get_product_limitation( $product ) ) && ! wcs_user_has_subscription( $user_id, $product->id, 'active' ) && ! wcs_user_has_subscription( $user_id, $product->id, 'on-hold' ) ) : // customer has an inactive subscription, maybe offer the renewal button ?>
    					<a href="<?php echo esc_url( $resubscribe_link ); ?>" class="button product-resubscribe-link"><?php esc_html_e( 'Resubscribe', 'woocommerce-subscriptions' ); ?></a>
    				<?php else : ?>
    					<p class="limited-subscription-notice notice"><?php esc_html_e( 'You have an active subscription to this product already.', 'woocommerce-subscriptions' ); ?></p>
    				<?php endif; ?>
    			<?php else : ?>
    				<table class="variations" cellspacing="0″>
    					<tbody>
    						<?php foreach ( $attributes as $name => $options ) : ?>
    							<tr>
    								<td class="label"><label for="<?php echo sanitize_title( $name ); ?>"><?php echo wc_attribute_label( $name ); ?></label></td>
    								<?php
    								$sanitized_name = sanitize_title( $name );
    								if ( isset( $_REQUEST[ 'attribute_' . $sanitized_name ] ) ) {
    									$checked_value = $_REQUEST[ 'attribute_' . $sanitized_name ];
    								} elseif ( isset( $selected_attributes[ $sanitized_name ] ) ) {
    									$checked_value = $selected_attributes[ $sanitized_name ];
    								} else {
    									$checked_value = '';
    								}
    								?>
    								<td class="value">
    									<?php
    									if ( ! empty( $options ) ) {
    										if ( taxonomy_exists( $name ) ) {
    											// Get terms if this is a taxonomy – ordered. We need the names too.
    											$terms = wc_get_product_terms( $product->id, $name, array( 'fields' => 'all' ) );
    
    											foreach ( $terms as $term ) {
    												if ( ! in_array( $term->slug, $options ) ) {
    													continue;
    												}
    												print_attribute_radio( $checked_value, $term->slug, $term->name, $sanitized_name );
    											}
    										} else {
    											foreach ( $options as $option ) {
    												print_attribute_radio( $checked_value, $option, $option, $sanitized_name );
    											}
    										}
    									}
    
    									echo end( $attribute_keys ) === $name ? apply_filters( 'woocommerce_reset_variations_link', '<a class="reset_variations" href="#">' . __( 'Clear', 'woocommerce' ) . '</a>' ) : '';
    									?>
    								</td>
    							</tr>
    						<?php endforeach; ?>
    					</tbody>
    				</table>
    
    			<?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>
    
    			<div class="single_variation_wrap" style="display:none;">
    				<?php
    				/**
    				* woocommerce_before_single_variation Hook
    				*/
    				do_action( 'woocommerce_before_single_variation' );
    
    				/**
    				* woocommerce_single_variation hook. Used to output the cart button and placeholder for variation data.
    				* @since 2.4.0
    				* @hooked woocommerce_single_variation – 10 Empty div for variation data.
    				* @hooked woocommerce_single_variation_add_to_cart_button – 20 Qty and cart button.
    				*/
    				do_action( 'woocommerce_single_variation' );
    
    				/**
    				* woocommerce_after_single_variation Hook
    				*/
    				do_action( 'woocommerce_after_single_variation' );
    				?>
    			</div>
    
    			<?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>
    		<?php endif; ?>
    	
    	<?php endif; ?>
    
    	<?php do_action( 'woocommerce_after_variations_form' ); ?>
    </form>
    
    <?php do_action( 'woocommerce_after_add_to_cart_form' ); ?>
    Thread Starter richberman

    (@richberman)

    Thanks for cleaning that up. I am wondering if you can email me the revised plugin in a zip format so I can upload it to my staging site to test it.

    Also, in my old site I had some trial customizations for formatting the labels for the options which I can’t get to work with this plugin.

    The variations-radio-buttons uses different element IDs and classes for their labels etc.

    This means these JQuery selectors in the plugin no longer work.

    $(‘.postid-2066 table.variations tr:first-of-type td.value fieldset strong’).text(‘Choose a Size’);
    $(‘.postid-2187 table.variations tr:first-of-type td.value fieldset strong’).text(‘Choose a Size’);
    $(‘table.variations tr:nth-of-type(2) td.value fieldset strong’).text(‘Length?’);

    I’m wondering if you know how this plugin identifies the element IDs and classes.

    Thread Starter richberman

    (@richberman)

    Here is my current formatting:
    https://pourrichardscoffee.com/product/coffee-subscriptions/

    I’m wondering how to identify only variable subscription items to create this layout with wc-radio-button plugin.

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘radio button for variable subscriptions’ is closed to new replies.