我正在尝试为wordpress / woocommerce创建一个功能,以在购物车页面中显示一个选项以进行拆分交付。
它应该做的是检查购物车中所有物品的库存状态。
第一种情况::如果购物车中的所有项目均可用,则不输出任何内容。
第二种情况::如果购物车中的所有物品都缺货,则什么也不输出。
第三种情况:唯一应显示的条件是两种情况(第一种和第二种)均发生。
因此,只有当购物车中有商品并且有商品缺货时,它才应显示通知。
以前的版本似乎是错误的方法。因此,这是我使用不同代码的新方法。
在functions.php中:
add_action( 'woocommerce_after_cart_table', 'split_devliery_notification' );
function split_devliery_notification() {
$all_items_in_stock = true; // initializing
$all_items_out_of_stock = true;
// Iterating through cart items (to get the stock info)
foreach (WC()->cart->get_cart() as $cart_item) {
# HANDLING SIMPLE AND VARIABLE PRODUCTS
// Variable products
$variation_id = $cart_item['variation_id'];
if( 0 != $variation_id) {
$variation_obj = new WC_Product_variation($variation_id);
$stock = $variation_obj->get_stock_quantity();
} else {
// Simple products
$product_id = $cart_item['product_id'];
$product_obj = new WC_Product($product_id);
$stock = $product_obj->get_stock_quantity();
}
if( $stock <= 0 ){
// if an item is out of stock
$all_items_in_stock = false;
break; // We break the loop
}
elseif ( $stock >= 0 ) {
$all_items_out_of_stock = false;
break;
}
}
// All items "in stock"
if( $all_items_in_stock ) {
echo 'All items in cart are in stock';
}
elseif ( $all_items_out_of_stock ) {
echo 'All items in cart are out of stock';
}
else {
echo 'Some items in cart are in stock and some are out of stock -> Show notification ON!';
}
}
此功能适用于两种情况:
但是,如果购物车中有库存的商品和缺货,则会回显第一条消息(购物车中的所有商品都有库存)。
答案 0 :(得分:2)
我已经重新访问了您的代码。对于购物车商品, $cart_item['data'];
是所需的WC_Product
对象,因此代码将更加紧凑。
当同时有存货和缺货的购物车中有混合存货时,以下代码将显示自定义通知。
第一个功能是检查购物车项目的条件功能。
第二个功能使用第一个功能有条件地显示自定义通知。
// 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_after_cart_table', 'display_delivery_notification' );
function display_delivery_notification() {
if( is_mixed_stock_items() ){
$message = __("Some items in cart are in stock and some others out of stock -> Show notification ON!");
wc_print_notice( $message, 'notice');
}
}
代码进入您的活动子主题(或活动主题)的function.php文件中。经过测试,可以正常工作。
如果需要,您可以使用条件功能检查不同挂钩中的购物车物品
答案 1 :(得分:0)
尽管@LoicTheAztec的解决方案工作完美,并且编码非常清晰,清晰,优雅,但我还是提出了自己的解决方案。我也将其张贴在这里,只是为了展示解决该问题的两种方法。
add_action( 'woocommerce_after_cart_contents', 'split_devliery_notification' );
function split_devliery_notification() {
$all_items_in_stock = true; // initializing a true statement at beginning to be proved
$all_items_out_of_stock = true;
// Iterating through cart items (to get the stock info)
foreach (WC()->cart->get_cart() as $cart_item) {
# HANDLING SIMPLE AND VARIABLE PRODUCTS
// Variable products
$variation_id = $cart_item['variation_id'];
if( 0 != $variation_id) {
$variation_obj = new WC_Product_variation($variation_id);
$stock = $variation_obj->get_stock_quantity();
} else {
// Simple products
$product_id = $cart_item['product_id'];
$product_obj = new WC_Product($product_id);
$stock = $product_obj->get_stock_quantity();
}
if ( $stock <= 0 ){
// if an item is out of stock
$all_items_in_stock = false;
break; // We break the loop
}
}
// Continue with iterating if first iterating was stopped because one item has stock status below 0
foreach (WC()->cart->get_cart() as $cart_item) {
# HANDLING SIMPLE AND VARIABLE PRODUCTS
// Variable products
$variation_id = $cart_item['variation_id'];
if( 0 != $variation_id) {
$variation_obj = new WC_Product_variation($variation_id);
$stock = $variation_obj->get_stock_quantity();
} else {
// Simple products
$product_id = $cart_item['product_id'];
$product_obj = new WC_Product($product_id);
$stock = $product_obj->get_stock_quantity();
}
// check if stock status is 0 or above. If not it is proven that there are both types in cart
if ( $stock >= 0 ) {
$all_items_out_of_stock = false;
break;
}
}
// All items "in stock"
if( $all_items_in_stock ) {
// commented out to prevent displaying a message in that case
// echo 'All items in cart are in stock';
}
elseif ( $all_items_out_of_stock ) {
// commented out to prevent displaying a message in that case
// echo 'All items in cart are out of stock';
}
else {
?>
<div class="split-delivery-notification-container">
<div class="split-delivery-notification-text">
Delivery notification ON.
</div>
</div>
<?php
}
}
这可以在functions.php中找到