自訂庫存狀態,例如「預訂」和「聯絡我們” WooCommerce 4 中的產品選項中缺少“,”。
將以下代碼添加到您的functions.php 文件中:
// Add new stock status options add_filter( 'woocommerce_product_stock_status_options', 'filter_woocommerce_product_stock_status_options', 10, 1 ); function filter_woocommerce_product_stock_status_options( $status ) { $status['pre_order'] = __( 'Pre order', 'woocommerce' ); $status['contact_us'] = __( 'Contact us', 'woocommerce' ); return $status; } // Availability text add_filter( 'woocommerce_get_availability_text', 'filter_woocommerce_get_availability_text', 10, 2 ); function filter_woocommerce_get_availability_text( $availability, $product ) { switch( $product->get_stock_status() ) { case 'pre_order': $availability = __( 'Pre order', 'woocommerce' ); break; case 'contact_us': $availability = __( 'Contact us', 'woocommerce' ); break; } return $availability; } // Availability CSS class add_filter( 'woocommerce_get_availability_class', 'filter_woocommerce_get_availability_class', 10, 2 ); function filter_woocommerce_get_availability_class( $class, $product ) { switch( $product->get_stock_status() ) { case 'pre_order': $class = 'pre-order'; break; case 'contact_us': $class = 'contact-us'; break; } return $class; } // Admin stock html add_filter( 'woocommerce_admin_stock_html', 'filter_woocommerce_admin_stock_html', 10, 2 ); function filter_woocommerce_admin_stock_html( $stock_html, $product ) { switch( $product->get_stock_status() ) { case 'pre_order': $stock_html = '<mark class="pre-order" style="background:transparent none;color:#33ccff;font-weight:700;line-height:1;">' . __( 'Pre order', 'woocommerce' ) . '</mark>'; break; case 'contact_us': $stock_html = '<mark class="contact-us" style="background:transparent none;color:#cc33ff;font-weight:700;line-height:1;">' . __( 'Contact us', 'woocommerce' ) . '</mark>'; break; } return $stock_html; }
補充說明:
以上是如何為 WooCommerce 產品新增「預訂」和「聯絡我們」等自訂庫存狀態?的詳細內容。更多資訊請關注PHP中文網其他相關文章!