Ad

Get Cart Items Quantities From Products With Specific Tag In WooCommerce

- 1 answer

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.

Ad

Answer

  • The hooks you use do not contain $cart as an argument by default
  • To get the cart item quantity from product in cart that contain a certain tag, you can use WC_Cart::get_cart_item_quantities()
  • Add the contents of the box + product ID to the $box_ids array, make sure these products do not contain the relevant tag
  • The cart item quantity of a product with the specific tag is kept in a counter
  • The content each box can contain * number of pieces of each box in cart is stored in another counter
  • With this basic answer that contains all the data you need, you can go either way, it depends on what you specifically want

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 );
Ad
source: stackoverflow.com
Ad