向显示“缺货"的变体选项添加自定义类在 Woocommerce [英] Add a custom class to variation options displaying "Out Of Stock" in Woocommerce

查看:32
本文介绍了向显示“缺货"的变体选项添加自定义类在 Woocommerce的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 "

我只需要向显示文本缺货"的这些变体添加自定义类.

我该怎么做?

伊曼纽尔

解决方案

下面的代码会在变体是缺货":

//检查库存状态并显示相应附加文本的函数函数 get_stock_status_text( $product, $name, $term_slug ){foreach ( $product->get_available_variations() 作为 $variation ){if($variation['attributes'][$name] == $term_slug)$stock = $variation['is_in_stock'];}返回 $stock == 1 ?' - (有货)' : ' - (缺货)';}//将股票状态添加到下拉选项元素的挂钩函数.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 ) .'</选项>';如果(!空($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 ) ) {//这里添加了获取文本状态的函数$stock_status = get_stock_status_text( $product, $name, $term->slug );//这里我们向 

代码位于活动子主题(或活动主题)的 function.php 文件中.经测试有效.

I'm using "How to add variation stock status to Woocommerce product variation dropdown" answer code to show the text "In Stock" or "Out of stock" according to the stock status of the variation.

This is what I get:

I need to add a custom class only to these variants that showing text "Out of Stock".

how can I do it?

Emanuele

解决方案

The following code will add the class outofstock to <option> html tag when the variation is "Out of Stock":

// Function that will check the stock status and display the corresponding additional text
function get_stock_status_text( $product, $name, $term_slug ){
    foreach ( $product->get_available_variations() as $variation ){
        if($variation['attributes'][$name] == $term_slug )
            $stock = $variation['is_in_stock'];
    }

    return $stock == 1 ? ' - (In Stock)' : ' - (Out of Stock)';
}

// The hooked function that will add the stock status to the dropdown options elements.
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']; // The product attribute taxonomy
    $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 ) ) {
                    // HERE Added the function to get the text status
                    $stock_status = get_stock_status_text( $product, $name, $term->slug );

                    // HERE we add a custom class to <option> that are "Out of stock"
                    $option_class = $stock_status === ' - (Out of Stock)' ? ' class="outofstock"' : '';

                    $html .= '<option value="' . esc_attr( $term->slug ) . '" ' . selected( sanitize_title( $args['selected'] ), $term->slug, false ) . $option_class . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $term->name ) . $stock_status ) . '</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 );
                // HERE Added the function to get the text status
                $stock_status = get_the_stock_status( $product, $name, $option );

                // HERE we add a custom class to <option> that are "Out of stock"
                $option_class = $stock_status === ' - (Out of Stock)' ? ' class="outofstock"' : '';

                $html .= '<option value="' . esc_attr( $option ) . '" ' . $selected . $option_class . '>' .
                esc_html( apply_filters( 'woocommerce_variation_option_name', $option ) . $stock_status ) . '</option>';
            }
        }
    }
    $html .= '</select>';

    endif;

    return $html;
}

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

这篇关于向显示“缺货"的变体选项添加自定义类在 Woocommerce的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆