chris_di

bulk discoutn for woo category

Jan 9th, 2023 (edited)
1,528
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.41 KB | None | 0 0
  1. <?php
  2.  
  3. add_action( 'woocommerce_cart_calculate_fees', 'discount_for_special_sale_category', 10, 1 );
  4.  
  5. function discount_for_special_sale_category( $cart ) {
  6.     if ( is_admin() && ! defined( 'DOING_AJAX' ) )
  7.         return;
  8.  
  9.     // Initializing
  10.     $discount = 0;
  11.     $saved_amount = 0;
  12.     $special_sale_category = 'special-sale';
  13.  
  14.     // Iterating through each cart item
  15.     foreach ( $cart->get_cart() as $cart_item ) {
  16.         // Get the product categories
  17.         $product_categories = get_the_terms( $cart_item['product_id'], 'product_cat' );
  18.  
  19.         // Iterating through each product category
  20.         foreach ( $product_categories as $product_category ) {
  21.             // Check if the product category is "special-sale"
  22.             if ( $product_category->slug == $special_sale_category ) {
  23.                 // Calculate the discount
  24.                 if ( $cart_item['quantity'] >= 3 ) {
  25.                     $discount = $cart_item['line_total'] * 0.6;
  26.                 } elseif ( $cart_item['quantity'] >= 1 ) {
  27.                     $discount = $cart_item['line_total'] * 0.5;
  28.                 }
  29.  
  30.                 // Calculate the saved amount
  31.                 $saved_amount += $discount;
  32.             }
  33.         }
  34.     }
  35.  
  36.     // Add the discount
  37.     if ( $saved_amount > 0 ) {
  38.         $cart->add_fee( __( 'Sonderrabatt für Kategorie "Special Sale"', 'woocommerce' ), -$saved_amount );
  39.     }
  40.  
  41.    
  42. }
Advertisement
Add Comment
Please, Sign In to add comment