Ich wollte einen Gutscheincode auf den Warenkorb anwenden, wenn der Warenkorb mindestens 2 Artikel enthält. Wenn nicht, dann wird der Gutschein nicht angewendet und eine Änderungsnachricht angezeigt. Wenn Sie ihn angewendet haben, wird eine Erfolgsmeldung angezeigt. Hier ist mein Code, den ich versucht habe, funktioniert nicht so, wie ich es möchte
add_action( 'woocommerce_before_calculate_totals','conditionally_auto_add_coupon', 30, 1 );
function conditionally_auto_add_coupon( $cart ) {
if ( is_admin() && !defined('DOING_AJAX') ) return; // Exit
// HERE set the coupon code (in lowercase)
$coupon_code="mycode";
$total_item = 0;
if (WC()->cart->has_discount('mycode')) {
foreach( $cart->get_cart() as $cart_item ){
$total_item++;
}
if($total_item < 2){
$cart->remove_coupon( $coupon_code );
wc_add_notice( __('you have only 1 item in cart'), 'alert');
}
else{
$cart->add_discount( $coupon_code );
wc_add_notice( __('coupon added'), 'notice');
}
}
}
Jede Hilfe ist willkommen.