I simply added an insurance-button to my cart web page the place a hidden insurance coverage product is robotically added to the cart if the purchasers needs so. Due to this fact I used and upated an older PHP snippet I discovered that was really useful by woocommerce for instances like this. This works fairly high quality.
My drawback is that this operate mustn’t add just one insurance coverage – it ought to robotically add a amount of insurances primarily based on the full variety of merchandise within the cart. For instance: When the shopper buys a complete amount of 5 merchandise and needs to get an insurance coverage it ought to add the insurance coverage 5 occasions when he clicks the button.
I do know I can get the variety of cart gadgets together with their amount counts with WC()->cart->get_cart_contents_count()
however I can’t determine find out how to use this in my PHP snippet or the shortcode. Possibly somebody can assist?
Right here’s my initially code:
add_action('woocommerce_cart_totals_after_shipping', 'wc_shipping_insurance_note_after_cart');
operate wc_shipping_insurance_note_after_cart() {
international $woocommerce;
$product_id = 7597;
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if ( $_product->id == $product_id )
$discovered = true;
}
if ( ! $discovered ):
?>
<tr class="woocommerce-shipping-totals delivery">
<th><?php _e( 'Versicherung', 'woocommerce' ); ?></th>
<td data-title="Versicherung"> <p>Rücknahmeschutz: Erstattung der Kosten z.B. bei Erkrankung. Infos <a href="https://rudolfweber-events.de/produkt/kartenruecknahmeschutz-versicherung/">hier</a>.
</p><a href="<?php echo do_shortcode('[add_to_cart_url id="7597"]'); ?>"><button><?php _e( 'Hinzufügen (+17,90€/Ticket)' ); ?> </button></a></td>
</tr>
<?php else: ?>
<tr class="woocommerce-shipping-totals delivery">
<th><?php _e( 'Versicherung', 'woocommerce' ); ?></th>
<td data-title="Versicherung">€17,90</td>
</tr>
<?php endif;
}