In my Wordpress store, products are set as simple, but I use WS Form to add attributes to the product/order.

We sell travels packages. When a user purchase a product, the quantity decrease to 1. But if they pick 4 travelers in the booking form, the correct approach should be to decrease the stock of 4 (total number of people partecipating).

I tried this snippet that althought it correctly updates the stock quantity on purchase, it makes the wrong calculation:

add_action('woocommerce_checkout_order_processed', 'adjust_stock_based_on_travelers_checkout', 10, 1);

function adjust_stock_based_on_travelers_checkout($order_id) {
    $order = wc_get_order($order_id);

    foreach ($order->get_items() as $item_id => $item) {
        // Passeggeri totali means "Total passengers"
        $passenger_total = wc_get_order_item_meta($item_id, 'Passeggeri totali', true);
        $passenger_total = intval($passenger_total);

        if ($passenger_total > 0) {
            $product = $item->get_product();
            if ($product && $product->managing_stock()) {
                // Correctly decrease the stock
                $new_stock = wc_update_product_stock($product, $passenger_total * -1, 'decrease');
  • I would var_dump( $passenger_total ) to make sure that the correct value is being passed, however, this functionality already exists within existing WooCommerce add-ons, so not sure why you're adding on WP Forms and trying to hack it together. Have you looked at WooCommerce Bookings? It's specifically designed to do exactly what you want done. Mar 25 at 17:01
  • Hi Tony. Woocommerce booking allows the user to pick dates for its booking. Let's say you want to book a house, you can pick any timeframe among the allowed terms. This is not our usecase. We're not a travel agency but a tour operator company that means we organize travel for very specic dates. Each date should be handle as a separate product although it is the same travel. I'm over simplifying here, but it's just how it should be. If you happen to know any other alternatives I'll be happy to look into it Mar 26 at 9:26
  • You may be able to restrict those details in WooCommerce Bookings, from what I remember it's really robust. I haven't used it with your exact use case but I have used it in similar incidences. Regardless, does the var_dump() return any values? Mar 26 at 12:24


Browse other questions tagged or ask your own question.