自定义库存状态,例如“预订”和“联系我们” ,”在 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中文网其他相关文章!