如何将变体库存状态添加到 Woocommerce 产品变体下拉列表 [英] How to add variation stock status to Woocommerce product variation dropdown

查看:45
本文介绍了如何将变体库存状态添加到 Woocommerce 产品变体下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示 Woocommerce 产品页面上的变体下拉列表中显示的每个产品变体的库存状态(例如,有货/缺货).我已将相关函数复制到我的主题的functions.php 文件中,并且可以编辑内容,但不确定如何为每个变体提取所需的库存状态.

<前>//更新的 Woocommerce 产品变体选择如果(!function_exists('wc_dropdown_variation_attribute_options')){/*** 输出用于购物车表单的变体属性列表.** @param 数组 $args* @自 2.4.0*//*函数 wc_dropdown_variation_attribute_options( $args = array() ) {$args = wp_parse_args( apply_filters( 'woocommerce_dropdown_variation_attribute_options_args', $args ), array('选项' => 假,'属性' => 假,'产品' => 假,'选择' => 假,'姓名' => '','id' => '','类' => '','show_option_none' => __( '选择一个选项', 'woocommerce'),) );$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 = '';$html .= '' .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 ) ) {$html .= 'slug ) .'" ' . selected( sanitize_title( $args['selected'] ), $term->slug, false ) . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $term->name ) ) . ' ;}}} 别的 {foreach ( $options as $option ) {//这处理了 lt 2.4.0 bw 兼容性,其中文本属性没有被清理.$selected = sanitize_title( $args['selected'] ) === $args['selected'] ?selected( $args['selected'], sanitize_title( $option ), false ) : selected( $args['selected'], $option, false );$html .= '' .esc_html( apply_filters( 'woocommerce_variation_option_name', $option ) ) .'在此处输出库存详细信息';}}}$html .= '';echo apply_filters('woocommerce_dropdown_variation_attribute_options_html', $html, $args );}}

我可以提取整个产品的库存水平,但现在是针对每个变体.

任何帮助将不胜感激.

解决方案

好的,首先你需要得到这样的产品变体:

$variations = $product->get_available_variations();

在期权循环中,您需要遍历变化并找到当前的期权股票状态

foreach ($variations as $variation) {if($variation['attributes'][$name] == $option) {$stock = $variation['is_in_stock'];}}

在变体循环之外,您需要添加有货和缺货变体的措辞

if( $stock == 1) {$stock_content = ' - 有货';} 别的 {$stock_content = ' - 缺货';}

然后更改 html 以包含附加变量 ($stock_content)

$html .= '<option value="' . esc_attr( $option ) . '" ' .$选择.'>'.esc_html( $option . $stock_content ) .'</选项>';

所以一个完整的函数看起来像这样:

add_filter( 'woocommerce_dropdown_variation_attribute_options_html', 'show_stock_status_in_dropdown', 10, 2);函数 show_stock_status_in_dropdown( $html, $args ) {$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' );//获取所有产品变体$variations = $product->get_available_variations();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 ) ) {$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 ) ) .'</选项>';}}} 别的 {foreach ( $options as $option ) {foreach ($variations 作为 $variation) {if($variation['attributes'][$name] == $option) {$stock = $variation['is_in_stock'];}}如果($股票== 1){$stock_content = ' - (有货)';} 别的 {$stock_content = '-(缺货)';}//这处理 <2.4.0 bw 兼容性,其中未清理文本属性.$selected = sanitize_title( $args['selected'] ) === $args['selected'] ?selected( $args['selected'], sanitize_title( $option ), false ) : selected( $args['selected'], $option, false );$html .= '<option value="' . esc_attr( $option ) . '" ' .$选择.'>'.esc_html( $option . $stock_content ) .'</选项>';}}}$html .= '';返回 $html;}

I would like to show the stock status (eg. In Stock / Out of Stock) for each product variation shown in the drop down list of variations on the Woocommerce Product Page. I have copied the relevant function to my theme's functions.php file, and can edit the content, but am unsure how to pull out the required stock status for each variation.


// Updated Woocommerce Product Variation Select 

if ( ! function_exists( 'wc_dropdown_variation_attribute_options' ) ) {

    /**
     * Output a list of variation attributes for use in the cart forms.
     *
     * @param array $args
     * @since 2.4.0
     */

     /*

    function wc_dropdown_variation_attribute_options( $args = array() ) {
        $args = wp_parse_args( apply_filters( 'woocommerce_dropdown_variation_attribute_options_args', $args ), array(
            'options'          => false,
            'attribute'        => false,
            'product'          => false,
            'selected'         => false,
            'name'             => '',
            'id'               => '',
            'class'            => '',
            'show_option_none' => __( 'Choose an option', 'woocommerce' ),
        ) );

        $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' ); // We'll do our best to hide the placeholder, but we'll need to show something when resetting options.

        if ( empty( $options ) && ! empty( $product ) && ! empty( $attribute ) ) {
            $attributes = $product->get_variation_attributes();
            $options    = $attributes[ $attribute ];
        }

        $html = '';
        $html .= '' . esc_html( $show_option_none_text ) . '';

        if ( ! empty( $options ) ) {
            if ( $product && taxonomy_exists( $attribute ) ) {
                // Get terms if this is a taxonomy - ordered. We need the names too.
                $terms = wc_get_product_terms( $product->get_id(), $attribute, array( 'fields' => 'all' ) );



                foreach ( $terms as $term ) {
                    if ( in_array( $term->slug, $options ) ) {
                        $html .= 'slug ) . '" ' . selected( sanitize_title( $args['selected'] ), $term->slug, false ) . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $term->name ) ) . ' ';
                    }
                }
            } else {
                foreach ( $options as $option ) {
                    // This handles lt 2.4.0 bw compatibility where text attributes were not sanitized.
                    $selected = sanitize_title( $args['selected'] ) === $args['selected'] ? selected( $args['selected'], sanitize_title( $option ), false ) : selected( $args['selected'], $option, false );

                    $html .= '' . esc_html( apply_filters( 'woocommerce_variation_option_name', $option ) ) . '  Output Stock Details Here ';
                }
            }
        }

        $html .= '';

        echo apply_filters( 'woocommerce_dropdown_variation_attribute_options_html', $html, $args );
    }
}

I can pull out the stock level for the overall product, but now for each variation.

Any help would be greatly appreciated.

解决方案

Ok, first you'll need to get product variations like this:

$variations = $product->get_available_variations();

And inside options loop, you need to loop through the variations and find the current option stock status

foreach ($variations as $variation) {
    if($variation['attributes'][$name] == $option) {
        $stock = $variation['is_in_stock'];

    }
}

Outside the variations loop you need to add the wording for in-stock and out-of-stock variations

if( $stock == 1) {
    $stock_content = ' - In stock';
} else {
    $stock_content = ' - Out of stock';
}

Then change the html to include an additional variable ($stock_content)

$html .= '<option value="' . esc_attr( $option ) . '" ' . $selected . '>' . esc_html( $option  .  $stock_content ) . '</option>'; 

So a complete function will look like this:

add_filter( 'woocommerce_dropdown_variation_attribute_options_html', 'show_stock_status_in_dropdown', 10, 2);
function show_stock_status_in_dropdown( $html, $args ) {
    $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' ); 

  // Get all product variations
    $variations = $product->get_available_variations();

    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 ) ) { 
          // Get terms if this is a taxonomy - ordered. We need the names too. 
          $terms = wc_get_product_terms( $product->get_id(), $attribute, array( 'fields' => 'all' ) ); 

          foreach ( $terms as $term ) { 
                if ( in_array( $term->slug, $options ) ) { 
                    $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 ) ) . '</option>'; 
                } 
            }
        } else {
            foreach ( $options as $option ) {
                    foreach ($variations as $variation) {
                        if($variation['attributes'][$name] == $option) {
                            $stock = $variation['is_in_stock'];
                        }
                    }       
                if( $stock == 1) {
                    $stock_content = ' - (In Stock)';
                } else {
                    $stock_content = ' - (Out of Stock)';
                }
                 // This handles < 2.4.0 bw compatibility where text attributes were not sanitized. 
                $selected = sanitize_title( $args['selected'] ) === $args['selected'] ? selected( $args['selected'], sanitize_title( $option ), false ) : selected( $args['selected'], $option, false ); 

                $html .= '<option value="' . esc_attr( $option ) . '" ' . $selected . '>' . esc_html( $option  .  $stock_content ) . '</option>'; 

            }
        } 
    } 

    $html .= '</select>'; 

    return $html;
}

这篇关于如何将变体库存状态添加到 Woocommerce 产品变体下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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