0

I'm trying to create multiple arrays from a foreach loop so each time it loops through the items, it create an array with all the values in. I then want these to be inserted into a a main array.

So essentially, i want the product name, id, quantity etc to be in one array and then the next item to be in another one. These will then be within the main $cartinfo array.

At the moment, when it loops through the items it adds them to a single array. Can anyone help me please?

function cart_items_array() { 

    $cartinfo = array();

    $carts2 = MultiCart\get_carts();

    foreach ( $carts2 as $cart2_id => $cart2 ) {
         // get array of items contained in a cart ...
        $items2 = MultiCart\get_cart( $cart_id2 );

        foreach ( $items2 as $item2_id => $item2 ) {

                $product_name = get_post($item2['product_id'])->post_title; 
                $familyterms = wp_get_post_terms( $item2['product_id'], 'pa_product-family'); 
                $cat_terms = wp_get_post_terms( $item2['product_id'], 'pa_product-category'); 
                $product_sku = get_post_meta( $item2['product_id'], '_sku', true );

                $cartinfo[] = $product_sku; 
                foreach ($cat_terms as $cat_term) { $cartinfo[] = $cat_term->name; };
                foreach ($familyterms as $family) { $cartinfo[] = $family->name; }; 
                $cartinfo[] = $product_name;
                $cartinfo[] = $item2['quantity'];
                $cartinfo[] = $cart2['name'];

        }
    } 

    return $cartinfo;
}

1 Answer 1

0

Correct me if I got it wrong.

Do you want to do something like this:

function cart_items_array() { 

$cartinfo = array();

$carts2 = MultiCart\get_carts();

// All the products
$allTheCarts = array();

foreach ( $carts2 as $cart2_id => $cart2 ) {
     // get array of items contained in a cart ...
    $items2 = MultiCart\get_cart( $cart_id2 );

    foreach ( $items2 as $item2_id => $item2 ) {
            $compProduct = array();
            $product_name = get_post($item2['product_id'])->post_title; 
            $familyterms = wp_get_post_terms( $item2['product_id'], 'pa_product-family'); 
            $cat_terms = wp_get_post_terms( $item2['product_id'], 'pa_product-category'); 
            $product_sku = get_post_meta( $item2['product_id'], '_sku', true );

            $cartinfo[] = $product_sku; 
            foreach ($cat_terms as $cat_term) { $cartinfo[] = $cat_term->name; };
            foreach ($familyterms as $family) { $cartinfo[] = $family->name; }; 
            $cartinfo[] = $product_name;
            $cartinfo[] = $item2['quantity'];
            $cartinfo[] = $cart2['name'];

            // Store the complete product info
            $allTheCarts[] = $cartinfo;
    }
} 

return $allTheCarts;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Hi mate, Thanks for looking at this for me. I want to be able to store each product in it's own array so the end result would look like this: $allthecarts = array(array('product-name-1', 'product-id-1')array('product-name-2', product-id-2)) etc I tried what you suggested but it's outputting as array();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.