WooCommerce-如果选定的属性术语等于

时间:2019-10-09 作者:Billy

我创建了一个属性“color”,其中包含两个术语“black”和“white”,我想在selected 术语等于白色。

我尝试了以下代码(基于https://stackoverflow.com/a/23622917/10183871) 但它似乎只是检查是否有可用的术语“白色”,以及是否有然后显示的东西。仅当选择了“白色”一词时,我才想显示一些内容

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

1 个回复
最合适的回答,由SO网友:Chetan Vaghela 整理而成

我修改了的代码https://stackoverflow.com/a/54722971/10183871 您提供的。我根据你的要求做了一些修改。您可以参考下面的代码,并根据您的要求进行更改。我已经测试了代码,它正在为我工作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;
}
更改文本自$(vendorTarget).text("here is your text"); 要显示的。if($(vendorVal).val() == \'White\') 根据需要更改颜色属性名称。如果这对你有帮助,请告诉我。