0

I have created an attribute "color" with two terms "black" and "white" and i want to show something when selected term equals to white.

I've tried the following code (based on https://stackoverflow.com/a/23622917/10183871) but it seems that it just checks if there is available term "white" and if there is then shows something. I want to show something only when the term "white" is selected

$terms = get_the_terms( $product->id, 'pa_color');
 foreach($terms as $term){
 if($term->name == 'white'){
     echo 'something';
 }
}

Thanks in advance.

  • what is name of attribute ? white or White ? – Chetan Vaghela Oct 9 '19 at 10:50
  • I have an attribute "color" with "white" and "black" terms/values. – Billy Oct 9 '19 at 10:53
  • i mean, you are comparing term name with attribute name or slug ? – Chetan Vaghela Oct 9 '19 at 10:57
  • Let me explain it better. I created a product attribute "color" and i have two terms "black" and "white". When a user selects "white" for product i want to echo something. – Billy Oct 9 '19 at 11:00
  • term name is case sensitive. try with if($term->slug == 'white') or if($term->name == 'White') try with slug or capital W. – Chetan Vaghela Oct 9 '19 at 11:10
0

I have modified code of https://stackoverflow.com/a/54722971/10183871 which you have provided. i have made some changes according your requirement. you can take reference below code and make changes as per your requirement. i have tested code and it is working for me https://prnt.sc/pgwwvq.

// Set the defined product attribute taxonomy
function vendor_defined_taxonomy() {
    // The targeted product attribute taxonomy
    return 'pa_color'; 
}

// Display the vendors on product meta
add_action( 'woocommerce_product_meta_end', 'display_product_vendors', 10 );
function display_product_vendors() {
    $taxonomy = vendor_defined_taxonomy();
    $term_ids = wp_get_post_terms( get_the_ID(), $taxonomy, array('fields' => 'ids') );
    if( sizeof($term_ids) > 0 ){ 
        echo '<span class="posted_in vendors"></span>';
    }
}

// Display the selected variation vendor in a hidden imput field
add_filter( 'woocommerce_available_variation', 'selected_variation_vendor_value', 10, 3 );
function selected_variation_vendor_value( $data, $product, $variation ) {
    $taxonomy = vendor_defined_taxonomy();

    if( isset($data['attributes']['attribute_'.$taxonomy]) )
        $term = get_term_by( 'slug', $data['attributes']['attribute_'.$taxonomy], $taxonomy );

    if( isset($term) && is_a($term, 'WP_Term' ) )
        $data['variation_description'] .= '<input type="hidden" name="vendor-hidden" id="vendor-hidden" value="'.$term->name.'">';

    return $data;
}

// Replace the vendors on product meta by the selected variation vendor
add_action('woocommerce_after_variations_form', 'custom_product_jquery_script');
function custom_product_jquery_script() {
    global $product;

    $taxonomy     = vendor_defined_taxonomy();
    $terms_string = $product->get_attribute($taxonomy);

    if( ! empty($terms_string) ) :
    ?>
    <script type="text/javascript">
    jQuery(function($) {
        var form = 'form.variations_form',       selected = 'input[name="variation_id"]',
            vendorVal = 'input#vendor-hidden',   vendorTarget = 'span.vendors',
            vendorHtml = $(vendorTarget).text(), vendorLabel = '';

        // On variation select
        $(form).on( 'blur', 'select', function() {
            if($(selected).val() != ''){
                $(vendorTarget).text("");
                if($(vendorVal).val() == 'White'){
                    //$(vendorTarget).text(vendorLabel+' '+$(vendorVal).val());
                    $(vendorTarget).text("here is your text");
                }
            } 
        });
    });
    </script>
    <?php
    endif;
}

change text from $(vendorTarget).text("here is your text"); which you want to display. if($(vendorVal).val() == 'White') change color attribute name as per your requirement. let me know if this helps to you.

  • It works! Thank you so much! If I select color: white it displays the text. The only problem i have is if i select white and after change the selected color to black, the text still displays. – Billy Oct 9 '19 at 12:17
  • i have updated code. $(vendorTarget).text(""); just add double quotes in text() – Chetan Vaghela Oct 9 '19 at 12:22
  • 1
    Thank you! I accepted your answer! – Billy Oct 9 '19 at 12:23
  • You're welcome!. – Chetan Vaghela Oct 9 '19 at 12:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.