Visualizzare l'ID prodotto e la chiave dell'elemento nel carrello
Come posso visualizzare l'ID prodotto e la product_item_key di ogni articolo nel carrello invece del conteggio totale?
function iconic_cart_count_fragments( $fragments ) {
$fragments['div.header-cart-count'] = '<div class="header-cart-count">' . WC()->cart->get_cart_contents_count() . '</div>';
return $fragments;
}
Grazie

Techno Deviser, probabilmente per errore, nel ciclo foreach
ha impostato il valore su $fragments['div.header-cart-count']
invece di concatenarlo.
Prova questa modifica:
function iconic_cart_count_fragments( $fragments ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$fragments['div.header-cart-count'] .= '<div class="header-cart-count">' .$cart_item_key.'<br><br>'. $cart_item['product_id']. '</div>';
}
return $fragments;
}
Oppure:
function iconic_cart_count_fragments( $fragments ) {
$cart = WC()->cart->get_cart();
if (!empty($cart)) {
foreach ( $cart as $cart_item_key => $cart_item )
$output .= $cart_item_key. ' - ' . $cart_item['product_id'] . '<br>';
$fragments['div.header-cart-count'] = '<div class="header-cart-count">' . $output . '</div>';
}
return $fragments;
}

@JyotiSharma Ho appena controllato il secondo esempio dal codice sopra. Se ho 2 prodotti diversi nel carrello e clicco "aggiungi al carrello" sul successivo, l'indice div.header-cart-count
nella risposta JSON ha 3 coppie chiave/valore.

Prova qualcosa del genere
global $woocommerce;
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $cart_item ) {
echo $cart_item_key;
echo $cart_item['product_id'];
}
OPPURE
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
echo $cart_item_key;
echo $cart_item['product_id'];
}
OPPURE
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$fragments['div.header-cart-count'] = '<div class="header-cart-count">' .$cart_item['product_id']. '</div>';
}
Questo ti darà gli ID dei prodotti, invece dell'output attuale.
Soluzione funzionante finale.
global $woocommerce;
add_filter( 'woocommerce_add_to_cart_fragments', 'iconic_cart_count_fragments', 10, 1 );
function iconic_cart_count_fragments( $fragments ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$fragments['div.header-cart-count'] = '<div class="header-cart-count">' .$cart_item_key.'<br><br>'. $cart_item['product_id']. '</div>';
return $fragments;
}
}

ho già provato così ma dopo il frontend diventa completamente bianco.

codice del file function.php add_filter( 'woocommerce_add_to_cart_fragments', 'iconic_cart_count_fragments', 10, 1 );
function iconic_cart_count_fragments( $fragments ) {
$fragments['div.header-cart-count'] = '<div class="header-cart-count">' . foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
echo $cart_item_key; echo $cart_item['product_id']; } . '</div>';
return $fragments;
}

qual è il modo corretto? il mio obiettivo è visualizzare ogni item_id e item_key del carrello dopo che viene attivato l'aggiunta al carrello via ajax sulla pagina della categoria/home

Sto solo fornendo una risposta in base alla tua domanda, all'interno della tua funzione.

grazie mille, funziona esattamente come volevo.
global $woocommerce; add_filter( 'woocommerce_add_to_cart_fragments', 'iconic_cart_count_fragments', 10, 1 );
function iconic_cart_count_fragments( $fragments ) { foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) { $fragments['div.header-cart-count'] = '<div class="header-cart-count">' .$cart_item_key.'<br><br>'. $cart_item['product_id']. '</div>';
} return $fragments; }
