Advertisement
Fany_VanDaal

Sleva podle počtu kusů v košíku

Jun 5th, 2019
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.11 KB | None | 0 0
  1. add_action( 'woocommerce_before_calculate_totals', 'vandaal_quantity_based_pricing', 9999 );
  2.  
  3. function vandaal_quantity_based_pricing( $cart ) {
  4.  
  5.     if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
  6.  
  7.     if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return;
  8.  
  9.     // Define discount rules and thresholds
  10.     $threshold1 = 101; // Když je počet kusů vyšší než > 100
  11.     $discount1 = 0.05; // Slevnit kus o 5%
  12.     $threshold2 = 1001; // Když je počet kusů vyšší než > 1000
  13.     $discount2 = 0.1; // Slevnit kus o 10%
  14.  
  15.     foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
  16.       $product_id = $cart_item['product_id'];
  17.       if ( $cart_item['quantity'] >= $threshold1 && $cart_item['quantity'] < $threshold2 ) {
  18.          $price = round( $cart_item['data']->get_price() * ( 1 - $discount1 ), 2 );
  19.          $cart_item['data']->set_price( $price );
  20.       } elseif ( $cart_item['quantity'] >= $threshold2 ) {
  21.          $price = round( $cart_item['data']->get_price() * ( 1 - $discount2 ), 2 );
  22.          $cart_item['data']->set_price( $price );
  23.       }    
  24.     }
  25.    
  26.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement