2 回答

TA貢獻1951條經驗 獲得超3個贊
要將數量限制為 1,您將使用 WC_cartset_quantity()方法,如下所示:
<?php
$current_product_id = 5; // The product ID
$cart = WC()->cart; // The WC_Cart Object
// When cart is not empty
if ( ! $cart->is_empty() ) {
// Loop through cart items
foreach( $cart->get_cart() as $cart_item_key => $cart_item ) {
// If the cart item is not the current defined product ID
if( $current_product_id != $cart_item['product_id'] ) {
$cart->remove_cart_item( $cart_item_key ); // remove it from cart
}
// If the cart item is the current defined product ID and quantity is more than 1
elseif( $cart_item['quantity'] > 1 ) {
$cart->set_quantity( $cart_item_key, 1 ); // Set the quantity to 1
}
}
}
?>
它應該有效。

TA貢獻1836條經驗 獲得超3個贊
對于任何希望無需代碼即可實現相同功能的人。您只需在產品的庫存設置中選擇“單獨出售”復選框,您也可以使用如下代碼來實現此目的:update_post_meta($product_id, '_sold_individually', 'yes');
- 2 回答
- 0 關注
- 193 瀏覽
添加回答
舉報