在Woocommerce的下拉变体属性选项中显示价格和库存状态

在Woocommerce的下拉变体属性选项中显示价格和库存状态

本文介绍了在Woocommerce的下拉变体属性选项中显示价格和库存状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在woocommerce上,我的可变产品上的每个变体都有特定的价格.

如何在属性下拉选项中显示变动价格和库存状态?

解决方案

以下代码将在唯一的下拉变化属性选项中显示变化价格和库存状态:

 //实用程序功能,用于从其属性值获取价格或变体的库存状态函数get_variation_price_stock_string($ product,$ name,$ term_slug){foreach($ product-> get_available_variations()as $ variation){if($ variation ['attributes'] [$ name] == $ term_slug){$ stock_status = $ variation ['is_in_stock'] == 1吗?__('In stock'):__('Out of stock');return'('.strip_tags($ variation ['price_html']).')'.$ stock_status;}}}//将价格和库存状态添加到下拉选项项中.add_filter('woocommerce_dropdown_variation_attribute_options_html','show_stock_status_in_dropdown',10,2);函数show_stock_status_in_dropdown($ html,$ args){//仅当存在唯一的变体属性(一个下拉列表)时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']吗?真假;$ show_option_none_text = $ args ['show_option_none']吗?$ args ['show_option_none']:__('选择一个选项','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').'>';;$ html.='< option value =">'.esc_html($ show_option_none_text).'</option>';如果(!empty($ options)){if($ product&& taxonomy_exists($ attribute)){$ terms = wc_get_product_terms($ product-> get_id(),$ attribute,array('fields'=>'all'));foreach(将$ terms作为$ term){if(in_array($ term-> slug,$ options)){//获取价格和库存状态$ price_stock_html = get_variation_price_stock_string($ product,$ name,$ term-> slug);//插入价格和库存状态$ html.='< option value ='.esc_attr($ term-> slug).'"'.selected(sanitize_title($ args ['selected']),$ term-> slug,false).'>'.esc_html(apply_filters('woocommerce_variation_option_name',$ term-> name).$ price_stock_html).'</option>';}}} 别的 {foreach($ options as $ option){$ selected = sanitize_title($ args ['selected'])=== $ args ['selected']吗?selected($ args ['selected'],sanitize_title($ option),false):selected($ args ['selected'],$ option,false);//获取价格和库存状态$ price_stock_html = get_variation_price_stock_string($ product,$ name,$ term-> slug);//插入价格和库存状态$ html.='< option value ='.esc_attr($ option).'"'.$ selected.'>'.esc_html(apply_filters('woocommerce_variation_option_name',$ option).$ price_stock_html).'</option>';}}}$ html.='</select>';;万一;返回$ html;} 

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

基于:

On woocommerce on my variable products each variation has specific price.

How can I display the variation price and stock status in the attribute dropdown options?

解决方案

The following code will display the variation price and stock status in a unique dropdown variation attribute options:

// Utility function to get the price or the stock status of a variation from it's attribute value
function get_variation_price_stock_string( $product, $name, $term_slug ){
    foreach ( $product->get_available_variations() as $variation ){
        if($variation['attributes'][$name] == $term_slug ){
            $stock_status = $variation['is_in_stock'] == 1 ? __('In stock') : __('Out of stock');
            return ' (' . strip_tags( $variation['price_html'] ) . ') ' . $stock_status;
        }
    }
}

// Add the price and stock status to the dropdown options items.
add_filter( 'woocommerce_dropdown_variation_attribute_options_html', 'show_stock_status_in_dropdown', 10, 2);
function show_stock_status_in_dropdown( $html, $args ) {
    // 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' ) . '">';
    $html .= '<option value="">' . esc_html( $show_option_none_text ) . '</option>';

    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 ) ) {
                    // Get the price and stock status
                    $price_stock_html = get_variation_price_stock_string( $product, $name, $term->slug );
                    // Insert the price and stock status
                    $html .= '<option value="' . esc_attr( $term->slug ) . '" ' . selected( sanitize_title( $args['selected'] ), $term->slug, false ) . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $term->name ) . $price_stock_html  ) . '</option>';
                }
            }
        } else {
            foreach ( $options as $option ) {
                $selected = sanitize_title( $args['selected'] ) === $args['selected'] ? selected( $args['selected'], sanitize_title( $option ), false ) : selected( $args['selected'], $option, false );
                // Get the price and stock status
                $price_stock_html = get_variation_price_stock_string( $product, $name, $term->slug );
                // Insert the price and stock status
                $html .= '<option value="' . esc_attr( $option ) . '" ' . $selected . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $option ) . $price_stock_html ) . '</option>';
            }
        }
    }
    $html .= '</select>';

    endif;

    return $html;
}

Code goes in function.php file of your active child theme (or active theme). tested and works.

Based on:

这篇关于在Woocommerce的下拉变体属性选项中显示价格和库存状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 03:08