Skip to content

Instantly share code, notes, and snippets.

@braddalton
Last active June 2, 2024 05:02
Show Gist options
  • Save braddalton/577a31befa3339de2c02991dc73deb57 to your computer and use it in GitHub Desktop.
Save braddalton/577a31befa3339de2c02991dc73deb57 to your computer and use it in GitHub Desktop.
// Add €40 deposit to the cart based on product ID
add_action('woocommerce_cart_calculate_fees', 'add_deposit_fee');
function add_deposit_fee() {
if (is_admin() && !defined('DOING_AJAX')) return;
$deposit_fee = 40; // Deposit amount
$product_ids = array(391); // Replace with your product IDs
$add_deposit = false;
foreach (WC()->cart->get_cart() as $cart_item) {
if (in_array($cart_item['product_id'], $product_ids)) {
$add_deposit = true;
break;
}
}
if ($add_deposit) {
WC()->cart->add_fee(__('Deposit', 'woocommerce'), $deposit_fee);
}
}
// Add deposit meta to order
add_action('woocommerce_checkout_create_order', 'add_deposit_order_meta', 20, 2);
function add_deposit_order_meta($order, $data) {
$product_ids = array(391); // Replace with your product IDs
$add_deposit = false;
foreach ($order->get_items() as $item_id => $item) {
if (in_array($item->get_product_id(), $product_ids)) {
$add_deposit = true;
break;
}
}
if ($add_deposit) {
$order->update_meta_data('_deposit_amount', 40);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment