在 WooCommerce 中添加自定义库存状态

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

问题描述

限时送ChatGPT账号..

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

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 类以区分我的新自定义字段.下拉菜单现在有新选项应要求".

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 再次只知道instock"和outofstock"值.所以我自己检查库存状态.

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 中添加自定义库存状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:WooCommerce 获取订单总数 下一篇:在 Woocommerce 结帐页面中添加城市下拉列表

相关文章

最新文章