如何在WooCommerce 4+中为产品添加自定义库存状态 [英] How to add custom stock status to products in WooCommerce 4+

查看:54
本文介绍了如何在WooCommerce 4+中为产品添加自定义库存状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码在WooCommerce 4.0中添加新的库存状态.

I am using the following code to add new stock statuses in WooCommerce 4.0.

新状态为:

  • 预订
  • 联系我们
function add_custom_stock_type() {
    ?>
    <script type="text/javascript">
    jQuery(function(){
        jQuery('._stock_status_field').not('.custom-stock-status').remove();
    });
    </script>
<?php   

    woocommerce_wp_select( array( 'id' => '_stock_status', 'wrapper_class' => 'custom-stock-status', 'label' => __( 'Stock status', 'woocommerce' ), 'options' => array(
        'instock'     => __( 'Available', 'woocommerce' ), //changed the name
        'outofstock'  => __( 'Sold out', 'woocommerce' ), //changed the name
        'onbackorder' => __( 'Preorder : Pending Distributor release', 'woocommerce' ), //changed the name
        'contact'     => __( 'Contact us for Availability', 'woocommerce' ), //added new one
        'preorder'    => __( 'On Preorder: Pending Distributor release', 'woocommerce' ), //added new one
    ), 'desc_tip' => true, 'description' => __( 'Controls whether or not the product is listed as "in stock" or "out of stock" on the frontend.', 'woocommerce' ) ) );
}
add_action('woocommerce_product_options_stock_status', 'add_custom_stock_type');

function save_custom_stock_status( $product_id ) {
    update_post_meta( $product_id, '_stock_status', wc_clean( $_POST['_stock_status'] ) );
}
add_action('woocommerce_process_product_meta', 'save_custom_stock_status',99,1);

function woo_add_custom_general_fields_save_two( $post_id ){
    // Select
    $woocommerce_select = $_POST['_stock_status'];
    if( !empty( $woocommerce_select ) )
        update_post_meta( $post_id, '_stock_status', esc_attr( $woocommerce_select ) );
    else
    update_post_meta( $post_id, '_stock_status', '' );
    }

function woocommerce_get_custom_availability( $data, $product ) {
    switch( $product->stock_status ) {
        case 'instock':
            $data = array( 'availability' => __( 'Available', 'woocommerce' ), 'class' => 'in-stock' ); //changed name
        break;
        case 'outofstock':
            $data = array( 'availability' => __( 'Sold Out', 'woocommerce' ), 'class' => 'out-of-stock' ); //changed name
        break;
        case 'onbackorder':
            $data = array( 'availability' => __( 'Preorder : Pending Distributor release', 'woocommerce' ), 'class' => 'onbackorder' ); //changed name
        break;
        case 'contact':
            $data = array( 'availability' => __( 'Contact us for Availability', 'woocommerce' ), 'class' => 'contact' ); //added new one
        break;
        case 'preorder':
            $data = array( 'availability' => __( 'On Preorder : Pending Distributor release', 'woocommerce' ), 'class' => 'preorder' ); //added new one
        break;
    }
    return $data;
}
add_action('woocommerce_get_availability', 'woocommerce_get_custom_availability', 10, 4);


作品:


Works:

  • 后端:新状态已添加到下拉菜单中,我可以选择所需的状态.

不起作用:

  • 前端:单个产品页面上的状态不正确.

  • Front end: on single product page is not showing the correct status.

后端:在管理产品列表中显示新状态

Backend: Display the new status on the admin products list table

有人可以帮助我吗?

推荐答案

最后更新:04/21-在WordPress 5.7.1和更高版本中进行了测试WooCommerce 5.2.2

Last update: 04/21 - Tested in WordPress 5.7.1 & WooCommerce 5.2.2

使用 woocommerce_product_stock_status_options

代替 woocommerce_product_options_stock_status .

这样,您可以立即添加状态,而不是替换现有的下拉列表

This way you can immediately add a status instead of replace the existing dropdown


还使用 woocommerce_get_availability_text & woocommerce_get_availability_class woocommerce_get_availability 相反.

通过这种方式,您不必再次添加现有状态

This way you don't have to add the existing statuses again

// Add new 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 );

// Availability text
function filter_woocommerce_get_availability_text( $availability, $product ) {
    // Get stock status
    switch( $product->get_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 );

// Availability CSS class
function filter_woocommerce_get_availability_class( $class, $product ) {
    // Get stock status
    switch( $product->get_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 );


使用 woocommerce_admin_stock_html 在管理产品列表表上显示新的库存状态

Use woocommerce_admin_stock_html to display the new stock status on the admin products list table

// Admin 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 );

如果需要,可以在已访问 $ product 对象或使用 global $ product 的挂钩中使用自定义库存状态.

If desired, the custom stock status can be used in hooks where you already have access to the $product object or you use global $product.

// An example based on global $product
// Get the global product object
global $product;

// Is a WC product
if ( is_a( $product, 'WC_Product' ) ) {
    // Get stock status
    $product_stock_status = $product->get_stock_status();
    
    // Compare
    if ( $product_stock_status == 'My custom stock status' ) {
        // Etc..
    }
}

这篇关于如何在WooCommerce 4+中为产品添加自定义库存状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆