如果购物车中有缺货商品,我需要在结帐订单中显示一个部分(b / c我们的产品可以延期交货)。以下是显示部分...
的代码的functions.php:
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() ){
//if cart has items out of stock
//if (cart has out of stock product) {
?>
<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', 'notes_in_cart' );
add_action( 'woocommerce_review_order_after_order_total', 'notes_in_cart' );
现在该部分一直显示。我知道我可能需要调用购物车物品并与foreach一起循环以确定是否缺货,但不确定如何。
答案 0 :(得分:1)
您可以这样做来检查&#34;库存数量&#34;和#34;允许延期交货?&#34;产品属性:
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']){ //thanks to LoicTheAztec for pointing it out in his answer
// 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;">*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', 'notes_in_cart' );
add_action( 'woocommerce_review_order_after_order_total', 'notes_in_cart' );
也可以使用get_stock_status()或is_in_stock()方法来完成,它返回&#34; Stock status&#34;的值。 - &#39; instock&#39;或者&#39; outofstock&#39; - 但不允许使用延期交货结账:https://github.com/woocommerce/woocommerce/issues/11187或https://github.com/woocommerce/woocommerce/issues/10834
编辑:有关为什么is_in_stock()不起作用的更多详细信息: is_in_stock()方法就是这个:
public function is_in_stock() {
return apply_filters( 'woocommerce_product_is_in_stock', 'instock' === $this->get_stock_status(), $this );
}
这意味着它会检查&#34;股票状态&#34;如果&#34;库存&#34;它返回true如果&#34;缺货&#34;则为假。
现在,如果你在这里阅读:https://conschneider.de/manage-stock-backorders-woocommerce/
你会发现:
只要库存状态为“in”,就可以进行延期交货 股票”。将产品设置为缺货将再次阻止购买 并显示“缺货”。
答案 1 :(得分:1)
已更新:由于所有产品均可在延期交货中使用,唯一有效的方法是检查 foreach
循环,如果库存数量每个购物车项目数量“足够”......
如果产品的库存数量小于购物车数量(WC显示“延期交货”),则会显示您的“货件单”
以下是代码:
function notes_in_cart() {
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)
}
// 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', 'notes_in_cart' );
add_action( 'woocommerce_review_order_after_order_total', 'notes_in_cart' );
代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。
此代码经过测试并有效。