Last active
June 2, 2024 05:02
-
-
Save braddalton/577a31befa3339de2c02991dc73deb57 to your computer and use it in GitHub Desktop.
Add Deposit to WooCommerce Product https://wpsites.net/product-tag/woocommerce_cart_calculate_fees/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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