冷凍本島運費 商品id: 61910
常溫本島運費 商品id: 61908
冷凍外島運費 商品id: 61914
常溫外島運費 商品id: 61912
當cold 總額,小於1988元,且 state 不等於 澎湖縣, 金門縣, 連江縣,幫我加入商品id 61910
當normal總額,小於1988元,且 state 不等於 澎湖縣, 金門縣, 連江縣,幫我加入商品id 61912
當cold總額,小於4500元,且 state 等於 澎湖縣, 金門縣, 連江縣,幫我加入商品id 61914
當normal總額,小於4500元,且 state 等於 澎湖縣, 金門縣, 連江縣,幫我加入商品id 61912
state 要搭配外掛 RY WC City Select
接著再啟用外掛,即可達成條件
function custom_update_cart_based_on_shipping_class_and_state() { $cart = WC()->cart; $cart_items = $cart->get_cart(); $cold_total = 0; $normal_total = 0; $cold_product_id = 61910; $normal_product_id = 61908; $cold_special_id = 61914; $normal_special_id = 61912; // 計算冷凍和常溫商品的總額 foreach ($cart_items as $cart_item_key => $cart_item) { $product = $cart_item['data']; $shipping_class_id = $product->get_shipping_class_id(); $shipping_class = get_term_by('id', $shipping_class_id, 'product_shipping_class'); if ($shipping_class) { if ($shipping_class->slug == 'cold') { $cold_total += $cart_item['line_total']; } elseif ($shipping_class->slug == 'normal') { $normal_total += $cart_item['line_total']; } } } // 獲取用戶的州(省) $customer_state = WC()->customer->get_shipping_state(); $special_states = array('PENGHU COUNTY', 'KINMEN COUNTY', 'LIENCHIANG COUNTY'); // 檢查冷凍商品的總額和州(省),並添加或移除商品 if ($cold_total > 0) { if ($cold_total < 1988 && !in_array($customer_state, $special_states)) { if (!$cart->find_product_in_cart($cart->generate_cart_id($cold_product_id))) { $cart->add_to_cart($cold_product_id); } } else { if ($cart->find_product_in_cart($cart->generate_cart_id($cold_product_id))) { $cart->remove_cart_item($cart->generate_cart_id($cold_product_id)); } } if ($cold_total < 4500 && in_array($customer_state, $special_states)) { if (!$cart->find_product_in_cart($cart->generate_cart_id($cold_special_id))) { $cart->add_to_cart($cold_special_id); } } else { if ($cart->find_product_in_cart($cart->generate_cart_id($cold_special_id))) { $cart->remove_cart_item($cart->generate_cart_id($cold_special_id)); } } } else { // 移除冷凍商品 if ($cart->find_product_in_cart($cart->generate_cart_id($cold_product_id))) { $cart->remove_cart_item($cart->generate_cart_id($cold_product_id)); } if ($cart->find_product_in_cart($cart->generate_cart_id($cold_special_id))) { $cart->remove_cart_item($cart->generate_cart_id($cold_special_id)); } } // 檢查常溫商品的總額和州(省),並添加或移除商品 if ($normal_total > 0) { if ($normal_total < 1988 && !in_array($customer_state, $special_states)) { if (!$cart->find_product_in_cart($cart->generate_cart_id($normal_product_id))) { $cart->add_to_cart($normal_product_id); } } else { if ($cart->find_product_in_cart($cart->generate_cart_id($normal_product_id))) { $cart->remove_cart_item($cart->generate_cart_id($normal_product_id)); } } if ($normal_total < 4500 && in_array($customer_state, $special_states)) { if (!$cart->find_product_in_cart($cart->generate_cart_id($normal_special_id))) { $cart->add_to_cart($normal_special_id); } } else { if ($cart->find_product_in_cart($cart->generate_cart_id($normal_special_id))) { $cart->remove_cart_item($cart->generate_cart_id($normal_special_id)); } } } else { // 移除常溫商品 if ($cart->find_product_in_cart($cart->generate_cart_id($normal_product_id))) { $cart->remove_cart_item($cart->generate_cart_id($normal_product_id)); } if ($cart->find_product_in_cart($cart->generate_cart_id($normal_special_id))) { $cart->remove_cart_item($cart->generate_cart_id($normal_special_id)); } } } add_action('woocommerce_before_calculate_totals', 'custom_update_cart_based_on_shipping_class_and_state'); function lock_special_product_quantity($cart_item_data, $cart_item_key) { $special_product_ids = array(61910, 61908, 61914, 61912); if (in_array($cart_item_data['product_id'], $special_product_ids)) { $cart_item_data['quantity'] = 1; } return $cart_item_data; } add_filter('woocommerce_add_cart_item', 'lock_special_product_quantity', 10, 2); add_filter('woocommerce_get_cart_item_from_session', 'lock_special_product_quantity', 10, 2); add_filter('woocommerce_add_to_cart_quantity', function($quantity, $product_id) { $special_product_ids = array(61910, 61908, 61914, 61912); if (in_array($product_id, $special_product_ids)) { $quantity = 1; } return $quantity; }, 10, 2);