I wish to allow customers to add items to their Woocommerce cart and leave it there for as long as they want, so they can add to the cart at their leisure. Any lines that go out of stock need to automatically be removed from the cart and a message to show that this has occurred. Something like "All out of stock items have been removed from the account as they are no longer available".
So far I have tried this
public function is_in_stock() {
return apply_filters( 'woocommerce_product_is_in_stock', 'instock' === $this->get_stock_status(), $this );
}
function notes_in_cart() {
global $woocommerce;
if ( ! $_POST || ( is_admin() && ! is_ajax() ) ) {
return;
}
if ( isset( $_POST['post_data'] ) ) {
parse_str( $_POST['post_data'], $post_data );
} else {
$post_data = $_POST; // fallback for final checkout (non-ajax)
}
if ( WC()->cart->needs_shipping() ){
// set $out_of_stock_exists to false by default
$out_of_stock_exists = false;
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
if($values['data']->backorders_allowed()){ //check if backorders are allowed on this product
// get the stock quantity - returns the available amount number
$stock_info = $values['data']->get_stock_quantity();
if($stock_info < $values['quantity']){
set $out_of_stock_exists to true and stop foreach execution
$out_of_stock_exists = true;
break;
}
}
}
//if cart has items out of stock
if ($out_of_stock_exists) {
?>
<tr class="ceckoutStockMeta">
<th>Item Shipments</th>
<td>
<p style="color: red;">*All out of stock items have been removed from your cart as they are no longer available.</p><br>
<form>
<input type="radio" name="stockOp" id="stockOption1" value="ship" />
<label for="stockOption1">Ship what is available now</label><br>
<input type="radio" name="stockOp" id="stockOption2" value="hold" />
<label for="stockOption2">Wait and ship together</label>
</form>
</td>
</tr>
<?php
}
}
add_action( 'woocommerce_cart_totals_after_order_total', 'notes_in_cart' );
add_action( 'woocommerce_review_order_after_order_total', 'notes_in_cart' );
I am not sure if all of this is necessary, considering backorders need to be disallowed anyway.
Can someone tell me if this is correct?
As for auto removal of out of stock lines from an account's cart, I am guessing that this will happen 'out of the box' with Woocommerce. Can someone please confirm this or provide a way to do it?
Thank you, Bryan