I got four giftboxes. The customer can only put products tagged with fits-in-giftbox into them. The boxes have space for 4, 9, 16 or 24 pieces. All other products will be packed separately.
I need to count how many times each of these boxes have been added to cart and calculate the total number of pieces they can fit.
I then need to calculate how many fits-in-giftbox tagged products the customer adds to their cart and match that number with how many pieces all giftboxes can hold.
Example: Customer puts one box-4 (holds 4 products) and one box-24 (holds 24 products) in the cart. In total, the customer can now add 28 products tagged with fits-in-giftbox.
Customer adds 18 products tagged with fits-in-giftbox. There are now 10 pieces left.
Expected output: "You have added three (3) giftboxes to your cart. Please add an additional XX products to fill your boxes."
OR: "You have added two (2) giftboxes which can hold 8 pieces. You have added 12 products to your cart. The exceeding four (4) products will be packed separately unless you add another giftbox to your cart."
OR: "You have added one giftbox to your cart but no giftbox products. Unless you do, these products will be packed separately."
I hope this makes sense. Feels like I am going in circles here..
This is the code I need help with:
function product_tag_count( $term ) {
$tag_count = 0;
foreach( WC()->cart->get_cart() as $cart_item ) {
if ( has_term( $term, 'product_tag', $cart_item['product_id'] ) )
$tag_count += $cart_item['quantity'];
}
return $tag_count == 0 ? false : $tag_count;
}
function display_cart_counters() {
ob_start();
$link = wc_get_checkout_url();
$title = __( 'Checkout and Pay', 'woocommerce' );
$tag_count = __( '0 products', 'woocommerce');
echo '<a class="product-cart-count" href="' . $link . '" title="' . $title . '">' . $tag_count . '</a>';
return ob_get_clean();
}
add_filter( 'woocommerce_add_to_cart_fragments', 'counters_add_to_cart_fragment', 30, 1 );
function counters_add_to_cart_fragment( $fragments ) {
ob_start();
// count the number of giftboxes
$giftbox4 = 'giftbox-4';
$fits_in_giftbox4 = '4';
$count_giftbox4 = product_tag_count( $giftbox4 );
$giftbox9 = 'giftbox-9';
$fits_in_giftbox9 = '9';
$count_giftbox9 = product_tag_count( $giftbox9 );
$giftbox16 = 'giftbox-16';
$fits_in_giftbox16 = '16';
$count_giftbox16 = product_tag_count( $giftbox16 );
$giftbox24 = 'giftbox-24';
$fits_in_giftbox24 = '24';
$count_giftbox24 = product_tag_count( $giftbox24 );
// get the total number of giftboxes
$count_giftboxes_total = ( $count_giftbox4 + $count_giftbox9 + $count_giftbox16 + $count_giftbox24 );
// count how many products that fits into giftboxes
$fits_in_giftbox = 'goes-into-box';
$count_fits_in_giftbox = product_tag_count( $fits_in_giftbox );
// count the cart as a whole
$cart_count = WC()->cart->get_cart_contents_count();
// calculate the difference between cart as a whole and giftbox products
$difference_between_cart_and_giftbox_products = ( $cart_count - $count_fits_in_giftbox );
// calculate boxes VS giftbox products
$fits_into_boxes_vs_giftbox_products = ( $count_giftboxes_total - $count_fits_in_giftbox );
// this is what I need help with.. how do I get the total pieces of all boxes added to cart?
// first check, is there space left or not?
if ( $count_giftboxes_total < $count_fits_in_giftbox ) {
$output = sprintf (_n( 'You have %d product to much. This will be packed separately unless you add another giftbox.', 'You have %d products to many. These will be packed separately unless you add another giftbox.' ), 'woocommerce');
} elseif () {
$output = sprintf (_n( 'You have %d product to much. This will be packed separately unless you add another giftbox.', 'You have %d products to many. These will be packed separately unless you add another giftbox.' ), 'woocommerce');
} elseif () {
$output = sprintf (_n( 'You have %d product to much. This will be packed separately unless you add another giftbox.', 'You have %d products to many. These will be packed separately unless you add another giftbox.' ), 'woocommerce');
} else {
$output = sprintf (_n( 'You have %d product to much. This will be packed separately unless you add another giftbox.', 'You have %d products to many. These will be packed separately unless you add another giftbox.' ), 'woocommerce');
}
$checkout_link = wc_get_checkout_url();
$link_title = __( 'Go to Checkout', 'woocommerce' );
?>
<div class="product-wrapper">
<?php echo $output; ?>
<br>
<a class="product-cart-count" href="<?php echo $link; ?>"
title="<?php echo $title ?>">Go to Checkout & Pay</a>
</div>
<?php
$fragments['a.product-cart-count'] = ob_get_clean();
return $fragments;
}
add_action( 'woocommerce_before_shop_loop', 'count_products_and_boxes');
add_action( 'woocommerce_before_add_to_cart_form', 'count_products_and_boxes');
function count_products_and_boxes() {
echo display_cart_counters();
}