我试图从foreach循环创建多个数组,所以每次循环遍历项时,它会创建一个包含所有值的数组。然后我希望将它们插入到主数组中。
基本上,我希望产品名称,ID,数量等在一个数组中,然后下一个项目在另一个数组中。然后这些将在主$ cartinfo数组中。
此刻,当它遍历项目时,它会将它们添加到单个数组中。有人可以帮帮我吗?
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;
}
答案 0 :(得分:0)
如果我弄错了,请纠正我。
你想做这样的事情:
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;
}