Title: Remove items
Last modified: October 4, 2021

---

# Remove items

 *  Resolved [danielbmxd](https://wordpress.org/support/users/danielbmxd/)
 * (@danielbmxd)
 * [4 years, 7 months ago](https://wordpress.org/support/topic/remove-items-4/)
 * Hello, is there a hook or filter that automatically removes products out of stock
   from the cart? In the case of a customer who enters after time and some products
   are no longer available and avoid having to delete them manually

Viewing 2 replies - 1 through 2 (of 2 total)

 *  Plugin Support [abwaita a11n](https://wordpress.org/support/users/abwaita/)
 * (@abwaita)
 * [4 years, 7 months ago](https://wordpress.org/support/topic/remove-items-4/#post-14934923)
 * Hi [@danielbmxd](https://wordpress.org/support/users/danielbmxd/),
 * I found the following custom code from [this article](https://orbisius.com/blog/how-to-auto-remove-out-of-stock-woocommerce-products-from-cart-and-notify-user-p4308)
   and it seems to be working well:
 * 
    Link to image: [https://snipboard.io/DgH2IY.jpg](https://snipboard.io/DgH2IY.jpg)
 *     ```
       function orb_check_for_out_of_stock_products() {
           if ( WC()->cart->is_empty() ) {
               return;
           }
   
           $removed_products = [];
   
           foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
               $product_obj = $cart_item['data'];
   
               if ( ! $product_obj->is_in_stock() ) {
                   WC()->cart->remove_cart_item( $cart_item_key );
                   $removed_products[] = $product_obj;
               }
           }
   
           if (!empty($removed_products)) {
               wc_clear_notices(); // remove any WC notice about sorry about out of stock products to be removed from cart.
   
               foreach ( $removed_products as $idx => $product_obj ) {
                   $product_name = $product_obj->get_title();
                   $msg = sprintf( __( "The product '%s' was removed from your cart because it is out of stock.", 'woocommerce' ), $product_name);
   
                   wc_add_notice( $msg, 'error' );
               }
           }
   
       }
   
       add_action('woocommerce_before_cart', 'orb_check_for_out_of_stock_products');
       ```
   
 * * I recommend using [Code Snippets](https://wordpress.org/plugins/code-snippets/)
   to add custom code to your site.
 * Let us know how it goes.
    Thanks.
 *  [MayKato](https://wordpress.org/support/users/maykato/)
 * (@maykato)
 * [4 years, 7 months ago](https://wordpress.org/support/topic/remove-items-4/#post-14963460)
 * HI [@danielbmxd](https://wordpress.org/support/users/danielbmxd/)
 * We’ve not heard back from you in a while, so I’m marking this thread as resolved.
   Hopefully, the above suggestion was helpful. If you have further questions, please
   feel free to open a new topic.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Remove items’ is closed to new replies.

 * ![](https://ps.w.org/woocommerce/assets/icon.svg?rev=3234504)
 * [WooCommerce](https://wordpress.org/plugins/woocommerce/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/woocommerce/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/woocommerce/)
 * [Active Topics](https://wordpress.org/support/plugin/woocommerce/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/woocommerce/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/woocommerce/reviews/)

 * 2 replies
 * 3 participants
 * Last reply from: [MayKato](https://wordpress.org/support/users/maykato/)
 * Last activity: [4 years, 7 months ago](https://wordpress.org/support/topic/remove-items-4/#post-14963460)
 * Status: resolved