【问题标题】:Display price and stock status in dropdown variation attribute options of Woocommerce在 Woocommerce 的下拉变量属性选项中显示价格和库存状态
【发布时间】:2019-04-28 14:55:11
【问题描述】:

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

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

【问题讨论】:

    标签: php wordpress woocommerce dropdown variations


    【解决方案1】:

    enter image description hereprice and stock status in dropdown

    1:https://i.强文本stack.imgur.com/yvBUH.jpg


    非常感谢您(非常快)的回复。我使用 Flatsome 主题,效果很好。 作为一个附加问题: 是否有机会将“In Stock”转换为“Verfügbar / in Stock”和 缺货变成“Ausverkauft / 缺货”? 我们有来自德国和其他欧盟国家的客户;他们中的大多数人都会明白的。但是如果我不能用德语提供一些简短的话,我来自德国的老客户将面临问题。 再来一次;感谢您的工作。 ct

    【讨论】:

      【解决方案2】:

      注意:这仅在只有一个下拉选择字段时有效(因此变量产品中设置的变体只有一个属性)。使用多个属性(因此有多个下拉选择字段),它会根据库存状态属性的变化术语组合显示可能出错的内容。

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

      // 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;
      }
      

      代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且有效。

      基于:

      【讨论】:

      • 感谢 Loic,这非常有效。有没有办法从价格中去掉“包括 GST”(税收文本)?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-15
      • 2021-06-01
      • 1970-01-01
      • 2019-08-28
      • 2016-05-09
      • 2019-07-17
      相关资源
      最近更新 更多