Poner un límite a las ventas de un producto al día

    Si quieres que un producto de WooCommerce no se pueda vender más de X veces al día, usa este snippet. Recuerda modificar el ID del producto y la cantidad.

    /* Limitar ventas diarias del producto con ID 12345 */
    add_filter('woocommerce_is_purchasable', 'ayudawp_limite_ventas_diarias_12345', 9999, 2);
    
    function ayudawp_limite_ventas_diarias_12345($is_purchasable, $product) {
        $limit_product_id = 12345; //ESTE DEBE SER EL ID DEL PRODUCTO A LIMITAR
        if ($product - > get_id() !== $limit_product_id) return $is_purchasable;
        // OBTENEMOS LOS PEDIDOS DIARIOS Y VOLVEMOS AL LOOP
        $all_orders = wc_get_orders(
            array(
                'limit' => -1,
                'date_created' => date('Y-m-d'),
                'return' => 'ids',
            )
        );
        $count = 0;
        foreach($all_orders as $all_order) {
            $order = wc_get_order($all_order);
            $items = $order - > get_items();
            foreach($items as $item) {
                $product_id = $item - > get_product_id();
                if ($product_id && $product_id == $limit_product_id) {
                    $count = $count + absint($item['qty']);
                }
            }
        }
        // LIMITAMOS A 15 VENTAS DIARIAS
        if ($count >= 15) return false;
        return $is_purchasable;
    }