customization – Conditionally display available subscription variations

Question

I have tapped into woocommerce_dropdown_variation_attribute_options_html to conditionally show or hide possible variations a subscribed user can switch to, based on what variation is active.

The custom function is working correctly however it take a lot to load and perform all the logic checks because it is doing it inside the foreach loop that generates the html output.

Is there a way I could optimize this function and perform all necessary logic checks before the loop?

code below:

// Utility function to get the price of a variation from it's attribute value
function get_the_variation_price_html( $product, $name, $term_slug ){
    foreach ( $product->get_available_variations() as $variation ){
        if($variation['attributes'][$name] == $term_slug ){
            return strip_tags( $variation['price_html'] );
        }
    }
}
// Utility function to get the id of a variation from it's attribute value
function get_the_vari_id( $product, $name, $term_slug ){
    foreach ( $product->get_available_variations() as $variation ){
        if($variation['attributes'][$name] == $term_slug ){
            return strip_tags( $variation['variation_id'] );
        }
    }
}
// Utility function to make the option HTML
function print_vari_html($vari_id, $term, $args, $price_html) {
    $html="<option var-id="" . esc_html( $vari_id ) . 
    '" value="' . esc_attr( $term->slug ) . 
    '" ' . selected( sanitize_title( $args['selected'] ), $term->slug, false ) . '>' 
    . esc_html( apply_filters( 'woocommerce_variation_option_name', $term->name ) . ' - ' . $price_html ) . '</option>';
    return $html;
}


function variation_switch_custom_logic( $html, $args ) {
    // check if is subscrition switch page and get current subscription and current variation
    if (isset($_GET['switch-subscription'])) {
        $get_sub_id = $_GET['switch-subscription'];
        $current_subscription = wcs_get_subscription($get_sub_id);
        $sub_items = $current_subscription ->get_items();
        foreach ( $sub_items as $product ) {
            $current_var_id = $product['variation_id'];
        }
    }
    
    // set variation id for PLAN1 and PLAN2 and FREEZE
    $FREEZE_id = 12366;
    $if_PLAN2_ids = array(12355, 12356, 12357, 12358, 12359, 12360, 12361, 12362, 12363, 12364, 12365); // list all PLAN2 ids
    $if_PLAN1_ids = array(12367, 12368, 12369, 12370, 12371, 12372, 12373, 12374, 12375, 12376, 12377); // list all PLAN1 ids
    $PLAN2_ids = array(12355, 12365); // PLAN2 and PLAN2 yearly
    $PLAN1_ids = array(12367, 12377); // PLAN1 and PLAN1 yearly
    $subscriber_PLAN2_ids = array(12363, 12364); // subscriber PLAN2 and PLAN2 yearly
    $subscriber_PLAN1_ids = array(12375, 12376); // subscriber PLAN1 and PLAN1 yearly
    $partner_PLAN2_ids = array(12361, 12362); // partner PLAN2 and PLAN2 yearly
    $partner_PLAN1_ids = array(12373, 12374); // partner PLAN1 and PLAN1 yearly
    
    
    // Only if there is a unique variation attribute (one dropdown)
    if( sizeof($args['product']->get_variation_attributes()) == 1 ) :

    $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'];
    $show_option_none      = $args['show_option_none'] ? true : false;
    $show_option_none_text = $args['show_option_none'] ? $args['show_option_none'] : __( 'Choose an option', 'woocommerce' );

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

    $html="<select id="" . esc_attr( $id ) . 
            '" class="' . esc_attr( $class ) . 
            '" name="' . esc_attr( $name ) . 
            '" data-attribute_name="attribute_' . esc_attr( sanitize_title( $attribute ) ) . 
            '" data-show_option_none="' . ( $show_option_none ? 'yes' : 'no' ) . '">';

    if ( ! empty( $options ) ) {
        if ( $product && taxonomy_exists( $attribute ) ) {
            $terms = wc_get_product_terms( $product->get_id(), $attribute, array( 'fields' => 'all' ) );
            foreach ( $terms as $term ) {
                if ( in_array( $term->slug, $options )) {
                    $price_html = get_the_variation_price_html( $product, $name, $term->slug );
                    $vari_id = get_the_vari_id( $product, $name, $term->slug );
                    // IF current variation is FREEZE
                    if ( $current_var_id == $FREEZE_id ) {
                        if ( in_array( $vari_id, $PLAN1_ids ) || in_array( $vari_id, $PLAN2_ids ) ) {
                            $html .= print_vari_html($vari_id, $term, $args, $price_html);
                        }
                    } // IF current variation is PLAN2
                    elseif ( in_array( $current_var_id, $if_PLAN2_ids ) ) {
                        if ( in_array( $vari_id, $PLAN1_ids ) || $vari_id == $FREEZE_id ) {
                            $html .= print_vari_html($vari_id, $term, $args, $price_html);
                        }
                    } // IF current variation is PLAN2 for partners
                    elseif ( in_array( $current_var_id, $partner_PLAN2_ids ) ) { 
                        if ( in_array( $vari_id, $partner_PLAN1_ids ) || $vari_id == $FREEZE_id) {
                            $html .= print_vari_html($vari_id, $term, $args, $price_html);
                        }
                    } // IF current variation is PLAN2 for subscriber 
                    elseif ( in_array( $current_var_id, $subscriber_PLAN2_ids ) ) { 
                        if ( in_array( $vari_id, $subscriber_PLAN1_ids ) || $vari_id == $FREEZE_id ) {
                            $html .= print_vari_html($vari_id, $term, $args, $price_html);
                        }
                    } // IF current variation is PLAN1
                    elseif ( in_array( $current_var_id, $if_PLAN1_ids ) ) {
                        if ( in_array( $vari_id, $PLAN2_ids ) || $vari_id == $FREEZE_id) {
                            $html .= print_vari_html($vari_id, $term, $args, $price_html);
                        }
                    } // IF current variation is PLAN1 for partner
                    elseif ( in_array( $current_var_id, $partner_PLAN1_ids ) ) {
                        if ( in_array( $vari_id, $partner_PLAN2_ids ) || $vari_id == $FREEZE_id) {
                            $html .= print_vari_html($vari_id, $term, $args, $price_html);
                        }
                    } // IF current variation is PLAN1 for subscriber
                    elseif ( in_array( $current_var_id, $subscriber_PLAN1_ids ) ) {
                        if ( in_array( $vari_id, $subscriber_PLAN2_ids ) || $vari_id == $FREEZE_id) {
                            $html .= print_vari_html($vari_id, $term, $args, $price_html);
                        }
                    }
                }
            }
        } else {
            foreach ( $options as $option ) {
                $selected = sanitize_title( $args['selected'] ) === $args['selected'] ? selected( $args['selected'], sanitize_title( $option ), false ) : selected( $args['selected'], $option, false );
                $html .= '<option value="' . esc_attr( $option ) . '" ' . $selected . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $option )) . '</option>';
            }
        }
    }
    
    
    $html .= '</select>';

    endif;

    return $html;
}
add_filter( 'woocommerce_dropdown_variation_attribute_options_html', 'variation_switch_custom_logic', 10, 2);

0
Hoofer 2 months 2021-09-29T06:49:33-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse