WordPress教程

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

阿里云

我想显示 WooCommerce 产品页面变体下拉列表中显示的每种产品差异的库存状态(例如库存/缺货)。我已将相关功能复制到我的主题的 functions.PHP 文件中,并且可以编辑内容,但我不确定如何为每个变体提取所需的库存状态。所以一个完整的函数将如下所示:

  1.     add_filter( 'woocommerce_dropdown_variation_attribute_options_HTML', 'show_stock_status_in_dropdown', 10, 2);
  2.     function show_stock_status_in_dropdown( $html, $args ) {
  3.         $options = $args['options']; 
  4.         $product = $args['product']; 
  5.         $attribute = $args['attribute']; 
  6.         $name = $args['name'] ? $args['name'] : 'attribute_' . sanitize_title( $attribute ); 
  7.         $id = $args['id'] ? $args['id'] : sanitize_title( $attribute ); 
  8.         $class = $args['class']; 
  9.         $show_option_none = $args['show_option_none'] ? true : false; 
  10.         $show_option_none_text = $args['show_option_none'] ? $args['show_option_none'] : __( 'Choose an option', 'woocommerce' ); 
  11.       // Get all product variations
  12.         $variations = $product->get_available_variations();
  13.         if ( empty( $options ) && ! empty( $product ) && ! empty( $attribute ) ) { 
  14.             $attributes = $product->get_variation_attributes(); 
  15.             $options = $attributes[ $attribute ]; 
  16.         } 
  17.         $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' ) . '">'; 
  18.         $html .= '<option value="">' . esc_html( $show_option_none_text ) . '</option>'; 
  19.         if ( ! empty( $options ) ) { 
  20.             if ( $product && taxonomy_exists( $attribute ) ) { 
  21.               // Get terms if this is a taxonomy - ordered. We need the names too. 
  22.               $terms = wc_get_product_terms( $product->get_id(), $attribute, array( 'fields' => 'all' ) ); 
  23.               foreach ( $terms as $term ) { 
  24.                     if ( in_array( $term->slug, $options ) ) { 
  25.                         $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>'; 
  26.                     } 
  27.                 }
  28.             } else {
  29.                 foreach ( $options as $option ) {
  30.                         foreach ($variations as $variation) {
  31.                             if($variation['attributes'][$name] == $option) {
  32.                                 $stock = $variation['is_in_stock'];
  33.                             }
  34.                         }       
  35.                     if( $stock == 1) {
  36.                         $stock_content = ' - (In Stock)';
  37.                     } else {
  38.                         $stock_content = ' - (Out of Stock)';
  39.                     }
  40.                      // This handles < 2.4.0 bw compatibility where text attributes were not sanitized. 
  41.                     $selected = sanitize_title( $args['selected'] ) === $args['selected'] ? selected( $args['selected'], sanitize_title( $option ), false ) : selected( $args['selected'], $option, false ); 
  42.                     $html .= '<option value="' . esc_attr( $option ) . '" ' . $selected . '>' . esc_html( $option  .  $stock_content ) . '</option>'; 
  43.                 }
  44.             } 
  45.         } 
  46.         $html .= '</select>'; 
  47.         return $html;
  48.     }
也想出现在这里?联系我们
创客主机

以上是编程之家为你收集整理的‘如何在 Woocommerce 产品变体可变属性下拉列表中添加库存状态’全部内容,希望文章能够帮你解决程序开发问题。

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

已有 182 人购买
查看演示升级 VIP立刻购买

收藏
(0)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!