Uptaded (для обработки нескольких элементов, подлежащих удалению) .
Попробуйте следующий пересмотренный код, который также будет лучше работать с вариациями продукта:
add_action( 'woocommerce_check_cart_items', 'conditionally_remove_specific_cart_item' );
function conditionally_remove_specific_cart_item() {
## -- Settings -- ##
$min_required = 300000; // The minimal required cart subtotal
$items_to_remove = array( 446, 27 ); // Product Ids to be removed from cart
// Only if cart subtotal is less than 300000
if( WC()->cart->cart_contents_total >= $min_required )
return;
// Loop through cart items
foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
if( array_intersect( $items_to_remove, array( $cart_item['variation_id'], $cart_item['product_id'] ) ) ) {
// Remove cart item
WC()->cart->remove_cart_item( $cart_item_key );
// Add a custom notice
wc_add_notice( sprintf(
__( "Your gift has been removed from the cart because its requires a minimal cart amount of %s", "woocommerce" ),
$min_required ), 'notice' );
}
}
}
Код помещается в файл function.php вашей активной дочерней темы (или активной темы).Проверено и работает.
В корзине:
![enter image description here](https://i.stack.imgur.com/OfL8h.png)
На странице оформления заказа:
![enter image description here](https://i.stack.imgur.com/lL3ok.png)