Wenden Sie keinen Gutscheinrabatt auf nachbestellte Produkte in WooCommerce an
Ich versuche, einen Gutschein nur für Produkte anzuwenden, die auf Lager sind.
Wenn ich also 5 Artikel im Warenkorb und 4 auf Lager habe, wird bei nur 4 der Rabatt angewendet , die Produkte bei Nachbestellung NICHT.
Eigentlich versuche ich, die Standardfunktion " get_items_to_apply_coupon
" in " class-wc-discounts.php
" zu ändern .
Ich habe versucht, die Lagermenge des aktuellen Produkts in jedem Cicle zu berechnen, und dann habe ich $item_to_apply->quantity
die Differenz zwischen Lager- und Warenkorbprodukten geändert .
Aber auch wenn ich "200" in $ -> Menge einsetze, ändert sich der Rabatt nicht .
protected function get_items_to_apply_coupon( $coupon ) { $items_to_apply = array();
foreach ( $this->get_items_to_validate() as $item ) {
$item_to_apply = clone $item; // Clone the item so changes to this item do not affect the originals.
if ( 0 === $this->get_discounted_price_in_cents( $item_to_apply ) || 0 >= $item_to_apply->quantity ) { continue; } if ( ! $coupon->is_valid_for_product( $item_to_apply->product, $item_to_apply->object ) && ! $coupon->is_valid_for_cart() ) { continue; } $items_to_apply[] = $item_to_apply; } return $items_to_apply;
}
Antworten
Das class-wc-discounts.phpenthält
- apply_coupon_percent - Wenden Sie einen prozentualen Rabatt auf Artikel an und geben Sie eine Reihe gewährter Rabatte zurück.
In dieser Funktion finden wir den woocommerce_coupon_get_discount_amountFilterhaken.
Wenn ein Produkt nachbestellt ist, können Sie 0 als Rabatt zurückgeben
function filter_woocommerce_coupon_get_discount_amount( $discount, $price_to_discount , $cart_item, $single, $coupon ) { // On backorder if ( $cart_item['data']->is_on_backorder() ) {
$discount = 0; } return $discount;
}
add_filter( 'woocommerce_coupon_get_discount_amount', 'filter_woocommerce_coupon_get_discount_amount', 10, 5 );