The idea:
Customer puts a box in their cart (four different simple products). They hold 4, 9, 16 or 24 pieces per box.
The customer then moves onto selecting products to put into the box / boxes. These products should be from the mix-and-match product tag. If they are not, they will be packed separately but more importantly, not counted as an mix-and-match product.
What I do not know how to do is this:
The function needs to count how many boxes that are in the cart and automatically calculate how many pieces that can be added.
Overall example:
Customer adds the box that can hold 4 pieces and the box that can hold 16 pieces. In total, the customer can now add 20 mix-and-match products to the cart.
If the customer not does not add 20 mix-and-match products to their cart, a message is shown. If the customer adds more than 20 mix-and-match products to their cart, a different message is shown.
Here are a few message examples:
"You have added the box that fits XX pieces. Please add an additional XX mix-and-match products to your cart. Note: non mix-and-match products will be packed separately in a cellophane bag."
"You have added XX boxes. You can add XX pieces. Please add an additional XX mix-and-match products to your cart. Note: non mix-and-match products will be packed separately in a cellophane bag."
This is the code I need help modifying:
add_action('woocommerce_before_shop_loop', 'number_of_pieces_in_boxes', 1, 1 );
add_action('woocommerce_before_add_to_cart_form', 'number_of_pieces_in_boxes', 1, 1 );
function number_of_pieces_in_boxes( $cart ) {
if ( is_admin() && !defined( 'DOING_AJAX' ) ) return;
global $product;
$product_count = WC()->cart->cart_contents_count;
$product_ids = array( 1718, 1719, 1720, 1721 );
$fits_four = '4';
$fits_nine = '9';
$fits_sixteen = '16';
$fits_twentyfour = '24';
$piece_slug = 'mix-and-match';
$in_cart = false;
foreach( WC()->cart->get_cart() as $cart_item ) {
$product_in_cart = $cart_item['product_id'];
if ( in_array( $product_in_cart, $product_ids ) && has_term( 'mix-and-match', 'product_tag', $product_in_cart, $product_ids ) ) {
$in_cart = true;
break;
}
}
if ( ! $in_cart ) {
echo '<div class="product-count">You have '.$product_count.' pieces in your cart whereof none fits inside a box. We will pack and wrap them for you.</div>';
} else {
echo '<div class="product-count">You have '.$product_count.' pieces in your cart.</div>';
}
}
I truly appreciate whatever help I can get on this.