在WooCommerce中添加自定义库存状态 [英] Add a custom stock status in WooCommerce

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

问题描述

我想在产品的股票期权下拉列表中添加一个新期权.默认情况下,有缺货",有货",我想添加第三个选项.

I would like to add a new option to the dropdown list of stocks options for a product. By default, there is "Out of stock", "In stock" and I would like to add a third option.

我找到了显示下拉列表的方法(在class-wc-meta-box-product-data.php中)

I found the method that displays the dropdown ( in class-wc-meta-box-product-data.php )

    // Stock status
    woocommerce_wp_select( array( 'id' => '_stock_status', 'wrapper_class' => 'hide_if_variable', 'label' => __( 'Stock status', 'woocommerce' ), 'options' => array(
        'instock' => __( 'In stock', 'woocommerce' ),
        'outofstock' => __( 'Out of stock', 'woocommerce' )
    ), 'desc_tip' => true, 'description' => __( 'Controls whether or not the product is listed as "in stock" or "out of stock" on the frontend.', 'woocommerce' ) ) );

    do_action( 'woocommerce_product_options_stock_status' );

但是我不想直接编辑Woocommerce类,这样我们就可以更新Woocommerce而不会丢失任何自定义代码.有没有方法可以覆盖此方法?

But I don't want to edit Woocommerce class directly, so that we can update Woocommerce without losing any custom code. Is there a way to override this method ?

推荐答案

对于有兴趣的人,这是基于Laila的方法的完整解决方案.警告!我的解决方案只能在禁用WooCommerce 管理库存"选项的情况下使用!我没有处理库存中确切数量的物品.像往常一样,所有代码都转到 functions.php .

for anyone interested, here is complete solution, based on Laila's approach. Warning! My solution is intended to work only with WooCommerce "manage stock" option disabled! I am not working with exact amounts of items in stock. All code goes to functions.php, as usual.

正在删除本地库存状态下拉字段.添加CSS类以区分我的新自定义字段.下拉菜单现在提供了新的选项"On Request".

Removing native stock status dropdown field. Adding CSS class to distinguish my new custom field. Dropdown has now new option "On Request".

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' => 'hide_if_variable custom-stock-status', 'label' => __( 'Stock status', 'woocommerce' ), 'options' => array(
        'instock' => __( 'In stock', 'woocommerce' ),
        'outofstock' => __( 'Out of stock', 'woocommerce' ),
        'onrequest' => __( 'On Request', 'woocommerce' ), // The new option !!!
    ), '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');

遗憾的是, WooCommerce将仅使用其本机功能保存库存"或缺货"值.因此,在处理完所有产品数据之后,我必须再次保存我的库存状态.

Sadly, WooCommerce will save only "instock" or "outofstock" values with its native functions. So after all product data processing, I have to re-save my stock status again.

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

模板部分

最后一件事-我必须更改产品 get_availability()函数返回的数据.禁用管理库存"后,WooCommerce再次仅知道库存"和缺货"值.因此,我可以自己查看库存状态.

Template part

And the last thing - I have to alter data returned by product get_availability() function. When "managing stock" is off, WooCommerce only knows "instock" and "outofstock" values, again. So I have check stock status on my own.

function woocommerce_get_custom_availability( $data, $product ) {
    switch( $product->stock_status ) {
        case 'instock':
            $data = array( 'availability' => __( 'In stock', 'woocommerce' ), 'class' => 'in-stock' );
        break;
        case 'outofstock':
            $data = array( 'availability' => __( 'Out of stock', 'woocommerce' ), 'class' => 'out-of-stock' );
        break;
        case 'onrequest':
            $data = array( 'availability' => __( 'On request', 'woocommerce' ), 'class' => 'on-request' );
        break;
    }
    return $data;
}
add_action('woocommerce_get_availability', 'woocommerce_get_custom_availability', 10, 2);

也许这不是防弹解决方案……最终我将对其进行更新.

Maybe it's not bulletproof solution ... I will update it, eventually.

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

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