phpwordpresswoocommercecart

Get cart items quantities from products with specific tag in WooCommerce


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.


Solution

  • So you get:

    function action_woocommerce_before_add_to_cart_form() {
        // Settings
        $term = 'mix-and-match';
        $taxonomy = 'product_tag';
        
        // Box IDs. Important, these should not contain the term!
        // Contents => Product ID
        $box_ids = array( 
            4  => 1718,
            9  => 1719,
            16 => 1720,
            20 => 1721
        );
        
        // Initialize
        $total_term = 0;
        $total_box = 0;
        
        // True
        if ( WC()->cart ) { 
            // Loop trough cart items quantities
            foreach( WC()->cart->get_cart_item_quantities() as $product_id => $cart_item_quantity ) {
                // Contains the definite term
                if ( has_term( $term, $taxonomy, $product_id ) ) {
                    // Add the cart item quantity from this certain product to the counter
                    $total_term += $cart_item_quantity;
                // The box ID is in the cart
                } elseif( in_array( $product_id, $box_ids ) ) {
                    // Add contens * the cart item quantity to the counter
                    $total_box += ( array_search ( $product_id, $box_ids ) * $cart_item_quantity );
                }
            }
        }
        
        echo '<p>Total term: ' . $total_term . '</p>';
        echo '<p>Total box = ' . $total_box . '</p>';
    }
    add_action( 'woocommerce_before_add_to_cart_form', 'action_woocommerce_before_add_to_cart_form', 10, 0 );