These two snippets work fine separately, but I can't seem to get them to work together and I am pretty sure its something to do with hierarchy of the code but not sure. Trying to check if a product from a certain category is in the cart and if so, change the cart total to a set price. The cart check works when i output a message to the frontend, and the cart total change works on its own too, but no change when put together.
add_action( 'woocommerce_before_cart', 'check_category_in_cart' );
function check_category_in_cart() {
$cat_in_cart = false;
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
// If Cart has category "services", set $cat_in_cart to true
if ( has_term( 'services', 'product_cat', $cart_item['product_id'] ) ) {
$cat_in_cart = true;
break;
}
}
if ( $cat_in_cart ) {
add_filter( 'woocommerce_calculated_total', 'change_calculated_total', 10, 2 );
function change_calculated_total( $total, $cart ) {
return 50;
}
}
}
Aucun commentaire:
Enregistrer un commentaire