简体   繁体   English

显示Woocommerce存档页面中变体的大小产品属性值

[英]Display size product attribute values for variations in Woocommerce archive pages

In Woocommerce, I have added a hooked function which displays the product attribute for variation assigned to a variable product on the Woocommerce archive pages: 在Woocommerce中,我添加了一个钩子函数,该函数显示分配给Woocommerce存档页面上的变量产品的变体的产品属性:

add_action( 'woocommerce_after_shop_loop_item', 'stock_variations_loop' );
function stock_variations_loop(){
    global $product;

    if ( $product->get_type() == 'variable' ) {
        foreach ($product->get_available_variations() as $key) {
            $attr_string = '';

            foreach ( $key['attributes'] as $attr_name => $attr_value) {
                $attr_string[] = $attr_value;
            }
            if ( $key['max_qty'] > 0 ) { 
                echo '<div class="sizeVariantCat">' . implode(', ', $attr_string).'</div>'; 
            } 
        }
    }
}

It is working fine, but some of the data is messy… I only want to display 'Size' product attribute for variation values, not all the products that have a "size" attribute. 它工作正常,但有些数据很混乱...我只想显示变量值的'Size'产品属性,而不是所有具有“size”属性的产品。

If you want to target the product attribute for variation "size" to get the corresponding values from the product variations set for the variable product, try the following: 如果您希望将变量“size”的product属性作为目标,以便从为变量product设置的产品变体中获取相应的值,请尝试以下操作:

add_action( 'woocommerce_after_shop_loop_item', 'display_attribute_size_for_variations' );
function display_attribute_size_for_variations(){
    global $product;

    // HERE the taxonomy for the targeted product attribute
    $taxonomy = 'pa_size';

    if ( $product->get_type() == 'variable' ) {
        $output = array();
        foreach ($product->get_available_variations() as $values) {
            foreach ( $values['attributes'] as $attr_variation => $term_slug ) {
                // Targetting "Size" attribute only
                if( $attr_variation === 'attribute_' . $taxonomy ){
                    // Add the size attribute term name value to the array (avoiding repetitions)
                    $output[$term_slug] = get_term_by( 'slug', $term_slug, $taxonomy )->name;
                }
            }
        }
        if ( sizeof($output) > 0 ) {
            echo '<div class="'.$taxonomy.'-variations-terms">' . implode( ', ', $output ).'</div>';
        }
    }
}

Code goes in function.php file of your active child theme (or theme). 代码位于活动子主题(或主题)的function.php文件中。 tested and works. 测试和工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Woocommerce存档页面中显示特定产品属性 - Display specific product attribute in Woocommerce archive pages 在商店的WooCommerce存档页面上显示产品变化 - Show product variations on WooCommerce archive pages as shop 存档页面上显示的Woocommerce变体 - Woocommerce Variations Displayed on Archive Pages 在存档页面上显示 Woocommerce 产品属性 - Display Woocommerce product attribute on archive page 在 Woocommerce 存档页面中的产品标题下显示特定的产品属性 - Display specific product attributes under product title in Woocommerce archive pages 获取Woocommerce中存档页面上特定产品属性的slug列表 - Get list of slugs for a specific product attribute on archive pages in Woocommerce 在 Woocommerce 存档页面中显示所有产品类型的库存可用性 - Display the stock availability for all product types in Woocommerce archive pages 在 WooCommerce 产品存档页面上显示自定义分类术语 - Display custom taxonomy terms on WooCommerce product archive pages 在 WooCommerce 存档页面上显示视频而不是产品缩略图 - Display video instead of product thumbnail on WooCommerce archive pages 在 WooCommerce 存档页面中的产品标题下方显示 ACF 字段/图像 - Display ACF field/image below the product title in WooCommerce archive pages
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM