Ich versuche, die Anzahl der Artikel im Warenkorb nur aus einer bestimmten Produktkategorie in WooCommerce zu bekommen.Zählen Warenkorb-Artikel bestimmter Produktkategorie
Ich mache eine Website für ein Weingut. Es hat alkoholische und alkoholfreie Produkte. Der gesamte Wein fällt unter die Hauptkategorie "Wein" oder Kategorie ID 34, mit vielen Unterkategorien und Produkten darunter.
Für alle Dinge, die unter diese Kategorie fallen ... Ich muss wissen, wie viele Artikel im Warenkorb in dieser Kategorie sind. Wenn es sechs Flaschen Wein gibt, egal, ob es sich um die gleiche Produkt-ID oder um sechs verschiedene Produkt-IDs handelt. Ich muss diese Zahl von 6 aus der Kategorie "Wein" oder Kategorie ID 34 erhalten.
Ich versuchte dies mit keinem Erfolg.
Ich bin sehr neu in WooCommerce und ein wenig neu in Object Oriented.
Dank
function cat_cart_count($cat_name) {
// $cat_name variable is for you normally "tshirts" or "shorts"
global $woocommerce; $cat_count = 0;
// For each product in the cart
foreach(WC()->cart->get_cart() as $cart_item_key => $values) {
$_product_id = $values['product_id']; // product ID
$_product_qty = $values['quantity']; // product quantity
// Getting categories of the product (could be more than one)
$terms = get_the_terms($_product_id, 'product_cat');
// Checking this product has a category
if ($terms && ! is_wp_error($terms)) {
$term_name = array();
// For each category of that product
foreach($terms as $term) {
// Adding the category name to an array
$term_name[] = $term->name;
// if the product has $cat_name category
if (in_array($cat_name, $term_name)) {
// add 1 x product quantity to the count
$cat_count =+ 1 * $_product_qty;
}
}
}
}
Das hat wunderbar funktioniert. Vielen Dank. –