In my website I use WooCommerce v3.1+ and Woocommerce Subscriptions. My products that contains items like video cameras and subscription plans. What I would like to do is:
- when purchasing a camera and subscription plan, at cart page(before checkout), the quantity of the subscription plan must be equal to the quantity of cameras,
- if another camera is added to the cart an error massage must appear...or something like that.
For example, if I have in my cart 2 specific products like a camera (quantity 2) and a subscription plan (quantity 1), the customer will not be able to checkout, if the subscription plan quantity does not match with cameras quantity.
I believe I have to modify the functions.php
in child theme. Any help would be appreciated
Edit: I have added a piece of code in functions.php, but this will add x2 total items in cart:
add_action('woocommerce_check_cart_items', 'wh_wc_minimum_order_amount');
function wh_wc_minimum_order_amount() {
$minimum = 2;
if (WC()->cart->get_cart_contents_count() % $minimum != 0) {
// wc_clear_notices();
wc_add_notice(sprintf('<strong>Error: check product quantity</strong>', $minimum), 'error');
}
}
So this doesn't work.
Here is a complete solution that will display custom notices when normal products count doesn't match with subscription plans, warning the customer lightly. If products doesn't match your requirements, the customer will be redirected from checkout to shop page (avoiding checkout).
The code (commented):
code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested on Woocommerce 3+ and works