根据库存数量和SKU灰色的产品变化

发布于 2025-02-05 16:40:59 字数 725 浏览 3 评论 0原文

我有一个可变产品,具有2种不同的变化。两种变体都在产品级别使用库存。股票为3,我有以下两个变化:

  • 用1个积分购买
  • 3个学分,

该产品的股票为3,当有3个学分购买产品的人时,也会通过自定义从股票中扣除3股。 - 写入功能,使库存变为0,并且产品不再可见。

当某人以1个积分购买产品时,将1从股票中扣除,因此股票变为2。如果发生这种情况,我希望“以3个学分购买”的变体变得不活跃或不可见。

当股票相等或小于2时,如何检查功能,并确保“使用3个学分购买”变化不再可见或不活动? -SKU总是以“ 003 *****”开头,

因此,如果产品库存等于或小于2,而SKU以003开头,则产品变化将不会显示。


我已经有效了,我还能还能检查该变量的SKU是否以“ 003”开头?

add_filter( 'woocommerce_variation_is_active', 'grey_out_variations_out_of_stock', 10, 2 );
 
function grey_out_variations_out_of_stock( $is_active, $variation ) {
    if ( $variation->get_stock_quantity() == 2 ) return false;
    return $is_active;
}

I have a variable product with 2 different variations. Both variations use inventory at the product level. The stock is at 3 and I have the following two variations:

  • Buy with 1 credit
  • Buy with 3 credits

The stock of the product is 3 and when someone with 3 credits buys the product, 3 is also deducted from the stock by means of a custom-written function, so that the stock becomes 0 and the product is no longer visible.

When someone buys the product with 1 credit, 1 is deducted from the stock, so that the stock becomes 2. If this happens I would like the variation "Buy with 3 credits" to become inactive or not visible anymore.

How can I check through a function when the stock is equal or less than 2 and make sure that "Buy with 3 credits" variation is no longer visible or becomes inactive? - SKU always starts with "003*****"

So if product inventory is equal or less than 2 and SKU starts with 003, then the product variation will not show.


I already got this working, how can I also check if the SKU of this variable starts with "003"?

add_filter( 'woocommerce_variation_is_active', 'grey_out_variations_out_of_stock', 10, 2 );
 
function grey_out_variations_out_of_stock( $is_active, $variation ) {
    if ( $variation->get_stock_quantity() == 2 ) return false;
    return $is_active;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若能看破又如何 2025-02-12 16:41:00

您可以使用$ variation-> get_sku()str_starts_with() - (php 8),它将检查字符串是否从某个子字符串开始。

因此,您得到:

function filter_woocommerce_variation_is_active( $active, $variation ) {
    // Get SKU
    $sku = $variation->get_sku();

    if ( $variation->get_stock_quantity() <= 2 && str_starts_with( $sku, '003' ) ) {
        $active = false;
    }
    
    return $active;
}
add_filter( 'woocommerce_variation_is_active', 'filter_woocommerce_variation_is_active', 10, 2 );

注意:对于PHP 7.0或以上的php,您可以使用substr($ sku,0,3)==='003'

You can use $variation->get_sku() and str_starts_with() - (PHP 8), which will check if a string starts with a certain substring.

So you get:

function filter_woocommerce_variation_is_active( $active, $variation ) {
    // Get SKU
    $sku = $variation->get_sku();

    if ( $variation->get_stock_quantity() <= 2 && str_starts_with( $sku, '003' ) ) {
        $active = false;
    }
    
    return $active;
}
add_filter( 'woocommerce_variation_is_active', 'filter_woocommerce_variation_is_active', 10, 2 );

Note: for PHP 7.0 or older you can use substr( $sku, 0, 3 ) === '003'

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文