Add shipping insurance for WooCommerce orders

Categories: Wordpress, CMS

Custom code to add shipping insurance option to WooCommerce orders:

add_action('woocommerce_cart_totals_after_shipping', 'wc_shipping_insurance_note_after_cart');
function wc_shipping_insurance_note_after_cart() {
global $woocommerce;
    $product_id = 81822;
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( 'Cobertura envío', 'woocommerce' ); ?></th>
        <td><a href="<?php echo do_shortcode('[add_to_cart_url id="81822"]'); ?>"><i class="fas fa-check"></i><?php _e( 'Añade convertura de envío (+0.50€)' ); ?> </a><br />En caso de deterioro o extravío no tendrás que esperar a que la situación se resuelva dentro del plazo ordinario. Simplemente notifícanoslo, documenta la incidencia y nosotros te reenviaremos el producto inmediatamente.</td>
<?php else: ?>
    <tr class="shipping">
        <th><?php _e( 'Cobertura envío', 'woocommerce' ); ?></th>
<?php endif;

Create a private product called Shipping Insurance or whatever you’d like and replace $product_id with the ID of the product created.

Edit the rest of the text as needed.