Ich kann die Menge aller Produkte abrufen, die dem Warenkorb hinzugefügt wurden, aber ich benötige sie für das jeweilige Produkt.
Überprüfen Sie einfach die if-Anweisung if($cart_item['product_id'] === 'your-id') {
– Tamil Selvan C
19. Juni 2017 um 14:28 Uhr
LoicTheAztec
Sie können Warenkorbartikel durchlaufen, um die Menge für eine bestimmte Produkt-ID wie folgt zu erhalten:
// Set here your product ID (or variation ID)
$targeted_id = 24;
// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item ) {
if( in_array( $targeted_id, array($cart_item['product_id'], $cart_item['variation_id']) )){
$quantity = $cart_item['quantity'];
break; // stop the loop if product is found
}
}
// Displaying the quantity if targeted product is in cart
if( isset( $quantity ) && $quantity > 0 ) {
printf( "<p>" . __("Quantity for Product ID %d is: %d") . "</p>", $targeted_id, $quantity );
}
// Set here your product ID (or variation ID)
$targeted_id = 24;
// Get quantities for each item in cart (array of product id / quantity pairs)
$quantities = WC()->cart->get_cart_item_quantities();
// Displaying the quantity if targeted product is in cart
if( isset($quantities[$targeted_id]) && $quantities[$targeted_id] > 0 ) {
printf( "<p>" . __("Quantity for Product ID %d is: %d") . "</p>", $targeted_id, $quantities[$targeted_id] );
}
Notiz: Diese letzte Möglichkeit erlaubt es nicht, das Produkt der übergeordneten Variable anzusprechen.
Bitte posten Sie nicht nur Code als Antwort, sondern geben Sie auch eine Erklärung, was Ihr Code tut und wie er das Problem der Frage löst. Antworten mit einer Erklärung sind in der Regel hilfreicher und von besserer Qualität und ziehen mit größerer Wahrscheinlichkeit Upvotes an.
Überprüfen Sie einfach die if-Anweisung
if($cart_item['product_id'] === 'your-id') {
– Tamil Selvan C
19. Juni 2017 um 14:28 Uhr