Last updated on September 25, 2023

WooCommerce inventory management

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Streamline WooCommerce inventory management.

WooCommerce is a popular e-commerce plugin for WordPress that allows you to manage and sell products on your website. One important aspect of running an online store is inventory management, which involves keeping track of product stock levels, updating stock quantities, and handling backorders. In this response, I will explain how to manage inventory in WooCommerce using code examples that follow the WordPress coding standards.

To get started with inventory management in WooCommerce, you can use the following code snippet to retrieve the stock quantity of a product:

$product_id = 123; // Replace with the actual product ID
$stock_quantity = get_post_meta( $product_id, '_stock', true );

echo 'Stock Quantity: ' . $stock_quantity;

This code snippet uses the get_post_meta() function to retrieve the stock quantity of a product. The $product_id variable should be replaced with the actual ID of the product you want to retrieve the stock quantity for. The _stock meta key is used to store the stock quantity in WooCommerce.

To update the stock quantity of a product, you can use the following code snippet:

$product_id = 123; // Replace with the actual product ID
$new_stock_quantity = 10; // Replace with the new stock quantity

update_post_meta( $product_id, '_stock', $new_stock_quantity );

This code snippet uses the update_post_meta() function to update the stock quantity of a product. The $product_id variable should be replaced with the actual ID of the product you want to update. The _stock meta key is used to store the stock quantity in WooCommerce, and the $new_stock_quantity variable should be replaced with the new stock quantity.

To handle backorders in WooCommerce, you can use the following code snippet:

$product_id = 123; // Replace with the actual product ID
$backorders_allowed = get_post_meta( $product_id, '_backorders', true );

if ( 'yes' === $backorders_allowed ) {
    // Backorders are allowed
    echo 'Backorders are allowed for this product.';
} else {
    // Backorders are not allowed
    echo 'Backorders are not allowed for this product.';
}

This code snippet retrieves the backorder status of a product using the _backorders meta key. If the value is set to 'yes', it means backorders are allowed for the product. You can customize the code to perform specific actions based on the backorder status.

These code snippets can be useful when building custom inventory management functionality in WooCommerce, such as displaying stock quantities on the front-end, updating stock levels programmatically, or implementing custom backorder handling logic.

Examples

Example 1: Update Product Stock Quantity

This use case demonstrates how to update the stock quantity of a WooCommerce product programmatically using the wp_update_post() and update_post_meta() functions.

/**
 * Update product stock quantity.
 *
 * @param int $product_id The ID of the product.
 * @param int $quantity The new stock quantity.
 */
function wpsnippets_update_product_stock_quantity( $product_id, $quantity ) {
    // Update the product post.
    wp_update_post( array(
        'ID' => $product_id,
        'post_type' => 'product',
    ) );

    // Update the stock quantity meta.
    update_post_meta( $product_id, '_stock', $quantity );
}

Explanation: The wpsnippets_update_product_stock_quantity() function takes the product ID and the new stock quantity as parameters. It first updates the product post using wp_update_post() to ensure any changes are saved. Then, it updates the stock quantity meta using update_post_meta() with the _stock meta key.

Example 2: Decrease Stock Quantity on Order Completion

This use case demonstrates how to decrease the stock quantity of a WooCommerce product automatically when an order is completed using the woocommerce_order_status_completed action hook.

/**
 * Decrease stock quantity on order completion.
 *
 * @param int $order_id The ID of the completed order.
 */
function wpsnippets_decrease_stock_on_order_completion( $order_id ) {
    $order = wc_get_order( $order_id );

    // Loop through order items.
    foreach ( $order->get_items() as $item ) {
        $product_id = $item->get_product_id();
        $quantity = $item->get_quantity();

        // Decrease stock quantity.
        $stock_quantity = get_post_meta( $product_id, '_stock', true );
        $new_stock_quantity = $stock_quantity - $quantity;
        update_post_meta( $product_id, '_stock', $new_stock_quantity );
    }
}
add_action( 'woocommerce_order_status_completed', 'wpsnippets_decrease_stock_on_order_completion' );

Explanation: The wpsnippets_decrease_stock_on_order_completion() function is hooked to the woocommerce_order_status_completed action. It retrieves the completed order using wc_get_order() and then loops through the order items. For each item, it gets the product ID and quantity, retrieves the current stock quantity using get_post_meta(), calculates the new stock quantity by subtracting the order quantity, and updates the stock quantity meta using update_post_meta().

Example 3: Check Stock Availability on Add to Cart

This use case demonstrates how to check the stock availability of a WooCommerce product before allowing it to be added to the cart using the woocommerce_add_to_cart_validation filter hook.

/**
 * Check stock availability on add to cart.
 *
 * @param bool $passed Whether the product can be added to the cart.
 * @param int $product_id The ID of the product being added.
 * @param int $quantity The quantity being added.
 * @param int $variation_id The ID of the product variation being added.
 * @return bool Whether the product can be added to the cart.
 */
function wpsnippets_check_stock_availability( $passed, $product_id, $quantity, $variation_id ) {
    $stock_quantity = get_post_meta( $product_id, '_stock', true );

    if ( $stock_quantity < $quantity ) {
        $passed = false;
        wc_add_notice( __( 'Insufficient stock quantity.', 'woocommerce' ), 'error' );
    }

    return $passed;
}
add_filter( 'woocommerce_add_to_cart_validation', 'wpsnippets_check_stock_availability', 10, 4 );

Explanation: The wpsnippets_check_stock_availability() function is hooked to the woocommerce_add_to_cart_validation filter. It retrieves the stock quantity of the product using get_post_meta() and compares it with the quantity being added. If the stock quantity is less than the requested quantity, it sets $passed to false and displays an error notice using wc_add_notice(). The function then returns the updated value of $passed.

Last updated on September 25, 2023. Originally posted on September 30, 2023.

Leave a Reply

Your email address will not be published. Required fields are marked *