Untitled

mail@pastecode.io avatar
unknown
plain_text
14 days ago
7.9 kB
4
Indexable
Never
add_action('woocommerce_cart_calculate_fees', 'apply_custom_discounts', 10, 1);

function apply_custom_discounts($cart) {
    if (is_admin() && !defined('DOING_AJAX')) {
        return;
    }
    $user_id = get_current_user_id();
    $is_b2b = get_user_meta($user_id, 'b2bking_b2buser', true);
    if ($is_b2b !== 'yes'){
        return;
    }
    
    $user_group = intval(get_user_meta($user_id, 'b2bking_customergroup', true));
    
    $bronze_group = 26777;
    $silver_group = 26778;
    $gold_group = 26779;
    $platinum_group = 26780;
    
    foreach ($cart->get_cart() as $cart_item_key => $cart_item) {
        $product = $cart_item['data'];
        $product_id = $product->get_id();
        
        // Get categories
        $categories = get_product_categories($product_id);
        
        $material = strtolower($product->get_attribute('pa_material-option'));
        $price = $cart_item['data']->get_price() * $cart_item['quantity'];
        $discount_percentage = 0;
        $discount_name = '';
        
        // Discount logic for all membership levels and product types
        if (in_array('chains', $categories)) {
            if (stripos($material, 'silver') !== false) {
                $discount_name = 'Silver Chains Discount';
                if ($user_group == $bronze_group) {
                    $discount_percentage = 0.35;
                } elseif ($user_group == $silver_group) {
                    $discount_percentage = 0.40;
                } elseif ($user_group == $gold_group) {
                    $discount_percentage = 0.45;
                } elseif ($user_group == $platinum_group) {
                    $discount_percentage = 0.50;
                }
            } elseif (stripos($material, 'gold') !== false) {
                $discount_name = 'Gold Chains Discount';
                if ($user_group == $bronze_group) {
                    $discount_percentage = 0.15;
                } elseif ($user_group == $silver_group) {
                    $discount_percentage = 0.20;
                } elseif ($user_group == $gold_group) {
                    $discount_percentage = 0.25;
                } elseif ($user_group == $platinum_group) {
                    $discount_percentage = 0.30;
                }
            }
        } elseif (in_array('rings', $categories)) {
            if (stripos($material, 'silver') !== false) {
                $discount_name = 'Silver Rings Discount';
                if ($user_group == $bronze_group) {
                    $discount_percentage = 0.30;
                } elseif ($user_group == $silver_group) {
                    $discount_percentage = 0.35;
                } elseif ($user_group == $gold_group || $user_group == $platinum_group) {
                    $discount_percentage = 0.40;
                }
            } elseif (stripos($material, 'gold') !== false) {
                $discount_name = 'Gold Rings Discount';
                if ($user_group == $bronze_group) {
                    $discount_percentage = 0.15;
                } elseif ($user_group == $silver_group) {
                    $discount_percentage = 0.20;
                } elseif ($user_group == $gold_group) {
                    $discount_percentage = 0.25;
                } elseif ($user_group == $platinum_group) {
                    $discount_percentage = 0.30;
                }
            }
        } elseif (in_array('earrings', $categories)) {
            if (stripos($material, 'silver') !== false) {
                $discount_name = 'Silver Earrings Discount';
                if ($user_group == $bronze_group) {
                    $discount_percentage = 0.30;
                } elseif ($user_group == $silver_group) {
                    $discount_percentage = 0.35;
                } elseif ($user_group == $gold_group || $user_group == $platinum_group) {
                    $discount_percentage = 0.40;
                }
            } elseif (stripos($material, 'gold') !== false) {
                $discount_name = 'Gold Earrings Discount';
                if ($user_group == $bronze_group) {
                    $discount_percentage = 0.15;
                } elseif ($user_group == $silver_group) {
                    $discount_percentage = 0.20;
                } elseif ($user_group == $gold_group) {
                    $discount_percentage = 0.25;
                } elseif ($user_group == $platinum_group) {
                    $discount_percentage = 0.30;
                }
            }
        } elseif (in_array('pendants', $categories)) {
            if (stripos($material, 'silver') !== false) {
                $discount_name = 'Silver Pendants Discount';
                if ($user_group == $bronze_group) {
                    $discount_percentage = 0.30;
                } elseif ($user_group == $silver_group) {
                    $discount_percentage = 0.35;
                } elseif ($user_group == $gold_group || $user_group == $platinum_group) {
                    $discount_percentage = 0.40;
                }
            } elseif (stripos($material, 'gold') !== false) {
                $discount_name = 'Gold Pendants Discount';
                if ($user_group == $bronze_group) {
                    $discount_percentage = 0.15;
                } elseif ($user_group == $silver_group) {
                    $discount_percentage = 0.20;
                } elseif ($user_group == $gold_group) {
                    $discount_percentage = 0.25;
                } elseif ($user_group == $platinum_group) {
                    $discount_percentage = 0.30;
                }
            }
        } elseif (in_array('bangles', $categories)) {
            if (stripos($material, 'silver') !== false) {
                $discount_name = 'Silver Bangles Discount';
                if ($user_group == $bronze_group) {
                    $discount_percentage = 0.30;
                } elseif ($user_group == $silver_group) {
                    $discount_percentage = 0.35;
                } elseif ($user_group == $gold_group || $user_group == $platinum_group) {
                    $discount_percentage = 0.40;
                }
            } elseif (stripos($material, 'gold') !== false) {
                $discount_name = 'Gold Bangles Discount';
                if ($user_group == $bronze_group) {
                    $discount_percentage = 0.15;
                } elseif ($user_group == $silver_group) {
                    $discount_percentage = 0.20;
                } elseif ($user_group == $gold_group) {
                    $discount_percentage = 0.25;
                } elseif ($user_group == $platinum_group) {
                    $discount_percentage = 0.30;
                }
            }
        }

        if ($discount_percentage > 0) {
            $discount_amount = $price * $discount_percentage;
            
            // remove 10% tax
            $discount_amount = $discount_amount * 100 / 110;
            
            $cart->add_fee($discount_name, -$discount_amount, false);
        }
    }
}

// Helper function to get product categories
function get_product_categories($product_id) {
    $categories = [];
    $terms = wp_get_post_terms($product_id, 'product_cat', array('fields' => 'slugs'));
    if (!is_wp_error($terms) && !empty($terms)) {
        $categories = $terms;
    }
    if (empty($categories)) {
        $terms = get_the_terms($product_id, 'product_cat');
        if (!is_wp_error($terms) && !empty($terms)) {
            $categories = wp_list_pluck($terms, 'slug');
        }
    }
    if (empty($categories) && wc_get_product($product_id)->is_type('variation')) {
        $parent_id = wc_get_product($product_id)->get_parent_id();
        $terms = wp_get_post_terms($parent_id, 'product_cat', array('fields' => 'slugs'));
        if (!is_wp_error($terms) && !empty($terms)) {
            $categories = $terms;
        }
    }
    return $categories;
}
Leave a Comment