本教學解決了顯示庫存狀態(有貨/缺貨)的需要) WooCommerce 產品頁面下拉清單中的產品變體。透過修改核心 WooCommerce 函數,我們可以檢索每個變體的庫存狀態資訊並將其與變體選項一起顯示。
<code class="php">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 ) ) { // Add the 'get_stock_status' function to retrieve and display the stock status $stock_status = get_stock_status( $product, $attribute, $term->slug ); $html .= 'slug ) . '" ' . selected( sanitize_title( $args['selected'] ), $term->slug, false ) . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $term->name ) ) . ' ' . $stock_status . ' '; } } } 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 ); // Add the 'get_stock_status' function to retrieve and display the stock status $stock_status = get_stock_status( $product, $attribute, $option ); $html .= '' . esc_html( apply_filters( 'woocommerce_variation_option_name', $option ) ) . ' ' . $stock_status . ' '; } } } $html .= ''; echo apply_filters( 'woocommerce_dropdown_variation_attribute_options_html', $html, $args ); }</code>
<code class="php">// Retrieve and display the stock status of a specific product variation function get_stock_status( $product, $attribute, $term ) { foreach ( $product->get_available_variations() as $variation ) { if ( $variation['attributes'][$attribute] == $term ) { $stock = $variation['is_in_stock'] ? 'In Stock' : 'Out of Stock'; break; } } return ' - (' . $stock . ')'; }</code>
<code class="php">add_filter( 'woocommerce_dropdown_variation_attribute_options_html', 'wc_dropdown_variation_attribute_options', 10, 2 );</code>
以上是如何在 WooCommerce 下拉清單中顯示產品變體庫存狀態?的詳細內容。更多資訊請關注PHP中文網其他相關文章!