Cara Menambah Status Stok Tersuai pada Produk dalam WooCommerce 4
Latar Belakang
WooCommerce oleh lalai menyediakan beberapa status stok, termasuk Dalam Stok, Habis Stok dan Atas Pesanan Belakang. Walau bagaimanapun, anda mungkin perlu membuat status stok tersuai untuk menampung keperluan pengurusan inventori khusus anda.
Mencipta Status Stok Tersuai
Untuk membuat status stok tersuai, gunakan kod berikut coretan dalam fail functions.php tema anda atau pemalam tersuai:
<code class="php">use WooCommerce\Admin\HTML; use WooCommerce\Utilities\Product;</code>
<code class="php">/** * Add custom stock status options. * * @param array $status Existing stock status options. * @return array Updated stock status options. */ function filter_woocommerce_product_stock_status_options( $status ) { // Add new statuses $status['pre_order'] = __( 'Pre order', 'woocommerce' ); $status['contact_us'] = __( 'Contact us', 'woocommerce' ); return $status; } add_filter( 'woocommerce_product_stock_status_options', 'filter_woocommerce_product_stock_status_options', 10, 1 ); /** * Modify availability text based on stock status. * * @param string $availability Availability text. * @param WC_Product $product Product object. * @return string Modified availability text. */ function filter_woocommerce_get_availability_text( $availability, $product ) { // Get stock status $product_stock_status = $product->get_stock_status(); // Modify availability text switch ( $product_stock_status ) { case 'pre_order': $availability = __( 'Pre order', 'woocommerce' ); break; case 'contact_us': $availability = __( 'Contact us', 'woocommerce' ); break; } return $availability; } add_filter( 'woocommerce_get_availability_text', 'filter_woocommerce_get_availability_text', 10, 2 ); /** * Modify availability CSS class based on stock status. * * @param string $class CSS class. * @param WC_Product $product Product object. * @return string Modified CSS class. */ function filter_woocommerce_get_availability_class( $class, $product ) { // Get stock status $product_stock_status = $product->get_stock_status(); // Modify CSS class switch ( $product_stock_status ) { case 'pre_order': $class = 'pre-order'; break; case 'contact_us': $class = 'contact-us'; break; } return $class; } add_filter( 'woocommerce_get_availability_class', 'filter_woocommerce_get_availability_class', 10, 2 ); // Admin side /** * Modify stock HTML on admin products list table. * * @param string $stock_html Stock HTML. * @param WC_Product $product Product object. * @return string Modified stock HTML. */ function filter_woocommerce_admin_stock_html( $stock_html, $product ) { // Simple if ( $product->is_type('simple') ) { // Get stock status $product_stock_status = $product->get_stock_status(); // Variable } elseif ( $product->is_type('variable') ) { foreach ( $product->get_visible_children() as $variation_id ) { // Get product $variation = wc_get_product( $variation_id ); // Get stock status $product_stock_status = $variation->get_stock_status(); /* Currently the status of the last variant in the loop will be displayed. So from here you need to add your own logic, depending on what you expect from your custom stock status. By default, for the existing statuses. The status displayed on the admin products list table for variable products is determined as: - Product should be in stock if a child is in stock. - Product should be out of stock if all children are out of stock. - Product should be on backorder if all children are on backorder. - Product should be on backorder if at least one child is on backorder and the rest are out of stock. */ } } // Stock status switch ( $product_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; } add_filter( 'woocommerce_admin_stock_html', 'filter_woocommerce_admin_stock_html', 10, 2 );</code>
Menambah pada Produk Tunggal
Setelah anda mencipta status tersuai, anda perlu menambahkannya pada produk individu.
Paparan Hadapan
Status stok tersuai kini akan muncul di bahagian hadapan tapak anda, kedua-duanya pada halaman produk tunggal dan arkib produk.
Penyelesaian masalah
Jika status tersuai tidak dipaparkan pada bahagian hadapan atau dalam pentadbir, pastikan coretan kod ditambahkan dengan betul pada tema atau pemalam anda dan penapis dinamakan dan disambungkan dengan betul.
Atas ialah kandungan terperinci Bagaimana untuk Mencipta dan Menggunakan Status Stok Tersuai dalam WooCommerce 4?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!