2

Ii've code for checking quantity of goods in cart, and then it makes a discount based on quantity. How can I make additional check on price for goods below 500 (for these goods, discount should be 0).

The following code is based on WooCommerce Cart quantity based discount answer:

add_action( 'woocommerce_cart_calculate_fees','wc_cart_quantity_discount', 10, 1 );

function wc_cart_quantity_discount( $cart_object ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    ## -------------- DEFINIG VARIABLES ------------- ##
    $discount = 0;
    $cart_item_count = $cart_object->get_cart_contents_count();
    $cart_total_excl_tax = $cart_object->subtotal_ex_tax;
    /*foreach( $cart_object->get_cart() as $cart_item_key => $cart_item ) {
        $cart_item = $cart_object->get_cart_contents_count();
    }*/
    
    
    // Assign each product's price to its cart item key (to be used again later)
    
    ## ----------- CONDITIONAL PERCENTAGE ----------- ##

        if( $cart_item_count <= 1  )
            $percent = 5;
        elseif( $cart_item_count <= 2  )
            $percent = 10;
        elseif( $cart_item_count <= 3  )
            $percent = 15;
        elseif( $cart_item_count <= 4  )
            $percent = 20;
        elseif( $cart_item_count >= 5  )
            $percent = 25;
        else {
            $percent = 0;
        }

    ## ------------------ CALCULATION ---------------- ##
    $discount -= ($cart_total_excl_tax / 100) * $percent;

    ## ----  APPLYING CALCULATED DISCOUNT TAXABLE ---- ##
    if( $percent > 0 )
        $cart_object->add_fee( __( "Quantity discount $percent%", "woocommerce" ), $discount, true);
}

Should be checking for quantity and price below, for example, 500 (the discount should be 0). For another goods with condition: price is higher 500 and the corresponding quantity, its own discount: 1 - 5% 2 - 10% 3 - 15%
etc.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Anton
  • 23
  • 2

1 Answers1

1

Updated

The following code will only count items that are above 500, to apply a percentage discount based on that item count:

add_action( 'woocommerce_cart_calculate_fees','wc_cart_intem_quantity_discount' );
function wc_cart_intem_quantity_discount( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // INITIALIZING VARIABLES
    $min_item_amount = 500;
    $discount = $items_count = $percent = $items_subtotal = 0;

    // Loop through cart items
    foreach( $cart->get_cart() as $cart_item_key => $cart_item ) {
        // Only counting items that are above 500
        if( $cart_item['data']->get_price() >= $min_item_amount ) {
            $items_count += $cart_item['quantity'];
            $items_subtotal += $cart_item['line_subtotal'];
        }
    }

    // CONDITIONAL PERCENTAGE
    if ($items_count == 1) {
        $percent = 5;
    } elseif ($items_count == 2) {
        $percent = 10;
    } elseif ($items_count == 3) {
        $percent = 15;
    } elseif ($items_count == 4) {
        $percent = 20;
    } elseif ($items_count >= 5) {
        $percent = 25;
    }

    // DISCOUNT (TAXABLE)
    if( $items_count > 0 ) {
        // Calculation
        $discount -= ($items_subtotal / 100) * $percent;

        $cart->add_fee( __( "Quantity discount $percent%", "woocommerce" ), $discount, true);
    }
}

Code based on: WooCommerce Cart quantity based discount

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Sorry but there some mistake: "all to a member function get_price() on null" I'v changed in this line: if( $art_item['data']->get_price() < $min_item_amount ) from $art_item to $cart_item, but still not work( – Anton Jun 29 '23 at 18:37
  • 1
    Very thank you, it solve the problem, good luck) – Anton Jun 29 '23 at 19:30