I am trying to create a function for wordpress/woocommerce to show an option in the cart page for split delivery.
What it should do is to check the stock status of all items in cart.
First case: If all items in cart are available output nothing.
Second case: If all items in cart are out of stock, output nothing.
Third case: The only condition when something should be shown is, when both cases (first and second) occurs.
So only if the cart has items in stock AND has items out of stock it should display a notification.
I've found a code here at stackoverflow which nearly does what I want. The only difference is, it doesn't combine the first two cases. It displays the message as soon as one item is out of stock.
Here is the code I've found from a previous question already a little bit modified to meet my ideas:
// Loop that check if cart items have enough stock quantity
$is_available_on_backorder = false;
foreach ( WC()->cart->get_cart() as $item_values ) {
$stock_qty = $item_values['data']->get_stock_quantity(); // Product stock
$item_qty = $item_values['quantity']; // Cart Item quantity
// Testing if the product has enough stock
if( $stock_qty < $item_qty ){
$is_available_on_backorder = true; // The condition is met
break; // we stop the loop
}
}
if ( WC()->cart->needs_shipping() && $is_available_on_backorder ){
?>
<tr class="ceckoutStockMeta">
<th>Item Shipments</th>
<td>
<p style="color: red;">*You have one or more items in your cart that are currently out of stock. Please select a custom shipping option for your order.</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', 'split_delivery_in_cart' );
I've tried various ideas I had, but as I'am not very though with php it wasn't successful. Do you have some ideas how to modify the above code to achieve the goal?
The url to the source where I got the code from is:
Check if there are out of stock items in cart - WooCommerce
Aucun commentaire:
Enregistrer un commentaire