Rumah > Artikel > pembangunan bahagian belakang > Bagaimana untuk Memaparkan Status Stok Variasi Produk dalam Dropdown WooCommerce?
Tutorial ini menangani keperluan untuk menunjukkan status stok (Dalam Stok/Kehabisan Stok ) variasi produk dalam senarai lungsur turun pada halaman produk WooCommerce. Dengan mengubah suai fungsi teras WooCommerce, kami boleh mendapatkan semula maklumat status stok untuk setiap variasi dan memaparkannya bersama pilihan variasi.
<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>
Atas ialah kandungan terperinci Bagaimana untuk Memaparkan Status Stok Variasi Produk dalam Dropdown WooCommerce?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!