简体   繁体   English

在WooCommerce添加自定义库存状态视为缺货

[英]Add custom stock status in WooCommerce treated as out of stock

I created 2 new custom stock statuses:我创建了 2 个新的自定义库存状态:

  1. Out of stock (Permanent)缺货(永久)
  2. Out of stock (Supplier)缺货(供应商)

As you can guess, I want them both to be treated as out of stock.您可以猜到,我希望它们都被视为缺货。


I used some extra code to hide the products with these custom statuses from the front end but I can not hide them from the ajax search (live) and also someone can add the product in cart.我使用了一些额外的代码来从前端隐藏具有这些自定义状态的产品,但我无法从 ajax 搜索(实时)中隐藏它们,而且有人可以将产品添加到购物车中。

(I use the Flatsome theme and the live search is from there) (我使用 Flatsome 主题,实时搜索来自那里)

Based on: How to add custom stock status to products in WooCommerce 4+ & Hide all products with a specific stock status from WooCommerce catalog , this is my code attempt:基于: How to add custom stock status to products in WooCommerce 4+ & Hide all products with a specific stock status from WooCommerce catalog ,这是我的代码尝试:

// Add new stock status options
function filter_woocommerce_product_stock_status_options( $status ) {
    // Add new statuses
    $status['permanent'] = __( 'Out of stock (Permanent)', 'woocommerce' );
    $status['supplier'] = __( 'Out of stock (Supplier)', '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 'permanent':
            $availability = __( 'Out of stock (Permanent)', 'woocommerce' );
        break;
        case 'supplier':
            $availability = __( 'Out of stock (Supplier)', '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 'permanent':
            $class = 'permanent';
        break;
        case 'supplier':
            $class = 'supplier';
        break;
    }

    return $class;
}
add_filter( 'woocommerce_get_availability_class', 'filter_woocommerce_get_availability_class', 10, 2 );

// 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();
            
        }
    }
    
    // Stock status
    switch( $product_stock_status ) {
        case 'permanent':
            $stock_html = '<mark class="permanent" style="background:transparent none;color:#33ccff;font-weight:700;line-height:1;">' . __( 'Out of stock (Permanent)', 'woocommerce' ) . '</mark>';
        break;
        case 'supplier':
            $stock_html = '<mark class="supplier" style="background:transparent none;color:#cc33ff;font-weight:700;line-height:1;">' . __( 'Out of stock (Supplier)', 'woocommerce' ) . '</mark>';
        break;
    }
 
    return $stock_html;
}
add_filter( 'woocommerce_admin_stock_html', 'filter_woocommerce_admin_stock_html', 10, 2 );


//hide specific stock status
add_action( 'woocommerce_product_query_meta_query', 'custom_product_query_meta_query', 1000 );
function custom_product_query_meta_query( $meta_query ) {
    if ( ! is_admin() ) {
        $meta_query[] = array(
            'key'     => '_stock_status',
            'value'   => 'permanent',
            'compare' => '!=',
        );
    }
    if ( ! is_admin() ) {
        $meta_query[] = array(
            'key'     => '_stock_status',
            'value'   => 'supplier',
            'compare' => '!=',
        );
    }
    return $meta_query;
}

Any advice on how to handle both custom stock statuses as out of stock?关于如何将两种自定义库存状态处理为缺货的任何建议?

Since you indicate in your question that your custom stock statuses should contain the same functionality as the current 'outofstock' status, you can instead of rewrite/reuse all existing functionality of the 'outofstock' status for your custom stock statuses, use a workaround.由于您在问题中指出您的自定义库存状态应包含与当前“缺货”状态相同的功能,因此您可以使用一种解决方法,而不是为您的自定义库存状态重写/重用“缺货”状态的所有现有功能。

This will offer a simple solution, since otherwise you would have to overwrite some template files in addition to writing custom code.这将提供一个简单的解决方案,否则除了编写自定义代码外,您还必须覆盖一些模板文件。

The workaround can be applied as follows:解决方法可以应用如下:

Step 1) add an extra custom field for your custom statuses, we will add this field under the existing stock status field:第 1 步)为您的自定义状态添加一个额外的自定义字段,我们将在现有库存状态字段下添加此字段:

// Add custom field
function action_woocommerce_product_options_stock_status() {
    // Custom stock status
    $options = array(
        'empty'     => __( 'N/A', 'woocommerce' ),
        'permanent' => __( 'Permanent', 'woocommerce' ),
        'supplier'  => __( 'Supplier', 'woocommerce' ),
    );

    woocommerce_wp_select(
        array(
            'id'            => '_custom_stock_status',
            'wrapper_class' => 'stock_status_field hide_if_variable hide_if_external hide_if_grouped',
            'label'         => __( 'Custom stock status', 'woocommerce' ),
            'options'       => $options,
            'desc_tip'      => true,
            'description'   => __( 'Your description', 'woocommerce' ),
        )
    );
}
add_action( 'woocommerce_product_options_stock_status', 'action_woocommerce_product_options_stock_status', 10 );

// Save custom field
function action_woocommerce_admin_process_product_object( $product ) {
    // Isset
    if ( isset( $_POST['_custom_stock_status'] ) ) {        
        // Update
        $product->update_meta_data( '_custom_stock_status', sanitize_text_field( $_POST['_custom_stock_status'] ) );
    }
}
add_action( 'woocommerce_admin_process_product_object', 'action_woocommerce_admin_process_product_object', 10, 1 );

Result:结果:

自定义库存状态


Step 2) when the product is 'out of stock', we will check whether a custom stock status has been set. Step 2)当商品“缺货”时,我们会检查是否设置了自定义库存状态。 If this is the case, we will visually change the text using the code below, so that the customer can see the new status.如果是这种情况,我们将使用下面的代码直观地更改文本,以便客户可以看到新状态。 In the background/backend, however, the 'out of stock' status is still used and therefore automatically also the existing functionality:然而,在后台/后端,仍然使用“缺货”状态,因此也会自动使用现有功能:

// Availability text
function filter_woocommerce_get_availability_text( $availability, $product ) {
    // Only for 'outofstock'
    if ( $product->get_stock_status() == 'outofstock' ) {
        // Get custom stock status
        $custom_stock_status = $product->get_meta( '_custom_stock_status' );

        // Compare and apply new text
        if ( $custom_stock_status == 'permanent' ) {
            $availability = __( 'Out of stock (Permanent)', 'woocommerce' );
        } elseif ( $custom_stock_status == 'supplier' ) {
            $availability = __( 'Out of stock (Supplier)', 'woocommerce' );
        }
    }

    return $availability; 
}
add_filter( 'woocommerce_get_availability_text', 'filter_woocommerce_get_availability_text', 10, 2 );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM