Files
cdnPRO/kreatywnybrand-pl/umam.php

83 lines
2.4 KiB
PHP

<?
add_action('woocommerce_cart_calculate_fees', 'add_custom_fees');
function add_custom_fees() {
if (is_admin() && !defined('DOING_AJAX')) return;
$fee_counter = 0; // Licznik do tworzenia unikalnych identyfikatorów opłat
foreach(WC()->cart->get_cart() as $cart_item) {
$product_id = $cart_item['product_id'];
$variation_id = $cart_item['variation_id']; // ID wariantu, jeśli istnieje
$quantity = $cart_item['quantity'];
$fee_amount = get_fee_amount($product_id, $variation_id);
if ($fee_amount > 0) {
$fee_total = $fee_amount * $quantity;
$fee_label = __('Opłata za opakowanie') . ' #' . ++$fee_counter; // Tworzenie unikalnej etykiety
WC()->cart->add_fee($fee_label, $fee_total, true);
}
}
}
function get_fee_amount($product_id, $variation_id = null) {
// Opłaty dla konkretnych produktów lub wariantów
$fee_ids = array(
8126 => 0.90,
12399 => 0.90,
5936 => 0.90,
7977 => 0.90,
11178 => 0.90,
3592 => 0.90,
7458 => 0.90,
11174 => 0.90,
10923 => 0.90,
3247 => 0.90,
2801 => 1.80,
2757 => 1.80,
2807 => 2.70,
2760 => 2.70,
2770 => 5.40,
2809 => 5.40,
2811 => 9.90,
2784 => 9.90,
2793 => 18.90,
2814 => 18.90,
2362 => 0.90,
4228 => 0.90,
4259 => 0.90,
4288 => 0.90,
2295 => 0.90,
2574 => 0.90,
2712 => 0.90,
2734 => 0.90,
3158 => 0.90,
8299 => 0.90
);
// Opłaty dla wybranych wariantów produktów
$variation_fees = array(
10934 => 0.90, // ID wariantu => opłata
10936 => 0.90
// Dodaj więcej wariantów, jeśli to konieczne
);
// Sprawdzenie opłaty dla wariantu
if ($variation_id && array_key_exists($variation_id, $variation_fees)) {
return $variation_fees[$variation_id];
}
// Sprawdzenie opłaty dla produktu
if (array_key_exists($product_id, $fee_ids)) {
return $fee_ids[$product_id];
}
/*// Domyślna opłata dla kategorii
$category_fee = 0.90;
$categories = array('torty', 'ciasta-swiateczne', 'torty_komunijne_umam', 'torty-standardowe', 'inne', 'konfitury-i-pasty', 'makaroniki','wielkanoc-z-umam');
foreach ($categories as $category) {
if (has_term($category, 'product_cat', $product_id)) {
return $category_fee;
}
}*/
return 0; // Brak opłaty domyślnie
}