1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
<?php
namespace SIW\WooCommerce\Checkout;
use SIW\Properties;
class Discount{
public static function init() {
$self = new self();
add_action( 'woocommerce_cart_calculate_fees', [ $self, 'calculate_discounts' ] );
}
public function calculate_discounts( \WC_Cart $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
return;
}
$cart_contents = $cart->get_cart_contents();
$count = 0;
foreach ( $cart_contents as $line ) {
$count++;
if ( 2 === $count ) {
$discount = $line['line_total'] * Properties::DISCOUNT_SECOND_PROJECT * -0.01;
$cart->add_fee( __( 'Korting 2e project', 'siw' ), $discount );
}
if ( 3 === $count ) {
$discount = $line['line_total'] * Properties::DISCOUNT_THIRD_PROJECT * -0.01;
$cart->add_fee( __( 'Korting 3e project', 'siw' ), $discount );
}
if ( 3 < $count ) {
$discount = $line['line_total'] * Properties::DISCOUNT_THIRD_PROJECT * -0.01;
$cart->add_fee( sprintf( __( 'Korting %de project', 'siw' ), $count ), $discount );
}
}
}
}