2 回答

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超6個(gè)贊
將總庫(kù)存數(shù)量與新添加的設(shè)置進(jìn)行比較$multiplier
注釋并添加到代碼中的解釋
function filter_woocommerce_variation_is_active( $active, $variation ) {
// Get multiplier
$multiplier = get_post_meta( $variation->get_variation_id(), '_stock_multiplier', true );
// NOT empty
if ( ! empty( $multiplier ) ) {
// Get stock quantity
$var_stock_count = $variation->get_stock_quantity();
// Stock quantity < multiplier
if( $var_stock_count < $multiplier ) {
$active = false;
}
}
return $active;
}
add_filter( 'woocommerce_variation_is_active', 'filter_woocommerce_variation_is_active', 10, 2 );

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超4個(gè)贊
它不起作用,因?yàn)椋?/p>
$item
您的代碼中未定義變量。您的自定義字段在父變量產(chǎn)品中定義。
所以你需要更換:
$multiplier = $item->get_product()->get_meta( '_stock_multiplier' );
通過(guò)以下方式(從父變量產(chǎn)品獲取數(shù)據(jù)):
$multiplier = get_post_meta( $variation->get_parent_id(), '_stock_multiplier', true );
所以在你的代碼中:
add_filter( 'woocommerce_variation_is_active', 'my_jazzy_function', 10, 2 );
function my_jazzy_function( $active, $variation ) {
// Get multiplier
if( $multiplier = get_post_meta( $variation->get_parent_id(), '_stock_multiplier', true ) {
// Get stock quantity
$var_stock_count = (int) $variation->get_stock_quantity();
// if there are 5 or less, disable the variant, could always just set to 0
return $var_stock_count <= $multiplier ? false : $active;
}
return $active;
}
現(xiàn)在應(yīng)該可以了。
- 2 回答
- 0 關(guān)注
- 172 瀏覽
添加回答
舉報(bào)