Created
October 27, 2015 13:30
-
-
Save WPprodigy/b0479625b14b4b7ee789 to your computer and use it in GitHub Desktop.
Conditionally show/hide WooCommerce checkout fields - http://calebburks.com/conditionally-showhide-woocommerce-checkout-fields
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 | |
/** | |
* Plugin Name: Conditional WooCommerce Checkout Fields | |
* Description: Adds the abilitiy to conditionally show / hide checkout fields. | |
* Version: 1.0 | |
* Author: Caleb Burks | |
* Author URI: http://calebburks.com | |
*/ | |
/** | |
* Check if a specific product ID is in the cart | |
*/ | |
function wc_ninja_product_is_in_the_cart( $ids ) { | |
// Products currently in the cart | |
$cart_ids = 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; | |
} | |
// If one of the special products are in the cart, return true. | |
if ( ! empty( array_intersect( $ids, $cart_ids ) ) ) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
/** | |
* Check if a specific product category is in the cart | |
*/ | |
function wc_ninja_category_is_in_the_cart( $categories ) { | |
// 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; | |
} | |
} | |
/** | |
* Check if a specific coupon was used | |
*/ | |
function wc_ninja_coupon_was_used( $coupons ) { | |
// Coupons Used | |
$coupons_used = WC()->cart->applied_coupons; | |
// If ones of our special coupons were used, return true. | |
if ( in_array( $coupons, $coupons_used ) ) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
/** | |
* Conditionally remove checkout fields | |
*/ | |
function wc_ninja_remove_checkout_field( $fields ) { | |
$product_ids = array( '45', '70', '75' ); | |
$categories = array( 'clothing', 'posters' ); | |
$coupons = array( 'flat_discout', 'free_shipping' ); | |
// If a special coupon was not used, hide the Company Field | |
if ( ! wc_ninja_coupon_was_used( $coupons ) ) { | |
unset( $fields['billing']['billing_company'] ); | |
} | |
// If a special category is in the cart, hide the First Name Field | |
if ( wc_ninja_category_is_in_the_cart( $categories ) ) { | |
unset( $fields['billing']['billing_first_name'] ); | |
} | |
// If a special product is not in the cart, hide the Phone Number Field | |
if ( ! wc_ninja_product_is_in_the_cart( $product_ids ) ) { | |
unset( $fields['billing']['billing_phone'] ); | |
} | |
return $fields; | |
} | |
add_filter( 'woocommerce_checkout_fields' , 'wc_ninja_remove_checkout_field' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, thank you for this one. Also, for anyone who will be using this, take note that
$cart_ids[] = $cart_product->id;
the 'id' will give an error to the newer woocommerce version. What I did is,
$cart_ids[] = $cart_product->get_id();
using get_id() instead of id.