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$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: You can then target the checkout add-on id to add attributes or change defaults.
Move Checkout Add-on Location
↑ Back to topwc_checkout_add_ons_position
filter, which can accept a different action on the checkout page:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 topNote 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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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' ); |
$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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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' ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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' ); |