I am working on a dynamic pricing system on my Wordpress store. I have set it up so users who have certain roles (right now, subscriber or admin - for testing purposes) get a 15% discount (price*0.85). However, I need to also exclude specific product categories from the discount rule.
Right now I have:
function add_cart_item_data( $cart_item_data, $product_id, $variation_id ) {
//if admin || subscriber
$role = get_user_role(); /* I know that isn't the default get user role. I made a shortened version of it in functions.php because I used it a lot in other functions */
if (in_array("admin", $role) || in_array("subscriber", $role)){
$product = wc_get_product( $product_id );
// ==> Start: Needed product category check HERE
$price = $product->get_price();
$cart_item_data['RefPrice'] = $price * 0.85;
// ==> End of product category check
}
return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data', 'add_cart_item_data', 10, 2 );
function before_calculate_totals( $cart_obj ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
return;
}
// Iterate through each cart item
foreach( $cart_obj->get_cart() as $key=>$value ) {
if( isset( $value['RefPrice'] ) ) {
$price = $value['RefPrice'];
$value['data']->set_price( ( $price ) );
}
}
}
add_action( 'woocommerce_before_calculate_totals', 'before_calculate_totals', 10, 1 );
This is working to modify the price by * 0.85
for certain roles, but I've hit a brick wall trying to exclude a product category from it. The product category ID is 978 (or its name is "last minute gifts").
How can I add in this check for each cart item?