WooCommerce – Set product quantity to 1

From the below code you can set/remove product quantity to 1 in cart and checkout pages

/**
* Set product quantity to 1 in cart and checkout
*/
function remove_duplicate_products_from_cart(){
	$flag = false;
	$quantity_message = "";
	foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {
		$quantity = $cart_item['quantity'];
		if($quantity > 1){
			$flag = true;
			WC()->cart->set_quantity( $cart_item_key, 1 );
			$quantity_message = __("Quantity can not be more than 1.");
		}
	}
	if($flag){
		wc_print_notice($quantity_message, 'notice');
	}
}
add_action('woocommerce_before_cart', 'remove_duplicate_products_from_cart');
add_action('woocommerce_before_checkout_form', 'remove_duplicate_products_from_cart');

+1
2
+1
1
+1
0
+1
1
+1
0
+1
0
+1
0