显示Woocommerce中所选变体的产品属性字词

时间:2019-02-15 13:08:38

标签: php jquery wordpress woocommerce variations

在Woocommerce中,我使用以下代码来获取产品属性字词。

function action_woocommerce_product_meta_end() {
    global $product;

    $term_ids = wp_get_post_terms( $product->get_id(), 'pa_apple-id', array('fields' => 'ids') );

    echo get_the_term_list( $product->get_id(), 'pa_apple-id', '<span class="posted_in">' . _n( 'Vendor:', 'Vendors:', count( $term_ids ), 'woocommerce' ) . ' ', ', ', '</span>' );
}
add_action( 'woocommerce_product_meta_end', 'action_woocommerce_product_meta_end', 10, 0 );

但这显示所有术语:

screenshot

相反,当选择变体时,我只想显示产品属性pa_apple-id的相应唯一术语。

1 个答案:

答案 0 :(得分:0)

这要复杂得多,因为它是客户端上的实时事件,因此需要javascript / jQuery和更多代码:

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

// 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 ){
        $label = _n("Vendor:", "Vendors:", count($term_ids), "woocommerce");
        echo get_the_term_list( get_the_ID(), $taxonomy, '<span class="posted_in vendors">' . $label . ' ', ', ', '</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 = '<?php _e("Vendor:", "woocommerce"); ?>';

        // Once DOM is loaded
        setTimeout(function() {
            if($(selected).val() != ''){
                $(vendorTarget).text(vendorLabel+' '+$(vendorVal).val());
            }
        }, 300 );

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

代码进入活动子主题(或活动主题)的function.php文件中。经过测试,可以正常工作。