向Woocommerce可变产品下拉列表中添加缺货库存状态 [英] Add backorders stock status to Woocommerce variable product dropdown

查看:213
本文介绍了向Woocommerce可变产品下拉列表中添加缺货库存状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在下拉菜单中显示各种产品的库存状态,包括待补",因为我网站上的大多数产品都可以待补而不是无货".

I would like to show the stock status of variable products in the dropdown menu, including 'on backorder' as most products on my site are available on backorder rather than being 'out of stock'.

我尝试了

I have tried the answer from How to add variation stock status to Woocommerce product variation dropdown however, every variable is listed as 'in stock' because the product is set to allow backorders.

我想像下面那样结合检查实际库存水平,但无法通过上面的链接使其正常工作.

I would like to incorporate checking the actual stock levels like below, but I can't get it to work properly with the above link.

$var_stock_count = $variation->get_stock_quantity();

// if there are 0 or less, display 'on backorder'
if( $var_stock_count <= 0 ) {
   return ' - (On Backorder)';
}
else {
   return ' - (In Stock)';
}

我需要帮助将这两段代码结合在一起.谢谢!

I need help incorporating the two pieces of code together. Thank you!

推荐答案

此更新的功能将处理未交货订单(当库存量小于1时):

This updated function that will handle products on backorders (when stock quantity is less than 1):

// Function that will check the stock status and display the corresponding additional text
function get_stock_status_text( $product, $name, $term_slug ){
    foreach ( $product->get_available_variations() as $variation ){
        if($variation['attributes'][$name] == $term_slug ) {
            $is_in_stock = $variation['is_in_stock'];
            $backordered = get_post_meta( $variation['variation_id'], '_backorders', true );
            $stock_qty   = get_post_meta( $variation['variation_id'], '_stock', true );
            break;
        }
    }
    $stock_status_text = $is_in_stock == 1 ? ' - (In Stock)' : ' - (Out of Stock)';
    return $backordered !== 'no' && $stock_qty <= 0 ? ' - (On Backorder)' : $stock_status_text;
}

代码进入您的活动子主题(或活动主题)的function.php文件中.经过测试,可以正常工作.

Code goes in function.php file of your active child theme (or active theme). Tested and works.

替换

Replaces the first function on this answer thread:

您将得到类似的东西:

这篇关于向Woocommerce可变产品下拉列表中添加缺货库存状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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