如何根据WooCommerce中的不同情况更改单个产品页面上的库存管理可用性文本 [英] How to change the 'stock management' availability text on single product page based on different conditions in WooCommerce

查看:16
本文介绍了如何根据WooCommerce中的不同情况更改单个产品页面上的库存管理可用性文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据WooCommerce中的不同条件更改单个产品页面上的stock management可用性文本。

根据onoff是否启用产品级库存管理,有5种情况。

  1. 打开,数量:0
  2. 打开,数量:2
  3. 打开,数量:3+
  4. 已关闭,缺货
  5. 已关闭,库存

我正在使用

add_filter( 'woocommerce_get_availability', 'wcs_custom_get_availability', 1, 2);
function wcs_custom_get_availability( $availability, $_product ) {
   global $product;

    // Case 3
    if ( $_product->is_in_stock() ) {
        $availability['availability'] = __('3. Turned on, Quantity: 3+', 'woocommerce');
    }

    // Case 2
    if ( $_product->is_in_stock() && $product->get_stock_quantity() <= 2 ) {
        $availability['availability'] = sprintf( __('2. Turned on, Quantity: 2', 'woocommerce');
    }

   // Case 5
    if ( $_product->is_in_stock() && $product->get_stock_quantity() <= 0 ) {
        $availability['availability'] = sprintf( __('5. Turned off, In stock', 'woocommerce'));
    }
    
    // Case 4 and 1 - out of stock
    if ( ! $_product->is_in_stock() ) {
        $availability['availability'] = __('4. Turned off, Out of stock / 1. Turned on, Quantity: 0', 'woocommerce');
    }

    return $availability;
}

但如何使其适用于可变产品?

对于简单的产品,它可以完美地工作,但对于可变的产品,2(满分5)的情况显示错误的结果:

  1. 打开,数量:0[工程正常]
  2. 打开,数量:2[错误-显示案例编号5]
  3. 打开,数量:3+[错误-显示案例编号5]
  4. 已关闭,缺货[工作正常]
  5. 已关闭,库存[工作正常]

推荐答案

If$product->get_stock_quantity() <= 0$_product->is_in_stock()将始终为False,因为库存编号小于0或等于0,因此您的逻辑中存在错误。

global $product$_product也不必使用和混合使用。因为$_product已包含产品对象。

  • $product->managing_stock()可能会派上用场

因此您可以改用:

// Change In Stock Text
function filter_woocommerce_get_availability( $availability, $product ) {       
    // Managing stock is activated
    if ( $product->managing_stock() ) {
        // Stock quantity
        $stock_quantity = $product->get_stock_quantity();
        
        // Compare
        if ( $stock_quantity <= 0 ) {
            $availability['availability'] = __('1. Turned on, Quantity: less then or equal to 0', 'woocommerce' );
        } elseif ( $stock_quantity > 0 && $stock_quantity <= 2 ) {
            $availability['availability'] = __('2. Turned on, Quantity: 1 or 2', 'woocommerce' );           
        } elseif ( $stock_quantity > 3 )  {
            $availability['availability'] = __('3. Turned on, Quantity: 3+', 'woocommerce' );
        }
    } else {
        // In stock
        if ( $product->is_in_stock() ) {
            $availability['availability'] = __('4. Turned off, In stock', 'woocommerce' );      
        } else {
            $availability['availability'] = __('5. Turned off, Out of stock', 'woocommerce' );              
        }
    }

    return $availability;
}
add_filter( 'woocommerce_get_availability', 'filter_woocommerce_get_availability', 10, 2 );

这篇关于如何根据WooCommerce中的不同情况更改单个产品页面上的库存管理可用性文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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