我正在尝试显示自定义通知,具体取决于小麦订购者所订购的产品是否有缺货或全部有现货。
我已经修改了this answer中的代码。 它有效,但只能在购物车中使用。我希望它显示在任何地方:购物车,结帐,管理,电子邮件等。
这就是我所拥有的:
// Conditional function: Checking cart items stock
function is_mixed_stock_items(){
$enough_stock = $out_of_stock = false; // initializing
// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item ) {
// Get an instance of the WC_Product Object
$product = $cart_item['data'];
// Get stock quantity
$stock_qty = $product->get_stock_quantity();
// get cart item quantity
$item_qty = $cart_item['quantity'];
if( ( $stock_qty - $item_qty ) >= 0 ){
$enough_stock = true; // enough stock
} else {
$out_of_stock = true; // not enough stock
}
}
// return true if stock is mixed and false if not
return $enough_stock && $out_of_stock ? true : false;
}
// Display a custom notice
add_action( 'woocommerce_before_cart_totals', 'display_delivery_notification' );
function display_delivery_notification() {
if( is_mixed_stock_items() ){
echo'<div class="cart_totals">Forventet levering: 2-10 hverdage</div>';
}
else {
echo'<div class="cart_totals">Levering: 1-2 hverdage</div>';
}
}