Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- add_action( 'woocommerce_cart_calculate_fees', 'discount_for_special_sale_category', 10, 1 );
- function discount_for_special_sale_category( $cart ) {
- if ( is_admin() && ! defined( 'DOING_AJAX' ) )
- return;
- // Initializing
- $discount = 0;
- $saved_amount = 0;
- $special_sale_category = 'special-sale';
- // Iterating through each cart item
- foreach ( $cart->get_cart() as $cart_item ) {
- // Get the product categories
- $product_categories = get_the_terms( $cart_item['product_id'], 'product_cat' );
- // Iterating through each product category
- foreach ( $product_categories as $product_category ) {
- // Check if the product category is "special-sale"
- if ( $product_category->slug == $special_sale_category ) {
- // Calculate the discount
- if ( $cart_item['quantity'] >= 3 ) {
- $discount = $cart_item['line_total'] * 0.6;
- } elseif ( $cart_item['quantity'] >= 1 ) {
- $discount = $cart_item['line_total'] * 0.5;
- }
- // Calculate the saved amount
- $saved_amount += $discount;
- }
- }
- }
- // Add the discount
- if ( $saved_amount > 0 ) {
- $cart->add_fee( __( 'Sonderrabatt für Kategorie "Special Sale"', 'woocommerce' ), -$saved_amount );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment