Last active
June 4, 2019 14:13
-
-
Save bekarice/0241196cc387edeb24c9 to your computer and use it in GitHub Desktop.
Remove WooCommerce checkout add-ons if all products in cart are in a 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 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 = get_option( 'wc_checkout_add_ons_position' ); | |
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' ); |
Doesn't work
This one seems to work. as its for WooCom 3.0 +
Hi, It's posibble to remove/add only specific add-on ID ?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This appears to be broken at this time. Checkout Add-ons Version 1.10.4
Checkout Add-ons are no longer hidden if all products in the cart are in the same category.