In WooCommerce muss ich für jeden Artikel einer Produktkategorie eine Mindestmenge festlegen. Ich habe das Forum durchsucht und einen Code gefunden, der gut funktioniert, außer dass er nur die Menge für eine Produktkategorie insgesamt zählt:
add_action( 'woocommerce_checkout_process', 'wc_minimum_order_amount' );
add_action( 'woocommerce_before_cart' , 'wc_minimum_order_amount' );
function wc_minimum_order_amount() {
$minimum = 5; //Qty product
if ( WC()->cart->cart_contents_count < $minimum ) {
$draught_links = array();
foreach(WC()->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
$terms = get_the_terms( $_product->id, 'product_cat' );
foreach ($terms as $term) {
$draught_links[] = $term->name;
}
}
if (in_array("Noten", $draught_links)){
$on_draught = true;
}else{
$on_draught = false;
}
if( is_cart() ) {
if($on_draught){
wc_print_notice(
sprintf( 'Bitte beachte die Mindestbestellmenge. Du brauchst mindestens %s Notenexemplare pro Arrangement. Aktuell hast du %s Stück in deinem Warenkorb.' ,
$minimum ,
WC()->cart->cart_contents_count
), 'error'
);
}
} else {
if($on_draught){
wc_add_notice(
sprintf( 'Bitte beachte die Mindestbestellmenge. Du brauchst mindestens %s Notenexemplare pro Arrangement. Aktuell hast du %s Stück in deinem Warenkorb.' ,
$minimum ,
WC()->cart->cart_contents_count
), 'error'
);
}
}
}
}
Wenn ich beispielsweise zwei Produkte (A und B) habe, die derselben Produktkategorie angehören, und die Mindestmenge für diese Kategorie auf 5 festlege, wird die Fehlermeldung für den Kunden in diesem Fall nicht angezeigt:
- Produkt A: 3
- Produkt B: 2
Ich benötige eine Mindestmenge von 5 für jedes einzelne Produkt dieser Kategorie.
Haben Sie eine Idee, wie Sie den folgenden Code ändern und optimieren können?
Seit WooCommerce 3 ist Ihr tatsächlicher Code veraltet und nicht bequem… Es gibt mehrere Möglichkeiten:
1). Die beste Weise: Richten Sie die Mindestmenge auf Produktebene (für eine Produktkategorie) ein:
// On single product pages
add_filter( 'woocommerce_quantity_input_args', 'min_qty_filter_callback', 20, 2 );
function min_qty_filter_callback( $args, $product ) {
$categories = array('Noten'); // The targeted product category(ies)
$min_qty = 5; // The minimum product quantity
$product_id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id();
if( has_term( $categories, 'product_cat', $product_id ) ){
$args['min_value'] = $min_qty;
}
return $args;
}
// On shop and archives pages
add_filter( 'woocommerce_loop_add_to_cart_args', 'min_qty_loop_add_to_cart_args', 10, 2 );
function min_qty_loop_add_to_cart_args( $args, $product ) {
$categories = array('Noten'); // The targeted product category
$min_qty = 5; // The minimum product quantity
$product_id = $product->get_id();
if( has_term( $categories, 'product_cat', $product_id ) ){
$args['quantity'] = $min_qty;
}
return $args;
}
Der Code wird in die Datei functions.php Ihres aktiven untergeordneten Designs (oder aktiven Designs) eingefügt. Getestet und funktioniert.
2). Alternativer Weg: Warenkorbartikel prüfen und Fehlermeldung anzeigen (ähnlich deinem Code):
add_action( 'woocommerce_check_cart_items', 'wc_min_item_required_qty' );
function wc_min_item_required_qty() {
$categories = array('Noten'); // The targeted product category
$min_item_qty = 5; // Minimum Qty required (for each item)
$display_error = false; // Initializing
// Loop through cart items
foreach(WC()->cart->get_cart() as $cart_item ) {
$item_quantity = $cart_item['quantity']; // Cart item quantity
$product_id = $cart_item['product_id']; // The product ID
// For cart items remaining to "Noten" producct category
if( has_term( $categories, 'product_cat', $product_id ) && $item_quantity < $min_item_qty ) {
wc_clear_notices(); // Clear all other notices
// Add an error notice (and avoid checkout).
wc_add_notice( sprintf( 'Bitte beachte die Mindestbestellmenge. Du brauchst mindestens %s Notenexemplare pro Arrangement. Aktuell hast du %s Stück in deinem Warenkorb.', $min_item_qty , $item_quantity ), 'error' );
break; // Stop the loop
}
}
}
Der Code wird in die Datei functions.php Ihres aktiven untergeordneten Designs (oder aktiven Designs) eingefügt. Getestet und funktioniert.
Damit es funktioniert übergeordnete Produktkategorie Außerdem fügen Sie diese benutzerdefinierte Funktion hinzu:
// Custom conditional function that handle parent product categories too
function has_product_categories( $categories, $product_id = 0 ) {
$parent_term_ids = $categories_ids = array(); // Initializing
$taxonomy = 'product_cat';
$product_id = $product_id == 0 ? get_the_id() : $product_id;
if( is_string( $categories ) ) {
$categories = (array) $categories; // Convert string to array
}
// Convert categories term names and slugs to categories term ids
foreach ( $categories as $category ){
$result = (array) term_exists( $category, $taxonomy );
if ( ! empty( $result ) ) {
$categories_ids[] = reset($result);
}
}
// Loop through the current product category terms to get only parent main category term
foreach( get_the_terms( $product_id, $taxonomy ) as $term ){
if( $term->parent > 0 ){
$parent_term_ids[] = $term->parent; // Set the parent product category
$parent_term_ids[] = $term->term_id; // (and the child)
} else {
$parent_term_ids[] = $term->term_id; // It is the Main category term and we set it.
}
}
return array_intersect( $categories_ids, array_unique($parent_term_ids) ) ? true : false;
}
Der Code wird in die Datei functions.php Ihres aktiven untergeordneten Designs (oder aktiven Designs) eingefügt. Getestet und funktioniert.
Dann ersetzen Sie im vorhandenen Code:
has_term( $category, 'product_cat', $product_id )
durch
has_product_categories( $category, $product_id )
Auf diese Weise können Sie auch übergeordnete Produktkategorien verwalten.
Damit es für eine bestimmte Produktkategorie insgesamt funktioniert, verwenden Sie stattdessen diesen Code:
add_action( 'woocommerce_check_cart_items', 'wc_min_item_required_qty' );
function wc_min_item_required_qty() {
$categories="30"; // The targeted product category slug or ID
$max_item_qty = 10; // Minimum Qty required (for each item)
$display_error = false; // Initializing
// Loop through cart items
foreach(WC()->cart->get_cart() as $cart_item ) {
$product_id = $cart_item['product_id']; // The product ID
if( has_term( $categories, 'product_cat', $product_id )) {
$item_quantity += $cart_item['quantity']; // Cart item quantity
}
// For cart items remaining to "Pizza" product category
if( has_term( $categories, 'product_cat', $product_id ) && $item_quantity > $max_item_qty ) {
wc_clear_notices(); // Clear all other notices
// Add an error notice (and avoid checkout).
wc_add_notice( sprintf( 'We can only take orders of up to 10 burgers per customer', $max_item_qty , $item_quantity ), 'error' );
break; // Stop the loop
}
}
}