WooCommerce Check stock of product ID before adding

user2115227 picture user2115227 · Dec 14, 2016 · Viewed 8.3k times · Source

I have some custom code in my WooCommerce site that adds a product to the users cart. I already have it check the cart contents to make sure there is another product in the cart first, but I also want it to check that the product that is being added is in stock also...

I can't work out the best way to do this. Would really appreciate if you can let me know what to add to the function to make it check the stock of "product_id" and if the stock is > 0. Here's my code:

add_action( 'template_redirect', 'add_product_to_cart' );
function add_product_to_cart() {
    if ( ! is_admin() ) {
        $product_id = 21576;
        $found = false;
        global $woocommerce;

        //check if product already in cart

    if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
        foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
            $_product = $values['data'];
            if ( $_product->id == $product_id )
                $found = true;
        }
        // if product not found, add it
        if ( ! $found )
            WC()->cart->add_to_cart( $product_id );

        if (sizeof( WC()->cart->get_cart() ) == 1 && $found == true ) {
            $woocommerce->cart->empty_cart();
        }
    } 
}

}

Answer

LoicTheAztec picture LoicTheAztec · Dec 14, 2016

You can use the WC_Product conditional method is_in_stock() directly in your if statement.

As $product is already a product object, we don't need anything else to make it work with WC_product methods.

Also instead of using sizeof( WC()->cart->get_cart() ) > 0 you can replace by the WC_cart method is_empty() this way ! WC()->cart->is_empty().

Then you can also replace sizeof( WC()->cart->get_cart() ) == 1 using WC_cart get_cart_contents_count() method this way WC()->cart->get_cart_contents_count() == 1.

You don't need anymore the global woocommerce; declaration if you use WC()->cart instead of $woocommerce->cart with all WC_Cart methods

Last thing:
May be is better to remove the concerned cart item, instead emptying the cart. So we will use remove_cart_item() method instead.
If is not convenient you can use empty_cart() method as in your original code…

So your code is going to be:

add_action( 'template_redirect', 'add_product_to_cart' );
function add_product_to_cart() {
    if ( ! is_admin() ) {
        $targeted_product_id = 21576;
        $found = false;

        //check if product already in cart

        if ( ! WC()->cart->is_empty() ) {
            foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
                $product = $cart_item['data'];
                if ( $product->id == $targeted_product_id ) {
                    $found = true;
                    break; // We can break the loop
                }
            }
            // Update HERE (we generate a new product object $_product)
            $_product = wc_get_product( $targeted_product_id );
            // If product is not in the cart items AND IS IN STOCK  <===  <===  @@@@@@@
            if ( !$found && $_product->is_in_stock() ) {
                WC()->cart->add_to_cart( $targeted_product_id );
            } elseif ( $found && WC()->cart->get_cart_contents_count() == 1 ) {
                // Removing only this cart item
                WC()->cart->remove_cart_item( $cart_item_key );
                // WC()->cart->empty_cart();
            }
        }
    } 
}

This code is tested and works.

Code goes in function.php file of your active child theme (or theme). Or also in any plugin php files.