Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- add_action( 'woocommerce_before_calculate_totals', 'vandaal_quantity_based_pricing', 9999 );
- function vandaal_quantity_based_pricing( $cart ) {
- if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
- if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return;
- // Define discount rules and thresholds
- $threshold1 = 101; // Když je počet kusů vyšší než > 100
- $discount1 = 0.05; // Slevnit kus o 5%
- $threshold2 = 1001; // Když je počet kusů vyšší než > 1000
- $discount2 = 0.1; // Slevnit kus o 10%
- foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
- $product_id = $cart_item['product_id'];
- if ( $cart_item['quantity'] >= $threshold1 && $cart_item['quantity'] < $threshold2 ) {
- $price = round( $cart_item['data']->get_price() * ( 1 - $discount1 ), 2 );
- $cart_item['data']->set_price( $price );
- } elseif ( $cart_item['quantity'] >= $threshold2 ) {
- $price = round( $cart_item['data']->get_price() * ( 1 - $discount2 ), 2 );
- $cart_item['data']->set_price( $price );
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement