Last active
July 25, 2017 05:35
-
-
Save johnmontfx/c073ddf3506df3de3fb431098f48f51a to your computer and use it in GitHub Desktop.
Hide fields in woo based upon category
This file contains hidden or 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
## Based upon code from https://calebburks.com/conditionally-showhide-woocommerce-checkout-fields/ | |
function fx_category_is_in_the_cart() { | |
// Add the category slugs here that you want to have the fields removed for | |
$categories = array( 'clothing', 'posters' ); | |
// Products currently in the cart | |
$cart_ids = array(); | |
// Categories currently in the cart | |
$cart_categories = array(); | |
// Find each product in the cart and add it to the $cart_ids array | |
foreach( WC()->cart->get_cart() as $cart_item_key => $values ) { | |
$cart_product = $values['data']; | |
$cart_ids[] = $cart_product->id; | |
} | |
// Connect the products in the cart w/ their categories | |
foreach( $cart_ids as $id ) { | |
$products_categories = get_the_terms( $id, 'product_cat' ); | |
// Loop through each product category and add it to our $cart_categories array | |
foreach ( $products_categories as $products_category ) { | |
$cart_categories[] = $products_category->slug; | |
} | |
} | |
// If one of the special categories are in the cart, return true. | |
if ( ! empty( array_intersect( $categories, $cart_categories ) ) ) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
function fx_remove_checkout_fields( $fields ) { | |
if ( ! fx_category_is_in_the_cart() ) { | |
// add or remove billing fields you do not want in this section | |
// list of fields: http://docs.woothemes.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/#section-2 | |
unset( $fields['billing']['billing_company'] ); | |
unset( $fields['billing']['billing_phone'] ); | |
unset( $fields['billing']['billing_address_1'] ); | |
unset( $fields['billing']['billing_city'] ); | |
} | |
return $fields; | |
} | |
add_filter( 'woocommerce_checkout_fields' , 'fx_remove_checkout_fields' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment