In WooCommerce, I would like to check if the products in the shopping cart have the attribute 'Varifocal'
(so I can show/hide checkout fields).
I'm struggling to get an array of id's of all the variations which have the attribute 'varifocal'. It would be really appreciated if someone can point me in the right direction.
The taxonomy is pa_lenses
.
I currently have the following function:
function varifocal() {
// Add product IDs here
$ids = array();
// 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->get_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;
}
}
Here is a custom conditional function that will return
true
when the a specific attribute argument is found in one cart item (product variation):Code goes in functions.php file of your active child theme (or active theme). Tested and works.