• Hello
    i made some products as services(category is “services”).
    when i add some products to cart: some services added automatically to cart by this code:

    add_action( 'template_redirect', 'iteam_sync_product_services' );
    function iteam_sync_product_services() {
        $services_map = [];
    
        foreach( WC()->cart->cart_contents as $prod_in_cart ) {
            $product_id = $prod_in_cart['product_id'];
    
            $product_services = get_field('services', $product_id);
    
            if (has_term( 'services', 'product_cat', $product_id )) {
                $product_cart_id = WC()->cart->generate_cart_id( $product_id );
                WC()->cart->remove_cart_item($product_cart_id);
            }
    
            if (!empty($product_services)) {
                foreach ($product_services as $service_id) {
                    @$services_map[$service_id] += $prod_in_cart['quantity'];
                }
            }
    
        }
    
        foreach ($services_map as $product_id => $quantity) {
            WC()->cart->add_to_cart($product_id, $quantity);
        }
    
    } 

    the problem is when I want to delete specific service items from cart I cant because the previous function is recalled and the service item appeared again
    can I find solution to my problem

  • The topic ‘delete items from cart that i automatically added with products’ is closed to new replies.