Skip to main content

Snippets – WooCommerce Add Insurance On Checkout

By May 18, 2020March 5th, 2021Start

You can use this for anything really, but I use it for setting a shipping insurance fee on checkout. Pretty simple really.

(1) Add a hidden product with the value (dollar amount)
(2) Save the hidden product and get the product ID
(3) Change ID and Costs in the code below.
(4) Add all this to your functions file or create a stand alone plugin.

add_action('woocommerce_cart_totals_after_shipping', 'wc_shipping_insurance_note_after_cart');
function wc_shipping_insurance_note_after_cart() {
global $woocommerce;
    $product_id = 1963;  // Change ID
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
    $_product = $values['data'];
    if ( $_product->id == $product_id )
        $found = true;
    // if product not found, add it
if ( ! $found ):
    <tr class="shipping">
        <th><?php _e( 'Shipping Insurance', 'woocommerce' ); ?></th>
        <td><a href="<?php echo do_shortcode('[add_to_cart_url id="1963"]'); ?>"><!-- Change ID --><?php _e( 'Add shipping insurance (+$25) ' ); ?><!-- Change Price --></a></td>
<?php else: ?>
    <tr class="shipping">
        <th><?php _e( 'Shipping Insurance', 'woocommerce' ); ?></th>
        <td>$25</td> <!-- Change Price -->
<?php endif;