1. Documentation /
  2. WooCommerce Checkout Add-ons Developer Docs

WooCommerce Checkout Add-ons Developer Docs

Please be aware that this document is meant for developers. We do not support or do plugin customizations as per our support policy.
If you need help changing this code or extending it, we recommend using a WooExpert.

Overview

↑ Back to top
Checkout Add-ons will add order fees for WooCommerce orders. These fee items are added as order line items, so you could access this data via the order line items. At checkout, add-ons use core WooCommerce fields. By default, WooCommerce uses the following attributes for fields:

$defaults = array(
    'type'              => 'text',
    'label'             => '',
    'description'       => '',
    'placeholder'       => '',
    'maxlength'         => false,
    'required'          => false,
    'id'                => $key,
    'class'             => array(),
    'label_class'       => array(),
    'input_class'       => array(),
    'return'            => false,
    'options'           => array(),
    'custom_attributes' => array(),
    'validate'          => array(),
    'default'           => '',
);
You can add custom attributes by adding an array of attributes, or changing these defaults. Please note that you should not change the “type” attribute for a checkout add-on. To make any adjustments, you’ll need the checkout add-on id, which can be found by viewing add-ons: WooCommerce Checkout Add-ons id You can then target the checkout add-on id to add attributes or change defaults.

Move Checkout Add-on Location

↑ Back to top
By default, add-ons are displayed after the billing details at checkout. You have limited ability to move the add-ons in the plugin settings, and have more fine-grained control via the wc_checkout_add_ons_position filter, which can accept a different action on the checkout page:
<?php
// Reposition Checkout Addons to under Order Notes
function sv_wc_checkout_addons_change_position() {
return 'woocommerce_after_order_notes';
}
add_filter( 'wc_checkout_add_ons_position', 'sv_wc_checkout_addons_change_position' );

Conditionally Display Add-ons

↑ Back to top
Sometimes users only want to display checkout add-ons as “gift” options. Here’s a snippet that will hide or show checkout add-ons based on the shipping address. If the shipping address is the same as billing, the fields will be hidden. If shipping differs from the billing address, the gift add-on fields will be shown.
Note that this should not be used with required add-ons, as customers won’t be able to complete the field if it’s hidden. These add-ons should always be optional.
/**
* Conditionally show gift add-ons if shipping address differs from billing
**/
function wc_checkout_add_ons_conditionally_show_gift_add_on() {
wc_enqueue_js( "
$( 'input[name=ship_to_different_address]' ).change( function () {
if ( $( this ).is( ':checked' ) ) {
// show the gift checkout add-on — replace '2' with the id of your add-on
$( '#wc_checkout_add_ons_2_field' ).show();
} else {
// hide the gift checkout add-on — replace '2' with the id of your add-on
$( '#wc_checkout_add_ons_2_field' ).hide();
}
} ).change();
" );
}
add_action( 'wp_enqueue_scripts', 'wc_checkout_add_ons_conditionally_show_gift_add_on' );
You can also use your own code to determine when add-ons will be shown, and conditionally remove them using:
$position = apply_filters( 'wc_checkout_add_ons_position', get_option( 'wc_checkout_add_ons_position', 'woocommerce_checkout_after_customer_details' ) );
remove_action( $position, array( wc_checkout_add_ons()->frontend, 'render_add_ons' ) );
For example, you could remove add-ons if any product in the cart is in a particular category:
<?php // only copy if needed
// Remove add-ons if any product in the cart is in the "Gift box" category
function sv_remove_checkout_add_ons_for_giftboxes() {
if ( function_exists( 'wc_checkout_add_ons' ) ) {
$cat_check = false;
// check each cart item for our category
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product = $cart_item['data'];
// replace 'gift_box' with your category's slug
if ( has_term( 'gift_box', 'product_cat', $product->id ) ) {
$cat_check = true;
// we only need one "true" to leave
break;
}
}
// if a product in the cart is in our category, remove the add-ons
if ( $cat_check ) {
// get the add-ons current position so we know where to remove them from
$position = apply_filters( 'wc_checkout_add_ons_position', get_option( 'wc_checkout_add_ons_position', 'woocommerce_checkout_after_customer_details' ) );
remove_action( $position, array( wc_checkout_add_ons()->get_frontend_instance(), 'render_add_ons' ), 20 );
}
}
}
add_action( 'woocommerce_before_checkout_form', 'sv_remove_checkout_add_ons_for_giftboxes' );
Or you could remove add-ons only if all products in the cart are in the category:
<?php // only copy if needed
// Remove add-ons if all products in the cart are in the "Gift box" category (and thus gift-wrapped already)
function sv_remove_checkout_add_ons_for_giftboxes() {
if ( function_exists( 'wc_checkout_add_ons' ) ) {
// holds checks for all products in cart to see if they're in our category
$category_checks = array();
// check each cart item for our category
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product = $cart_item['data'];
// replace 'gift_box' with your category's slug
$product_in_cat = has_term( 'gift_box', 'product_cat', $product->id );
array_push( $category_checks, $product_in_cat );
}
// if all items are in this category, remove the checkout add-ons
if ( ! in_array( false, $category_checks, true ) ) {
// get the add-ons current position so we know where to remove them from
$position = apply_filters( 'wc_checkout_add_ons_position', get_option( 'wc_checkout_add_ons_position', 'woocommerce_checkout_after_customer_details' ) );
remove_action( $position, array( wc_checkout_add_ons()->get_frontend_instance(), 'render_add_ons' ), 20 );
}
}
}
add_action( 'woocommerce_before_checkout_form', 'sv_remove_checkout_add_ons_for_giftboxes' );

User Docs

↑ Back to top
Return to the user documentation →