Custom notice based on cart item stock quantity in Woocommerce

久未见 提交于 2020-02-29 07:21:11

问题


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.

The previous version seemed to be the wrong way to go. So here is my new approach with a different code.

in 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!';
    }

}

This function works for two cases:

  1. If all items in cart are in stock it echoes the right message (All items in cart are in stock ).
  2. If all items in cart are out of stock is echoes the right message (All items in cart are out of stock).

But if the cart contains items in stock AND items out of stock it echoes the first message (All items in cart are in stock).


回答1:


I have revisited your code. For cart items, $cart_item['data']; is the WC_Product Object that is needed, so the code will be much more compact.

The code below will display a custom notice when there is mixed stock for cart items that are in stock and out of stock at the same time.
The first function is the conditional function that checks cart items.
The second function use the first function to display conditionally a custom notice.

// 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');
    }
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

You can use the conditional function that check cart items stock in different hooks if needed

Related: Get in WooCommerce cart the product ID of a cart item




回答2:


Although the solution from @LoicTheAztec works perfect and is coded really clearly, well and elegant I came up with my own solution as well. I will post it here too, just to show two different approaches to the problem.

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
     }

}

this goes in functions.php



来源:https://stackoverflow.com/questions/52280406/custom-notice-based-on-cart-item-stock-quantity-in-woocommerce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!