mirror of
https://gh.wpcy.net/https://github.com/mainwp/Code-Snippets-Functions.git
synced 2026-05-02 12:02:25 +08:00
23 lines
698 B
Text
23 lines
698 B
Text
add_action( 'template_redirect', 'remove_oos_items_from_cart_on_cart_and_checkout' );
|
|
|
|
function remove_oos_items_from_cart_on_cart_and_checkout() {
|
|
if ( is_admin() || ! WC()->cart || ( ! is_cart() && ! is_checkout() ) ) {
|
|
return;
|
|
}
|
|
|
|
$cart = WC()->cart;
|
|
$removed = false;
|
|
|
|
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
|
|
$product = $cart_item['data'];
|
|
|
|
if ( ! $product->is_in_stock() ) {
|
|
$cart->remove_cart_item( $cart_item_key );
|
|
$removed = true;
|
|
}
|
|
}
|
|
|
|
if ( $removed ) {
|
|
wc_add_notice( __( 'Some out-of-stock products have been removed from your cart.', 'woocommerce' ), 'notice' );
|
|
}
|
|
}
|