当前位置: 首页 > 面试题库 >

如何在Woocommerce产品变体下拉列表中添加变体库存状态

包承望
2023-03-14
问题内容

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

//更新了Woocommerce产品版本选择

如果(!function_exists('wc_dropdown_variation_attribute_options')){

    / **
     *输出用于购物车表单的变体属性列表。
     *
     * @参数数组$ args
     * @自2.4.0起
     * /

     / *

    函数wc_dropdown_variation_attribute_options($ args = array()){
        $ args = wp_parse_args(apply_filters('woocommerce_dropdown_variation_attribute_options_args',$ args),数组(
            '选项'=>否,
            'attribute'=>否,
            '产品'=>否,
            '已选择'=>否,
            '名称'=>'',
            'id'=>'',
            'class'=>'',
            '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(空($ options)&&!空($ product)&&!空($ attribute)){
            $ attributes = $ product-> get_variation_attributes();
            $ options = $ attributes [$ attribute];
        }

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

        如果(!empty($ options)){
            如果($ product && taxonomy_exists($ attribute)){
                //如果这是一个分类法,则获取术语-已排序。我们也需要名称。
                $ terms = wc_get_product_terms($ product-> get_id(),$ attribute,array('fields'=>'all'));



                foreach($ terms as $ term){
                    如果(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。='';

        回声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 = ' - In stock';
} else {
    $stock_content = ' - Out of stock';
}

然后更改html以包含其他变量($ stock_content)

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

因此,完整的功能应如下所示:

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产品页面上的变种下拉列表中显示每个产品变种的库存状态(例如,库存中/脱销)。我已经将相关函数复制到主题的functions.php文件中,并且可以编辑内容,但是不确定如何为每个变体提取所需的库存状态。 我可以取出整个产品的库存水平,但现在是每个变体的库存水平。 null

  • 但如果我尝试添加表单短代码或其他HTML(如iFrame),它就会被删除,并且不会呈现?!

  • 目标:在一个按钮点击中添加多个可变产品到购物车。 我试着将这段代码放入循环中,但它不起作用。 但是这种方法不能解决问题,因为我需要在一个按钮点击中添加几个产品变体。 所有的建议都将不胜感激。多谢了。

  • 我希望在变体下拉列表中显示每个woocommerce产品变体的价格和库存状态,如下所示:价格-变体名称-库存状态。 下面的代码给我的价格和变化名称在下拉列表中,但我不确定添加每个变化所需的额外代码当前库存状态? 提前感谢你所有的帮助。

  • 我正在使用名为高级定制字段(ACF)和WooCommerce的插件。我想为WooCommerce产品变体创建一个文本和图像字段。我在ACF中创建了字段,并将它们分配给。 但是在下没有看到这些字段。我搜索了,看起来我必须写一个自定义代码来适应这些字段。我试着搜索这个问题,我创建的大多数建议和教程都是关于通过代码创建自定义字段,而不是使用ACF,这对我没有帮助,因为字段必须使用ACF,这是因为我使用V