I am trying to build an woocommerce extension which will add an additional cost to product and shows in product single page, cart page and in checkout page and in woocommerce order meta data. Everything works fine. But the additional price added twice once order is placed in woocommerce order confirmation page and shows in order meta data as twice.
// Add custom addon cost field to product general options
function custom_addon_field()
{
woocommerce_wp_text_input(
array(
‘id’ => ‘_addon_cost’,
‘label’ => __(‘Addon Cost’, ‘woocommerce’),
‘placeholder’ => __(‘Enter addon cost’, ‘woocommerce’),
‘desc_tip’ => ‘true’,
‘description’ => __(‘Enter the additional cost for this addon.’, ‘woocommerce’),
‘type’ => ‘number’,
‘custom_attributes’ => array(
‘step’ => ‘any’,
‘min’ => ‘0’
),
)
);
}
add_action(‘woocommerce_product_options_general_product_data’, ‘custom_addon_field’);
// Save custom addon cost field value
function save_custom_addon_field($post_id)
{
$addon_cost = isset($_POST[‘_addon_cost’]) ? wc_clean($_POST[‘_addon_cost’]) : ‘’;
update_post_meta($post_id, ‘_addon_cost’, $addon_cost);
}
add_action(‘woocommerce_process_product_meta’, ‘save_custom_addon_field’);
// Display custom addon cost on product single page
function display_custom_addon_cost_on_single_product()
{
global $product;
$addon_cost = get_post_meta($product->get_id(), '_addon_cost', true);
if ($addon_cost) {
echo '<p class="addon-cost">' . __('Addon Cost:', 'woocommerce') . ' ' . wc_price($addon_cost) . '</p>';
}
}
add_action(‘woocommerce_single_product_summary’, ‘display_custom_addon_cost_on_single_product’, 11);
// Add custom addon cost to cart
function add_custom_addon_cost_to_cart($cart_item_data, $product_id)
{
$addon_cost = get_post_meta($product_id, ‘_addon_cost’, true);
if ($addon_cost) {
$cart_item_data['addon_cost'] = $addon_cost;
}
return $cart_item_data;
}
add_filter(‘woocommerce_add_cart_item_data’, ‘add_custom_addon_cost_to_cart’, 10, 2);
// Calculate custom addon cost in cart totals
function calculate_custom_addon_cost_in_totals($cart)
{
foreach ($cart->get_cart() as $cart_item_key => $cart_item) {
if (isset($cart_item[‘addon_cost’])) {
$addon_cost = floatval($cart_item[‘addon_cost’]);
// Set the new price including the addon cost
$cart_item['data']->set_price($cart_item['data']->get_price() + $addon_cost);
}
}
}
add_action(‘woocommerce_before_calculate_totals’, ‘calculate_custom_addon_cost_in_totals’);
// Display custom addon cost in cart and checkout page
function display_custom_addon_cost_in_cart_and_checkout($item_data, $cart_item)
{
if (isset($cart_item[‘addon_cost’])) {
$addon_cost = $cart_item[‘addon_cost’];
$item_data = array(
‘key’ => __(‘Addon Cost’, ‘woocommerce’),
‘value’ => wc_price($addon_cost),
‘display’ => ‘’,
);
}
return $item_data;
}
add_filter(‘woocommerce_get_item_data’, ‘display_custom_addon_cost_in_cart_and_checkout’, 10, 2);
// Save custom addon cost to order meta
function save_custom_addon_cost_to_order_meta($item_id, $values)
{
if (isset($values[‘addon_cost’])) {
wc_add_order_item_meta($item_id, __(‘Addon Cost’, ‘woocommerce’), $values[‘addon_cost’]);
}
}
add_action(‘woocommerce_add_order_item_meta’, ‘save_custom_addon_cost_to_order_meta’, 10, 2);