栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

好的,首先,您需要获得以下产品变体:

$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 ) . '"  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;}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/466160.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号