根据库存数量和SKU灰色的产品变化
我有一个可变产品,具有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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
$ variation-> get_sku()
和str_starts_with()
- (php 8),它将检查字符串是否从某个子字符串开始。因此,您得到:
注意:对于PHP 7.0或以上的php,您可以使用
substr($ sku,0,3)==='003'
You can use
$variation->get_sku()
andstr_starts_with()
- (PHP 8), which will check if a string starts with a certain substring.So you get:
Note: for PHP 7.0 or older you can use
substr( $sku, 0, 3 ) === '003'