Product Specific Conditional

/** * Check if a specific product ID is in the cart */ function wc_ninja_product_is_in_the_cart() { // Add your special product IDs here $ids = array( ’45’, ’70’, ’75’ );; // 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; } }

 

/**
* Conditionally remove a checkout field based on products in the cart
*/
function wc_ninja_remove_checkout_field( $fields ) {
if ( ! wc_ninja_product_is_in_the_cart() ) {
unset( $fields[‘billing’][‘billing_company’] );
}

return $fields;
}
add_filter( ‘woocommerce_checkout_fields’ , ‘wc_ninja_remove_checkout_field’ );

 

Product Category Conditional

 

/**
* Check if a specific product category is in the cart
*/
function wc_ninja_category_is_in_the_cart() {
// Add your special category slugs here
$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;
}
}

 

Coupon Conditional

 

/**
* Check if a specific coupon was used
*/
function wc_ninja_coupon_was_used() {
// Add a list of coupons to check for here.
$coupons = array( ‘percent_coupon’, ‘free_shipping’ );

// 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;
}
}