wie man eine bestimmte Produktmenge von der Warenkorbseite im WooCommerce erhält

Lesezeit: 4 Minuten

Benutzeravatar von vivek raj
vivek raj

Mit diesem Code:

foreach ( WC()->cart->get_cart() as $cart_item ) {  
   $quantity =  $cart_item['quantity'];
   echo $quantity;
}

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

Benutzeravatar von LoicTheAztec
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 );
}

Oder Sie können auch die verwenden WC_Cart Methode get_cart_item_quantities() wie folgt:

// 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.

Benutzeravatar von Pankaj Patel
Pankaj Patel

global $woocommerce;
    $items = $woocommerce->cart->get_cart();

    foreach($items as $item => $values) { 
        $_product = $values['data']->post; 
        echo "<b>".$_product->post_title.'</b>  <br> Quantity: '.$values['quantity'].'<br>'; 
        $price = get_post_meta($values['product_id'] , '_price', true);
        echo "  Price: ".$price."<br>";
    } 

$cart = WC()->cart->get_cart();
$product_cart_id = WC()->cart->generate_cart_id( $product->get_id() );
if( WC()->cart->find_product_in_cart( $product_cart_id )) {
    echo($cart[$product_cart_id]['quantity']);
}

  • 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.

    – Mark Rotteveel

    5. Mai 2020 um 7:56 Uhr

Versuche dies :

<?php
    global $woocommerce;
    $items = $woocommerce->cart->get_cart();

        foreach($items as $item => $values) { 
            $_product = $values['data']->post; 
            echo "<b>".$_product->post_title.'</b>  <br> Quantity: '.$values['quantity'].'<br>'; 
            $price = get_post_meta($values['product_id'] , '_price', true);
            echo "  Price: ".$price."<br>";
        } 
?>

Benutzeravatar von Fraide
Fraide

1) Erstellen Sie ein untergeordnetes Thema

2) Erstellen Sie eine Datei namens functions.php mit diesem Code:

<?php
if( !function_exists('in_cart_product_quantity') ) {
    function in_cart_product_quantity( $atts ) {

        $atts = shortcode_atts(array('id' => ''), $atts, 'product_qty'); // shortcode attributes
        if( empty($atts['id'])) return; // check id not null

        if ( WC()->cart ) { // check cart exists
            $qty = 0; // init qty
            foreach (WC()->cart->get_cart() as $cart_item) {
                if($cart_item['product_id'] == $atts['id']) {
                    $qty =  $cart_item['quantity'];
                    break; // stop the loop if product is found
                }
            }
            return $qty;
        }
        return;
    }
    add_shortcode( 'product_qty', 'in_cart_product_quantity' );
}

3) Verwenden Sie den Shortcode:

[product_qty id="xxx"]

(Ändern Sie xxx mit der ID des Produkts, dessen Menge Sie im Warenkorb erfahren möchten.)

Danke an LoicTheAztec (gleicher Thread)

WC_Cart::get_cart_item_quantities() – Get cart items quantities
  1. Über $product_id
// Your product ID
$product_id = 30;

// Get cart items quantities
$cart_item_quantities = WC()->cart->get_cart_item_quantities();

// Product quantity in cart - All PHP versions
$product_qty_in_cart = isset( $cart_item_quantities[ $product_id ] ) ? $cart_item_quantities[ $product_id ] : null;

// Product quantity in cart - Same as the previous one with PHP 7
$product_qty_in_cart = $cart_item_quantities[ $product_id ] ?? null;

// Result
echo $product_qty_in_cart;
  1. Über $product
// Product
global $product;

// Get cart items quantities
$cart_item_quantities = WC()->cart->get_cart_item_quantities();

// Product quantity in cart 
$product_qty_in_cart = $cart_item_quantities[ $product->get_stock_managed_by_id() ];

// Result
echo $product_qty_in_cart;

1393990cookie-checkwie man eine bestimmte Produktmenge von der Warenkorbseite im WooCommerce erhält

This website is using cookies to improve the user-friendliness. You agree by using the website further.

Privacy policy