Home > database >  Add optional additional fee to checkout
Add optional additional fee to checkout

Time:12-16

i am using this following code to show the option to add an additional fee (product) to the cart on the "cart" page. It is working great but it is now showing on the cart page, but how do i get it to show on the checkout page additionally:

<?php
/* ADD custom theme functions here  */
add_filter( 'woocommerce_price_trim_zeros', 'wc_hide_trailing_zeros', 10, 1 );
function wc_hide_trailing_zeros( $trim ) {
    return true;
}
add_action('woocommerce_cart_totals_after_shipping', 'wc_shipping_insurance_note_after_cart');
function wc_shipping_insurance_note_after_cart() {
global $woocommerce;
    $product_id = 971;
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 >
        <th><?php _e( 'Gift wrapper', 'woocommerce' ); ?></th>
        <td><a href="<?php echo do_shortcode('[add_to_cart_url id="971"]'); ?>"><?php _e( 'Add ($3)' ); ?> </a></td>
    </tr>
<?php else: ?>
    <tr >
        <th><?php _e( 'Gift wrapper', 'woocommerce' ); ?></th>
        <td>$3</td>
    </tr>
<?php endif;
}

I have tried different methods, and it should be pretty basic but i am rusty on my Functions.php skills.

CodePudding user response:

You could try copying the function and giving it a new name. Then change the hook depending on where you want it on the checkout page. Here is a great visual guide to the hooks on the checkout page: https://www.businessbloomer.com/woocommerce-visual-hook-guide-checkout-page/

Try something like this:

Notice how I changed the hook from woocommerce_cart_totals_after_shipping to woocommerce_before_checkout_form here you can experiment using the hooks on the guide I linked to. I also changed the name of the function from wc_shipping_insurance_note_after_cart to wc_shipping_insurance_note_after_checkout to avoid conflicts.

add_action('woocommerce_before_checkout_form', 'wc_shipping_insurance_note_after_checkout');
function wc_shipping_insurance_note_after_checkout() {
global $woocommerce;
    $product_id = 971;
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 >
        <th><?php _e( 'Gift wrapper', 'woocommerce' ); ?></th>
        <td><a href="<?php echo do_shortcode('[add_to_cart_url id="971"]'); ?>"><?php _e( 'Add ($3)' ); ?> </a></td>
    </tr>
<?php else: ?>
    <tr >
        <th><?php _e( 'Gift wrapper', 'woocommerce' ); ?></th>
        <td>$3</td>
    </tr>
<?php endif;
}
  • Related